Ankush

Ankush

  • NA
  • 267
  • 63k

Adding checkbox column in datagridview at specific position.

Mar 21 2016 11:53 AM
I am developing a windows application using c#.
I have a datagridview in which the data is coming from database based on the value selected in combobox on form, now I want to add checkbox column at first position ( Checkbox column should be first column )
  1. dgvDetails.DataSource = dt;  //dt is DataTable  
  2. DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();    
  3. chk.HeaderText = "";    
  4. chk.Width = 30;    
  5. chk.Name = "chk";    
  6. dgvDetails.Columns.Insert(0, chk);  
  7. dgvDetails.Columns[1].DataPropertyName = "Trans_ID";  
  8. dgvDetails.Columns[1].HeaderText = "Trans ID";  
  9. dgvDetails.Columns[2].DataPropertyName = "UserName";  
  10. dgvDetails.Columns[2].HeaderText = "User Name";   
I wrote the following code :-
The number of rows depend upon the value selected in combobox.
This doesn't work properly. When the form is loaded and the value in combobox is selected the checkbox column appears as first column and the data also appears correctly, but when the combobox selected value is changed, the data in datagirdview is jumbled and error occurs

Answers (4)