我创建了一个Windows窗体应用程序并插入了一个DataGridViewCheckBoxColumn作为网格的第一列,但是我没有成功将默认值设置为“checked”。我插入列后并在gridview的DefaultValuesNeeded事件中尝试将其设置为循环。有没有更简单的方法来做到这一点,还是有什么我想念的?
我的代码如下:
private void FillDataGrid()
{
BAFarmer baobj = new BAFarmer();
gvFarmers.AutoGenerateColumns = true;
gvFarmers.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
gvFarmers.DataSource = baobj.GetAllFarmersCol();
gvFarmers.Enabled = true;
gvFarmers.ReadOnly = false;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "Include";
checkColumn.ValueType = typeof(Boolean);
checkColumn.HeaderText = "Include";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
checkColumn.ThreeState = false;
checkColumn.TrueValue = true;
checkColumn.FalseValue = false;
gvFarmers.Columns.Insert(0, checkColumn);
foreach (DataGridViewRow row in gvFarmers.Rows)
{
row.Cells[checkColumn.Name].Value = true;
}
}
答案 0 :(得分:0)
我能够通过向表单加载事件添加代码来解决这个问题,该事件遍历复选框并在那时设置它们。
foreach (DataGridViewRow row in gvFarmers.Rows)
{
DataGridViewCheckBoxCell chk = DataGridViewCheckBoxCell)row.Cells["Select"];
chk.Value = true;
}