Tutorial :Dynamically Added CheckBox Column is Disabled in GridView



Question:

I'm dynamically adding a Boolean column to a DataSet. The DataSet's table is the DataSource for a GridView, which AutoGenerates the columns.

Issue: The checkboxes for this dynamically generated column are all disabled. How can I enable them?

ds.Tables["Transactions"].Columns.Add("Retry", typeof(System.Boolean));  ds.Tables["Transactions"].Columns["Retry"].ReadOnly = false;  

In other words, how can I control how GridView generates the CheckBoxes for a Boolean field? (And why does setting ReadOnly to False have no effect?)

Thanks!


Solution:1

You should probably add CellContentClick event handler and negate the value that the cell already has. Like:

private void dgvRaceDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)  {              if (e.ColumnIndex == 5)              {                  dgvRaceDetails.Rows[e.RowIndex].Cells[5].Value =                      !(Boolean)dgvRaceDetails.Rows[e.RowIndex].Cells[5].Value;              }                if (e.ColumnIndex == 6)              {                  dgvRaceDetails.Rows[e.RowIndex].Cells[6].Value =                      !(Boolean)dgvRaceDetails.Rows[e.RowIndex].Cells[6].Value;              }  }  


Solution:2

I believe they will be automatically disabled until they have a value.

DataRow row = ds.Tables["Transactions"].NewRow();  row("Retry") = true;  ds.Tables["Transactions"].Rows.Add(row)  


Solution:3

Set the value of Boolean to true. Its default value is false. I hope this helps.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »