Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
I have a DataGridView which filled from binding source
the data i get from the source contains a 0,1 column witch i show it as checkbox

I want to invert checkbox value in the DataGridView
(means if value=0>>checked , if value=1>>unchecked)
Posted
Comments
CHill60 24-Jan-15 9:46am    
How do you get the data from the source (e.g. what is the sql query?)
M. Daban 24-Jan-15 9:56am    
its "select *" but i can't edit it
CHill60 24-Jan-15 9:59am    
Why can't you edit it?
M. Daban 24-Jan-15 10:00am    
its from dll
George Swan 24-Jan-15 10:07am    
Is this any use to you?
http://www.codeproject.com/Articles/24330/WPF-Bind-to-Opposite-Boolean-Value-Using-a-Convert

Hello Mr.Daban,
You Can access any control value on rowDataBound event of gridView by using .net control
below code is in c# language


C#
protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text == 0)
            {
                ((CheckBox)e.Rows.Cells[1].Controls[0]).Checked= true;
            }
             else  if (e.Row.Cells[0].Text == 1)
            {
                ((CheckBox)e.Rows.Cells[1].Controls[0]).Checked= false;
            }
        }
    }
 
Share this answer
 
v3
If your cells are really check box cells, the values are not 0 or 1, they are Boolean values.

You can do this to all the cells in questions:
C#
DataGridViewCell cell = // some cell of the runtime type DataGridViewCheckBoxCell
cell.Value = !((bool)cell.Value);

If you have to, you can checkup either the runtime type of the cell, or, better, the property DataGridViewCell.ValueType, to make sure you are working with the cell or appropriate type.

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.valuetype(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
M. Daban 25-Jan-15 8:04am    
i don't want to change source data, i want gui change(apperance) only
Sergey Alexandrovich Kryukov 25-Jan-15 12:14pm    
Then read my answer more carefully. I also mean UI.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900