i want to make a chekbox column in my datagrid,and wants to do some action on checked and unchecked,i am using c# in window application,can anybody help me regarding this......that how to apply these checkbox.??
thanx in advance,,
amit hasija
i want to make a chekbox column in my datagrid,and wants to do some action on checked and unchecked,i am using c# in window application,can anybody help me regarding this......that how to apply these checkbox.??
thanx in advance,,
amit hasija
For : since you said this is a Windows Forms app, the simplest, most robust option is to use a DataGridView with a DataGridViewCheckBoxColumn (or bind a bool DataColumn in a DataTable). pointed to an online sample and mentioned he posted a solution; the notes below focus on the common pitfalls that those examples sometimes miss and a compact, practical pattern to use now.
Add a checkbox column (bound or unbound), then handle the immediate-change issue so checking fires code as soon as the box is clicked. The two events to use are CurrentCellDirtyStateChanged (call CommitEdit) and CellValueChanged to react to the new value:
dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Select", HeaderText = "Select" });
dataGridView1.CurrentCellDirtyStateChanged += (s,e) =>
{
if (dataGridView1.IsCurrentCellDirty && dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
};
dataGridView1.CellValueChanged += (s,e) =>
{
if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Select"].Index)
{
bool checkedState = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
// handle check/uncheck here
}
}; Troubleshooting and tips: if you bind to a DataTable, make sure the DataColumn is type bool (or a bool property on your objects when binding a BindingList<T>). When reading values, guard against null/DBNull (use Convert.ToBoolean or bool.TryParse). To collect checked rows, iterate Rows and use Convert.ToBoolean on the checkbox cell. If you are on older .NET and using the legacy DataGrid, create a boolean DataColumn or a DataGridBoolColumn instead. If the thread contains a posted solution from , compare this event-handling pattern — it resolves the frequent issue of the checkbox value not being committed immediately.
Jump to Post— c# seeker 0hey pal,
You may have found the solution by now. If you haven't check out this link.
hey pal,
You may have found the solution by now. If you haven't check out this link.
i have already provide solution for this.just check my posts.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.