我正在使用DataGridView,并且想知道是否有一个属性会在内容需要时自动使单元格更大。
到目前为止,我已经在DataGridViewBindingComplete处理程序的末尾:
dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
但遗憾的是,这并没有成功。
这是我到目前为止所尝试的内容:
public partial class Form1 : Form
{
private void dgv1BindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
}
public Form1()
{
InitializeComponent();
// [...] set up datasource: orders
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.DataSource = orders;
DataGridViewTextBoxColumn idCol = new DataGridViewTextBoxColumn();
idCol.DataPropertyName = "id";
idCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
idCol.HeaderText = "#";
idCol.DisplayIndex = 0;
DataGridViewTextBoxColumn placedCol = new DataGridViewTextBoxColumn();
placedCol.DataPropertyName = "placed";
placedCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
placedCol.HeaderText = "Time Placed";
placedCol.DisplayIndex = 1;
// [...] more of these columns
dataGridView1.Columns.Add(idCol);
dataGridView1.Columns.Add(placedCol);
// [...] adding the rest of the columns
dataGridView1.DataBindingComplete += dgv1BindingComplete;
}
}
具有以下结果:
答案 0 :(得分:10)
答案隐藏在另一个Stackoverflow问题中:How to set DataGridView textbox column to multi-line?
将DefaultCellStyle.WrapMode设置为TriState.True就可以了。
答案 1 :(得分:4)
datagridview1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
datagridview1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
答案 2 :(得分:2)
将 datagridview 的属性 AutoSizeColumnMode 设置为 AllCells 并进行检查。< / p>