Lennie Kuah

Lennie Kuah

  • NA
  • 27
  • 0

C#NET2008 Windown Application Form with DataGridView ComboBox

Sep 15 2011 2:43 AM
Hullo Friends,
I do need your help. Please help me.
I do encounter another interesting error of having Combobox on C#NET2008 Window Application Form DataGridView column.

The datagridview contains 2 columns, "OrderID", "ProductID".
On the ProductID Column, I am very surprised that it does not display the data of ProductID initially when display. This column is the  DataGridViewComboBoxColumn.  When click on the ComboxBox to select the ProductName expecting the ProductID to display on the column cells, the selected combobox item display the ProductName.

Here are the Overall Coding

 private void FLoadDataGridView()
{
     string strSql = " Select OrderID, ProductID, "
                      + " From tblOrders Where (CustomerID = N'" + strCustID + "')"
                      + " ORDER BY OrderID ProductID";

        sqlconn = new SqlConnection(connstr);
        sqlconn.Open();
        DA = new SqlDataAdapter(strSql, sqlconn);
        DS = new System.Data.DataSet();
        DS.Clear();
        DA.Fill(DS,"Orders");

        // --- set DataGridView DataSource ---
        this.dataGridView1.AutoGenerateColumns = false;
        this.dataGridView1.AutoSize = false;
        this.dataGridView1.DataSource = DS.Tables["Orders"];

        // ---Format DataGridView Column ---
        FFormatDataGridViewColumn();
}

private void FFormatDataGridViewColumn()
{ //format datagridview Column - combobox column

        // --- create Product combobox Data ---
       string strSqlProd = " Select ProductID, ProductName "
                              + " From testProducts ORDER BY ProductName";

        sqlconn = new SqlConnection(connstr);
        sqlconn.Open();
        cboDA = new SqlDataAdapter(strSqlProd, sqlconn);
        cboDS = new DataSet("cboDS");
        cboDS.Clear();
        cboDA.Fill(cboDS, "Prod");

        //--- format column Header ---
        this.dataGridView1.ColumnHeadersDefaultCellStyle.Font =           new System.Drawing.Font (dataGridView1.Font,  FontStyle.Bold);

        //--- OrderID Column ---
        DataGridViewColumn colOrderID = new DataGridViewTextBoxColumn();
        colOrderID.DataPropertyName = "OrderID";
        colOrderID.HeaderText = "Order ID ";
        colOrderID.Width = 90;
        dataGridView1.Columns.Add(colOrderID);

        // --- ProductID Column with combobox --- 
        DataGridViewComboBoxColumn colCboProd = new DataGridViewComboBoxColumn();
        colCboProd.DataPropertyName = "ProductID";
        colCboProd.HeaderText = "Product ID";
        colCboProd.Width = 140;


        // Bind colCboProd to Product table - Combobox
        colCboProd.DataSource = cboDS.Tables["Prod"];
        colCboProd.ValueMember = "ProductID";
        colCboProd.DisplayMember = "ProductName";
        dataGridView1.Columns.Add(colCboProd);

}

Thank you for helping me. Appreciate you very much.
I am very glad to meet you here a Wonderful Software Forum.

HAVE GOOD DAY.
Cheers,
TeeLeong


Answers (1)