问题
I ve created columns of my datagrid using this,
private void Receive_Load(object sender, System.EventArgs e)
{
DataGridView1.Columns.Add("Sender",typeof(string));
DataGridView1.Columns.Add("Time",typeof(string));
DataGridView1.Columns.Add("Message",typeof(string));
}
- How can i dynamically assign a column width to a winforms datagrid?
回答1:
I think you are searching for something line
DataGridView1.Columns["ColumnName"].Width = 75;
I Hope it help you.
In addition, you can set the AutoSizeMode of the column to obtain different behaviours automatically. For example, if you set it to ColumnHeader, then the cell width will be set to the best fit for showing the header text. You can get more info in this Link.
回答2:
Here's a suggestion: If you know the length of your fields you can multiply their length by a constant value (max character width for example) to produce a dynamic width.
来源:https://stackoverflow.com/questions/2175274/dynamically-assign-a-column-width-to-a-winforms-datagrid