how to fill datagridviewcomboboxcolumn based on another datagridviewcomboboxcolumn
  • hi for fill datagridviewcomboboxcolumn based on another datagridviewcomboboxcolumn row wise we can use CellEndEdit Event there is some other event but when i work with them i did not get proper result for this task so i use this event my code is below.Category and Product is name of my DataGridViewComboBoxColumn.
    image




    private void Form1_Load(object sender, EventArgs e)
    {
    this.Category.DataPropertyName = "CategoryId";
    this.Product.DataPropertyName = "ProductCode";

    SqlConnection objCon = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest3;Integrated Security=True");

    SqlCommand objCmd = new SqlCommand("select CategoryId,CategoryName from tblCategory", objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable();

    objDA.Fill(dt);
    Category.DataSource = dt;

    //this column1 will display as text
    Category.DisplayMember = "CategoryName";

    //this column2 will use as back end value who can you use in selectedValue property
    Category.ValueMember = "CategoryId";

    }



    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
    if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["Category"])
    {
    string CategoryValue = "";

    if (dataGridView1.CurrentCell.Value != null)
    {
    CategoryValue = dataGridView1.CurrentCell.Value.ToString();
    }
    SqlConnection objCon = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest3;Integrated Security=True");

    SqlCommand objCmd = new SqlCommand("select ProductCode,ProductName from tblProduct where CategoryId=" + CategoryValue, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable();

    objDA.Fill(dt);

    DataGridViewComboBoxCell t = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] as DataGridViewComboBoxCell;
    t.DataSource = dt;
    t.DisplayMember = "ProductName";
    t.ValueMember = "ProductCode";
    }
    }


    1.jpg
    621 x 413 - 33K
  • 2 Comments sorted by
  • you can do it on combo box selected index change event instead of Cell end edit event
    usually user want to do like as I told above. with out edit mode
  • Thank you Waqas you made my day ;)
    hussainroyal

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Top Posters Weekly

Optimum Creative