How to populate Combox box in Datagrid view.

I have a datagrid view show address details. It is also a input datagrid.

When a new record is create, the datagrid view is empty. It contains a state combo box.

I have set the state combo box column datasource to a dataset control called "dsState"

In form Load.

I do the below, however, the state combox box is still empty.

DataTable dt = new DataTable("state");

//DataRow dr;

dt.Columns.Add("state");

dt.Rows.Add("NSW");

dt.Rows.Add("ACT");

dsState.Tables.Add(dt);

The other questions that I have is when there is record in database, this datagridview will get the data from database, how do I set the combox box selectedindex to the new returned dataset




Answer this question

How to populate Combox box in Datagrid view.

  • WhPonds

    Let me explain it in details.

    I have a datagridview(dgv) control, which has mulitpule addresses. One record has multipule address rows.

    ie.

    1 abc street, Sydney, NSW, 2000, Australia

    2 xyz street, Sydney, NSW, 2000, Australia

    When a record is new, the dgv is empty, it contain the state combo box.(Question1: how to populate)

    After the record is save and close, the next time, when the record is retrieve, the dgv should show the correct information, i.e., the combo box should selected the "NSW" state correctly.(Question 2)

    For the dgv, I always use typed dataset, so that I can customize the column width ...etc in the design time. This is why I create a dataset control(dsState).  This dsState is linked to an xsd dataset and the xsd is linked to a store procedure. However, this time, there is combo box involved and I don't know how to do with it.

    Also, as I have multipule combox boxes, the way you explain will not fix to my situation.

    Thanks!

     

     

     

     



  • gary.ruan

    As to your first question, try setting the DataSource property to your DataTable, the DisplayMember and ValueMember properties to the name of the column in your table. For example:

    cbo.DataSource = dt

    cbo.DisplayMember = "state"

    cbo.ValueMember = "state"

    Also look at: http://msdn2.microsoft.com/en-us/library/ms233685.aspx

    Sorry, but I didn't understand your second question...



  • EricJ

    I have found the solution from the web.

    Here is the link.

    http://msdn.microsoft.com/msdnmag/issues/05/04/CuttingEdge/



  • How to populate Combox box in Datagrid view.