I have created a Windows Forms Application and inserted a DataGridViewCheckBoxColumn as the first column of the grid, but I have been unsuccessful in setting the default value to "checked". I have tried to set it both in a loop after I insert the column and in the DefaultValuesNeeded event of the gridview. Is there an easier way to do this, or is there something I'm missing?
My code is as follows:
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;
}
}
I was able to solve this by adding code to the form load event that loops through the check boxes and sets them at that point.
foreach (DataGridViewRow row in gvFarmers.Rows)
{
DataGridViewCheckBoxCell chk = DataGridViewCheckBoxCell)row.Cells["Select"];
chk.Value = true;
}