Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Populating combo box present in Datagridview
 

Populating combo box present in Datagridview

Hi All,
I want to populate a combo box present ina DataGridView with some values read from a file.My requirement is such that as soon as a new row is added to DGV the combo box should populate accordingly.
Although, I am able to get the contents that I want to populate the Combo box with but I dont know how to populate the Combox box present in DGV.One thing to be noted is that every combo box in the DGV is to be populated with different values(which I am able to fetch but I am not able to store them in the Combo Box of each row).


Ashish Agrawal
  • Moved byeryangMSFTMonday, September 28, 2009 7:56 AM (From:.NET Base Class Library)
  •  
NewBie_in_CSharp  Saturday, September 26, 2009 2:11 PM
Khanna Gaurav  Sunday, September 27, 2009 5:56 PM
HI Gaurav,
Thanks for the reply but I don't want to bind the DGV with a database instead I want to to Populate Combo Box with some values.I want that as soon as a new row is added to DGV the combo box should get populated with some values (that I already have).
Ashish Agrawal
NewBie_in_CSharp  Sunday, September 27, 2009 6:07 PM
Then I think you will have to use RowAdded event of datagridview

 Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
        Dim dgCombo As DataGridViewComboBoxCell = DataGridView1.Rows(e.RowIndex).Cells("Combo Column Index")
        dgCombo.Items.Add("")
        ''OR 
        dgCombo.DataSource = "Your Collection"
    End Sub


If above code doesn't work then post what you have done upto now.
Gaurav Khanna
Khanna Gaurav  Sunday, September 27, 2009 7:09 PM
Hi All, I'll try to explain my problem once again.I am using C# to create a tool. I have created a DataGridViewComboBoxColumn cmbExistingProducts=new DataGridViewComboBoxColumn (); cmbExistingProducts.HeaderText = "ExistingProducts"; Then there is a button in the Form on whose click I write dgv.Rows.Add(new Object[] { });//this creates a new blank row in the DataGridView.Then for each row in the dgv I fill the cells with some values that I am reading from a word file. All the cells are text boxes except one which is a Combo Box. My problem is how to populate the combo box with the values that I have read from the word file. Right now I am doing : cmbExistingProducts.Items.Clear(); cmbExistingProducts.Items.Add("A"); cmbExistingProducts.Items.Add("B"); in a method which does this for each row in the dgv and here lies the actual problem. All the combo boxes get populated with these values only and the values keep adding to the existing Combo Box.Items Collection.And I want them to populate with th e different value that I am reading from the word file. Please guide me in this respect.
Ashish Agrawal
NewBie_in_CSharp  Monday, September 28, 2009 10:16 AM

You can use google to search for other answers

Custom Search

More Threads

• Thread restart problem
• Turn off intermittent DataSet updates to DataGridViews
• DataBindings of Winform using ADO.Net
• The given datarow is not in the current DataRowCollection.
• Yes/No checkbox in a datagridview - how to enforce only one row permitted as Yes
• DataViewManager - Getting Sort Value
• a simple question about datagridview
• Newbie confused... why cant I update or delete?
• Polymorphic data bound to a DatagridView
• How to display specified no.of rows in a datagridview in windows application