sree
sree

Reputation: 601

how to select a value from the particular cell which is a combobox column in the datagridview

i have a datagrid with 2 combobox columns first column is categories column and the second column is Subcategories column. now i have to select the category in the first combobox column then the sub categories which comes under this selected category should be binded in the second combobox column i wrote selction changed event for the combobox column as follows.

private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{ 

  ComboBox cb = e.Control as ComboBox;

                if (cb!=null)

                { cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);


                    // now attach the event handler

                    cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);

                }

}


void cb_SelectedIndexChanged(object sender, EventArgs e)

     {
       int i = datagrdADDTEMP.CurrentCell.ColumnIndex;
       int j = datagrdADDTEMP.CurrentCell.RowIndex;
       if(i==0)
       {

       var tb = datagrdADDTEMP.EditingControl as ComboBox;

       if (tb != null)

       str = tb.SelectedValue != null ? tb.SelectedValue.ToString() : null;

       Assesment_Business_layer.Businesslayer bl = new Assesment_Business_layer.Businesslayer();

       DataSet ds = new DataSet();**strong text**

       ds = bl.GetSubCatNamesBA(str);

       cmbDataGridSubCategory.DataSource = ds.Tables[0];

       cmbDataGridSubCategory.DisplayMember = "SubCategoryName";

       cmbDataGridSubCategory.ValueMember = "SubCategoryCode";

       }
       }

     }

its working well. when im selecting the category from the first combobox column in the particular row then related subcategories of this selected category are binding to the subcategory column in same row. its good,but the problem is, the second combobox columns which are in the previous to the particular row are also binding with the same subcategories

for example

if im selecting the category from the third row first combobox column then its subcategories are binding in the 1st row 2nd row and third row aswell.i jus want to bind it only to the third row.

please any one help me, im struk up with this problem..

Upvotes: 1

Views: 1283

Answers (1)

Tergiver
Tergiver

Reputation: 14507

You're changing the DataSource for the entire column. Instead, change the DataSource only for the cell. DataGridViewComboBoxCell contains its own DataSource member, which if null uses its OwningColumn's DataSource.

Updated

Instead of

cmbDataGridSubCategory.DataSource = ds.Tables[0];
cmbDataGridSubCategory.DisplayMember = "SubCategoryName";
cmbDataGridSubCategory.ValueMember = "SubCategoryCode";

Use

// Retrieve the individual sub-category cell
DataGridViewComboBoxCell subComboCell = (DataGridViewComboBoxCell)datagrdADDTEMP.Rows[j].Cells[cmbDataGridSubCategory.Index];
// Alter its DataSource
subComboCell.DataSource = ds.Tables[0];
subComboCell.DisplayMember = "SubCategoryName";
subComboCell.ValueMember = "SubCategoryCode";

The DGV is a grid of cells (derived from DataGridViewCell). Each of these cells has some specific type based on the DataGridViewColumn type for that column. So if you have a DataGridViewComboBoxColumn, all of the cells in that column are DataGridViewComboBoxCells.

A combo box cell has a DataSource member that tells it how to populate the drop down (you can also populate the drop down through the Items member, but that's not relevant to your problem). When the DGV populates the drop down, it looks first at the cell's DataSource member. If it's null (which it is by default), it then looks to the OwningColumn (which for a DataGridViewComboBoxCell is always a DataGridViewComboBoxColumn) for its DataSource member.

That's what you were changing, the column's DataSource member. So all of the cells in that column are going to change to this new DataSource. What you want is to change just that specific cell's DataSource.

Upvotes: 1

Related Questions