WinForm 应用程序:无法设置 DataGridViewCheckBoxColumn 的默认值
WinForm app: Cannot set default values of a DataGridViewCheckBoxColumn
我已经创建了一个 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;
}
}
我能够通过向表单加载事件添加代码来解决这个问题,该事件循环检查复选框并在该点设置它们。
foreach (DataGridViewRow row in gvFarmers.Rows)
{
DataGridViewCheckBoxCell chk = DataGridViewCheckBoxCell)row.Cells["Select"];
chk.Value = true;
}
我已经创建了一个 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;
}
}
我能够通过向表单加载事件添加代码来解决这个问题,该事件循环检查复选框并在该点设置它们。
foreach (DataGridViewRow row in gvFarmers.Rows)
{
DataGridViewCheckBoxCell chk = DataGridViewCheckBoxCell)row.Cells["Select"];
chk.Value = true;
}