Friday, 12 April 2013

How to add ComboBox dynamically to a Gridview on button click event?




Below i wrote a code by which you can add a combobox to a grid view on button click event.



 private void BTN_ADD_Click(object sender, EventArgs e)
{  int j=0;


          DataGridViewComboBoxCell ColumnItem2 = new DataGridViewComboBoxCell();  // create a combobox cell
                ColumnItem2.ValueMember = "Display";
                ColumnItem2.DisplayMember = "Display";

              for(int i=0;i<10;i++)// it will add elements to a combox
               {
                   ColumnItem2.Items.Add(i);
                   }

          dataGridView1.Rows.Add();  // add row everytime to add a new combobox to the next row.


             int columncount = dataGridView1.ColumnCount;// count no of coloumns
             int rowcount = dataGridView1.RowCount;;// count no of rows

//i kept count no of columns and rows so that by rows and columns count u can specify to which row or  you want to add combobox inside gridview
// below i hard coded the combobox row and column to 2,2 so it will add a combobox to 2nd row and 2nd column.
           

               dataGridView1[2, 2] = ColumnItem2;
             //dataGridView1[2, j] = ColumnItem2;   // general way of adding combox to second column and row is everytime added by one as i m incrementing j value every time to each time on button click it will add combox on next row on second column..same way you can specify row and column as per your need.

j++;

}

hope this will help....

0 comments:

Post a Comment