Question Remove check box column from datagridview when clear datagridview

archanapalani

New member
Joined
Aug 27, 2014
Messages
2
Programming Experience
1-3
i have created datagridview dynamically with check box column,once i refresh my datagrid its removes all data except checkbox column,below code for adding checkbox column in datagrid

C#:
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
        dataGridView1.Columns.Add(chk);
        chk.HeaderText = "Check Data";
        chk.Name = "chk";
         int count = dataGridView1.Rows.Count;
         for (int i = 0; i < count; i++)
         {
             dataGridView1.Rows[i].Cells[12].Value = false; [COLOR=#111111][FONT=Segoe UI]
[/FONT][/COLOR]


and below code for clearing my datagrid,if i try to add new data in datagridview after refresh still old check box exists and it keeps adding one more checkbox column.
C#:
 dataGridView1.Rows.Clear();
 dataGridView1.Refresh();


any suggestion???
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
It's only going to keep adding columns if you keep executing the code to add them. Just add the column once at the beginning and then don't execute that code again. Simple.
 

archanapalani

New member
Joined
Aug 27, 2014
Messages
2
Programming Experience
1-3
i tried as follows

dataGridView1.Columns.RemoveAt(12);
i mentioned the index of checkbox and its worked.

Thanks All

exactly Mr jmcilhinney :p , but i have to execute again and again with new inputs :-( this is the requirement :) anyway i have solved this :)
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
exactly Mr jmcilhinney :p , but i have to execute again and again with new inputs

No, you don't. You may have to execute the code to get and display data again and again but that doesn;t mean that you have to execute the code to add the check box column again and again. They are not the same thing. Your "solution" is a hack and should not be used. Don't pointlessly add and remove a column over and over. Add it once and just leave it. In fact, don't even add it in code. You should be adding it in the designer.
 
Top Bottom