Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Checkbox value issue in databind grid.
 

DataGridView Checkbox value issue in databind grid.

Hello to all,

I have got some problem with a DataGridViewCheckBoxColumn.

I use a DataGridView to display a list of channel which is bound to the DataGridView's DataSource Property.

In addition to that, i add a DataGridViewCheckBoxColumn to the DataGridView which is unbound.

I load list of channels on FormLoad event. Now user can create new profiles by selecting list of Channels in the grid. User can also later edit previously created profile.

Here is the problem, When user selects any of the previously created profile there is a function which checks the saved Channels with that particular profile, I am unable to assign a value to checkboxcolumn to show saved data.

My Findings: I can assign a value, which actually is stored and can be seen in the debugger watch, but the control is not visually checked .

I can check it manually, and I can as well read the value of the Checkbox.

here is the code:

for (int i = 0; i < gvChannels.Rows.Count; i++) //gvChannels is DataGridView
{
if (gvChannels.Rows[i].Cells[1].Value.ToString() == ht["ChannelNumber"].ToString()) //ht is hashtable containing saved channels, I need to check if bind channel matches the saved channel then checkbox should be checked.
{
gvChannels.Rows[i].Cells[0].Value = 1;
break;
}
}

peter1981  Thursday, September 24, 2009 6:47 AM

Hi,

Have you set the true value and false value of the checkboxColumn?

If not, the default checkboxcell will be cheked when you set the value to true or 1.

You can try the following code:

DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

chk.Name = "Check";

chk.HeaderText = "Test";

dataGridView1.Columns.Add(chk);

dataGridView1.Rows[0].Cells["Check"].Value = 1;

dataGridView1.Rows[1].Cells["Check"].Value = true;

DataGridViewCheckBoxColumn chk02 = new DataGridViewCheckBoxColumn();

chk02.Name = "Check02";

chk02.HeaderText = "Test02";

chk02.TrueValue = "aa";

chk02.FalseValue = "bb";

dataGridView1.Columns.Add(chk02);

dataGridView1.Rows[0].Cells["Check02"].Value = "aa";

dataGridView1.Rows[1].Cells["Check02"].Value = "bb";

By the way, the checked state of unbound checkboxcells will lose when you sort the column. You can use a dictionary object to store the checked state.

29. How do I show unbound data along with bound data?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq29

More information:

DataGridViewCheckBoxColumn.FalseValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.falsevalue.aspx

DataGridViewCheckBoxColumn.TrueValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.truevalue.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 30, 2009 1:29 PM

Hi,

Have you set the true value and false value of the checkboxColumn?

If not, the default checkboxcell will be cheked when you set the value to true or 1.

You can try the following code:

DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

chk.Name = "Check";

chk.HeaderText = "Test";

dataGridView1.Columns.Add(chk);

dataGridView1.Rows[0].Cells["Check"].Value = 1;

dataGridView1.Rows[1].Cells["Check"].Value = true;

DataGridViewCheckBoxColumn chk02 = new DataGridViewCheckBoxColumn();

chk02.Name = "Check02";

chk02.HeaderText = "Test02";

chk02.TrueValue = "aa";

chk02.FalseValue = "bb";

dataGridView1.Columns.Add(chk02);

dataGridView1.Rows[0].Cells["Check02"].Value = "aa";

dataGridView1.Rows[1].Cells["Check02"].Value = "bb";

By the way, the checked state of unbound checkboxcells will lose when you sort the column. You can use a dictionary object to store the checked state.

29. How do I show unbound data along with bound data?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq29

More information:

DataGridViewCheckBoxColumn.FalseValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.falsevalue.aspx

DataGridViewCheckBoxColumn.TrueValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.truevalue.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 30, 2009 1:29 PM

You can use google to search for other answers

Custom Search

More Threads

• GridView and Dropdownlist issues or bug?
• Programmatically add a row to a dataset table with IDENTITY column
• Relational DataSet and Datagrid Insert
• DataGridView does't show updated data from dataset
• Printing
• Tab control for windows form. Is their a way to Hide and unhide a tab.
• Accessing DataGridView Index from Bound DataTable Index
• DatagridViewComboBoxCell value is not valid
• Selecting a record from a Datagrid
• Database is changing when cell is editing