Click here to Skip to main content
16,020,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello House,

i am working with datagrid that has a level checkbox of which if it is checked others checkbox in the datagrid will also be checked (just the same way yahoomail works), that is working fine for me, i am having issues unchecking the top level checkbox if any of the checkboxes is unchecked, please i need your assistance. here is my code:

Global variables:
C#
CheckBox chkbox=new CheckBox( );
        DataTable dt = new DataTable( );



C#
private void Form1_Load(object sender, EventArgs e)
        {
            //CheckBox chkbox=new CheckBox( );
            dataGridView1.AllowUserToAddRows = false;
            
            dataGridView1.Columns.Clear( );
            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn( );            
            colCB.Name = "chkcol";
            colCB.HeaderText = "";
           
            colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns.Add( colCB );

            Rectangle rect = this.dataGridView1.GetCellDisplayRectangle( 0, -1, true );
           
            chkbox.Size = new Size( 18, 18 );
            rect.Offset( 43, 2 );
            chkbox.Location = rect.Location;
            chkbox.CheckedChanged += chkBoxChange;
            this.dataGridView1.Controls.Add( chkbox );

            DataRow dr = default( DataRow );

            //Declare Column names
            dt.Columns.Add( "eno" );
            dt.Columns.Add( "empname" );
            dt.Columns.Add( "sal" );

            //Create rows with data
            dr = dt.NewRow( );
            dr["eno"] = 101;
            dr["empname"] = "test1";
            dr["sal"] = 9000;
            dt.Rows.Add( dr );

            dr = dt.NewRow( );
            dr["eno"] = 102;
            dr["empname"] = "test2";
            dr["sal"] = 15000;
            dt.Rows.Add( dr );

            dr = dt.NewRow( );
            dr["eno"] = 103;
            dr["empname"] = "test3";
            dr["sal"] = 20000;
            dt.Rows.Add( dr );

            //Bind that datatable data into the datagridview
            dataGridView1.DataSource = dt;
        }


C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           double k = 0.0;
           if ( e.ColumnIndex == 0 )
           {
               dataGridView1.CommitEdit( DataGridViewDataErrorContexts.Commit );
               for ( int i = 0 ; i <= dataGridView1.RowCount - 1 ; i++ )
               {
                   //chkcol is checkbox column
                   if ( Convert.ToBoolean( dataGridView1.Rows[i].Cells["chkcol"].Value ) == true )
                   {
                       k = k + Convert.ToDouble( dataGridView1.Rows[i].Cells[3].Value );      //3 is salary column
                   }
               }
               //This one is total display textbox
               //label1.Text = k.ToString( );
           }
       }
any assistance will be appreciated thanks.
Posted
Comments
Dilan Shaminda 10-Aug-14 0:37am    
what happens when you are unchecking the top level checkbox?
Uwakpeter 10-Aug-14 3:43am    
All the checkboxes in the datagrid will be unchecked, if I checked it, all will be checked as well, I want to be able to unchecked the checkbox at the datagrid header if after checking all, the user decides to unchecked one or more of the checkboxes. Thanks

1 solution

Check this Article.I thinks this is what you are looking for

Toggling the States of all CheckBoxes Inside a DataGridView Column
 
Share this answer
 
v3
Comments
Uwakpeter 10-Aug-14 10:30am    
Yes that is what I am looking for, but I am working on a windows application, and I don't think javascribt will work on windows application, I have not tried it before, if there is a way I can achieve this in windows apllication, I will really appreciate, thanks.
Dilan Shaminda 10-Aug-14 10:40am    
sorry..mistaken..I have updated the link :-)
Uwakpeter 11-Aug-14 4:47am    
Thanks, i have tried implementing what i got from that link, the following are being underlined as errors:

IsHeaderCheckBoxClicked
TotalCheckedCheckBoxes
TotalCheckBoxes
ResetHeaderCheckBoxLocation, and i tried to fixed it but couldn't, i will be glad if you could further assist. Thanks
Dilan Shaminda 11-Aug-14 4:58am    
Those are defined as global variables
int TotalCheckBoxes = 0;
int TotalCheckedCheckBoxes = 0;
CheckBox HeaderCheckBox = null;
bool IsHeaderCheckBoxClicked = false;

and ResetHeaderCheckBoxLocation is a private method. Check the source code given :-)
Uwakpeter 11-Aug-14 5:32am    
I can only find ResetLocation, can i rename ResetHeaderCheckBoxLocation to ResetLocation? also the GetDataSource() method is not include in this example, though i would write mine later on, i would have love to test it with the one that came with this tutorial, Is there anyway i can get that too? Thanks

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