Hello all,

I have a windows application with a DataGridView. I have successfully bound the DataGridView to a datasource (datatable based on database query). I would like one of the columns to be a ComboBox that is selected to the correct datasource value, yet upon click displays data from a separate dataset. Is that clear?

Recommended Answers

All 3 Replies

This first bit retrieves two tables:
table 0 contains rows of data without the customer number field.
table 1 contains the same rows of data, with just the row id and the customer number.

connPT = new SqlConnection();
connPT.ConnectionString = PTConnection;
connPT.Open();
SqlCommand cmdPT = new SqlCommand();
cmdPT.Connection = connPT;
cmdPT.CommandType = CommandType.StoredProcedure;
cmdPT.CommandText = "GetReceivingData_ToProcess";
cmdPT.Parameters.Add("@BeginDate", SqlDbType.DateTime);
cmdPT.Parameters["@BeginDate"].Value = fBeginDate;
cmdPT.Parameters.Add("@EndDate", SqlDbType.DateTime);
cmdPT.Parameters["@EndDate"].Value = fEndDate;
cmdPT.Parameters.Add("@Division", SqlDbType.VarChar);
cmdPT.Parameters["@Division"].Value = strDivision.ToString().Trim();
cmdPT.CommandTimeout = 0;
pda = new SqlDataAdapter(cmdPT);
pds = new DataSet();
pda.Fill(pds);
connPT.Close();
connPT.Dispose();

Here I successfully fill the combobox with a new set of data, which is all the
possible customer numbers from which the user can choose.

if (pds.Tables[0].Rows.Count > 0)
{
    // set dataGridView1 datasource.
    dataGridView1.DataSource = pds.Tables[0];
    // create datasource for combobox dropdown 
    SqlConnection connC = new SqlConnection();
    connC.ConnectionString = ConfigurationSettings.AppSettings["GPConnection"].ToString();
    connC.Open();
    SqlCommand cmdC = new SqlCommand();
    cmdC.Connection = connC;
    cmdC.CommandType = CommandType.Text;
    cmdC.CommandText = "SELECT DISTINCT CUSTNMBR AS [Customer Number] FROM RM00101 ORDER BY CUSTNMBR";               
    DataSet dsC = new DataSet();
    SqlDataAdapter daC = new SqlDataAdapter(cmdC);
    daC.Fill(dsC);
    connC.Close();
    connC.Dispose();            
    
    // create the combobox
    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    col.HeaderText = "Customer Number";
    col.Name = "Customer Number";
    col.DataSource = dsC.Tables[0];
    col.DisplayMember = dsC.Tables[0].Columns[0].ToString();
    dataGridView1.Columns.Add(col);
}

However, what I want to do in addition is to show a value in the combobox corresponding to the pds.Tables[1].Row.

>what I want to do in addition is to show a value in the combobox corresponding to the pds.Tables[1].Row.

Do not bind "ComboBox". Add ListItems via Add method.

Thanks, but the problem was not the items in the combobox, I was attempting to display a selected item. I have found elsewhere that the DataGridViewComboBox does not allow this.

>what I want to do in addition is to show a value in the combobox corresponding to the pds.Tables[1].Row.

Do not bind "ComboBox". Add ListItems via Add method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.