Reputation: 5
I have two tables in my database
datatable have two columns Product, Producttype. kar table also have two columns type, tax.
In my form I have a Datagridview with 3 columns Productname, type, tax.
Productname column is datagridviewcomboboxcolumn which display all the product from datatable. `
public Form5()
{
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
this.datatableTableAdapter.Fill(this.myBillDataSet.datatable);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
string item = null;
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=ZEE-PC\SQLEXPRESS;Initial Catalog=MyBill;Integrated Security=True");
ComboBox cb = (ComboBox)sender;
item = cb.Text;
if (item != null)
{
SqlCommand cmd = new SqlCommand("select Producttype from datatable where product=@pro", con);
cmd.Parameters.AddWithValue("@pro", item);
con.Open();
string val = cmd.ExecuteScalar().ToString();
con.Close();
SqlCommand cmd2 = new SqlCommand("select tax from kar where type=@pro2", con);
cmd2.Parameters.AddWithValue("@pro2", val);
con.Open();
string val2 = cmd2.ExecuteScalar().ToString();
con.Close();
}
}
when user select any product from dropdown the type and tax column will display the value. I am getting the values but not able to display in gridview cell
Upvotes: 0
Views: 802
Reputation: 16
You can directly insert value in your cells inside ComboBox_SelectedIndexChanged event using column index
dataGridView1.Rows[e.RowIndex].Cells[YourCellindex] = val;
dataGridView1.Rows[e.RowIndex].Cells[YourSecondCellindex] = val2;
dataGridView1.BeginEdit(true);
dataGridView1.EndEdit();
Hope It helps
Upvotes: 0