Reputation: 115
I'm trying to resize a DataGridViewCell to smaller width than autosize does.
For example, in one column there is only "A" or nothing possible, but if I resize the column (doesn't matter if manually or programmatically) the string is "cut off" to "A...".
Is there a way to suppress that behaviour? I just want the "A" to fit exactly between the borders of that cell in order to provide the maximum amount of data possible.
Edit: Just to make my problem clear: autosize leaves a litle amount of blank space on every side of the column.
Upvotes: 2
Views: 1794
Reputation: 4489
After give the datasource to grid.Please put this code to auto resize the cell width according to data.
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
For Detail you can drill down in that link
Upvotes: 0
Reputation: 32541
You should trim the values. Probably you have trailing white-spaces after A
. Here's a short demo which explains the behavior:
dataGridView1.DataSource = new[] {
new{A="A "},
new{A="A"},
new{A=""}
};
After re-sizing, you'll see that only the first row has the trailing dots.
Alternatively, you may try to set the column's wrap mode, padding, alignment and width:
dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[0].DefaultCellStyle.Padding =new Padding(0);
dataGridView1.Columns[0].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[0].Width = (int)this.Font.Size * 96 / 72;
Upvotes: 3