0
Reply

How to read datagridview combobox value and display member?

Ankit Agarwal

Ankit Agarwal

Oct 16 2013 8:43 AM
4.1k
Hello,

How to read datagridview combobox value and display member on cellvalue changed event?

I am using this code for display combobox value and cell value changed event but it's not running properly.

DisplayMember and ValueMember displayed prpperly but when we select combobox then display in id in combobox it is a wrong.

I want to change another value when we choose combobox item.

Code:-

combobox fill code:-

private void loadUUSLaminates()
        {
            #region loadUUSLaminates
            try
            {
                SqlConnection con = new SqlConnection(cnString1);//connection to the your database
                SqlCommand cmd = new SqlCommand("fetchUUSLaminateName", con);//create a SQL command object by passing name of the stored procedure and database connection 
                cmd.CommandType = CommandType.StoredProcedure;//set command object command type to stored procedure type

                SqlDataAdapter adp = new SqlDataAdapter(cmd);//create SQL data adapter with command object
                DataTable dt = new DataTable();//create a data table to hold result of the command
                //DataGridViewComboBoxColumn dgvcmb = new DataGridViewComboBoxColumn();

                if (con.State == ConnectionState.Closed)//check whether connection to database is close or not
                    con.Open();//if connection is close then only open the connection
                adp.Fill(dt);//data table fill with data by calling the fill method of data adapter object
                if (dt.Rows.Count > 0)//check whether data table contain any row or not
                {
                    
                    LamiName.DataSource = dt;
                    LamiName.DisplayMember = "UUSLaminateName";
                    LamiName.ValueMember = "UUSLaminateID";
                    
                    //dgvUUSKreationPriceCalculator.DataSource = dt;
                    if (con.State == ConnectionState.Open)
                        con.Close();
                }
            }
            catch (Exception ex)//catch if any error occur
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);//display error message with exception 
            }
            #endregion
        }


private void dgvUUSKreationPriceCalculator_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int LaminatePercentage = 0;
            if (blnload && e.ColumnIndex == 3)
            {
                if (dgvUUSKreationPriceCalculator.Rows[e.RowIndex].Cells["LamiName"].Value != null)
                {
                    SqlConnection con = new SqlConnection(cnString1);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("ReadLaminateRate", con);//create a SQL command object by passing name of the stored procedure and database connection 
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@UUSLaminateID", SqlDbType.VarChar).Value = dgvUUSKreationPriceCalculator.Rows[e.RowIndex].Cells["LamiName"].Value;
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.Read())
                    {
                        if (sdr[0].ToString() == "")
                        {
                            MessageBox.Show("Laminate Rate not set for selected UUSLaminate ID in backend", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            dgvUUSKreationPriceCalculator.Rows[e.RowIndex].Cells["LamiName"].Selected = true;
                        }
                        else
                        {
                            dgvUUSKreationPriceCalculator.Rows[e.RowIndex].Cells["LamPercentage"].Value = sdr[0].ToString();
                            //dgvUUSKreationPriceCalculator.Rows[e.RowIndex].Cells["LamiName"].Value = sdr[1].ToString();
                        }
                    }
                    sdr.Close();
                    con.Close();
                }
            }
        }

Please help me.

Thanks in Advance.
Ankit Agarwal