Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to when i click on multiple rows in datagridview bind another table to another datagridview and i use cellclick property but it does not work:

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
  foreach (DataGridViewRow dr in dataGridView1.Rows)
  {
    if (dr.Cells[0].Value != null)
    {
 
       CustomerID1 = Convert.ToString(dr.Cells[1].Value);
 
      SqlConnection cnn = new SqlConnection();
      SqlCommand cmd = new SqlCommand();
 
      cnn.ConnectionString = "server=USER-VAIO\\SQLEXPRESS;database=Northwind;      integrated security=true";
      cnn.Open();
 
      cmd.Connection = cnn;
      cmd.CommandType = CommandType.Text;
      cmd.CommandText = "select OrderID,CustomerID,OrderDate from Orders where CustomerID=@CustomerID";
 
      cmd.Parameters.Add("@CustomerID", SqlDbType.NChar).Value = CustomerID1;
 
      SqlDataReader dr1 = cmd.ExecuteReader();
      DataTable dt1 = new DataTable();
      dt1.Load(dr1);
      dataGridView2.DataSource = dt1;
 

     cnn.Close();
   }
  }
}
Posted
Updated 4-Mar-15 4:43am
v2
Comments
Not clear. Please explain more with codes.
Member 11414302 4-Mar-15 8:40am    
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells[0].Value != null)
{

CustomerID1 = Convert.ToString(dr.Cells[1].Value);

SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();

cnn.ConnectionString = "server=USER-VAIO\\SQLEXPRESS;database=Northwind;integrated security=true";
cnn.Open();

cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select OrderID,CustomerID,OrderDate from Orders where CustomerID=@CustomerID";

cmd.Parameters.Add("@CustomerID", SqlDbType.NChar).Value = CustomerID1;

SqlDataReader dr1 = cmd.ExecuteReader();
DataTable dt1 = new DataTable();
dt1.Load(dr1);
dataGridView2.DataSource = dt1;


cnn.Close();
}
}
}
Please improve your question with these codes. Please don't comment here.
Sergey Alexandrovich Kryukov 4-Mar-15 9:14am    
I am curious how anyone could possibly click on multiple rows...
—SA
Yes Sergey. :D

1 solution

The events for getting multiple selections and cell-click are different types of events.

DataGridView.SelectionChanged Event

is for getting all SelectedRows, whereas

DataGridView.OnCellClick Method 

is triggered from a single cell, which means you will only get one row and column value.

If you are trying to link two DataGridView to 2 linked table in a database, it's easiest to create a DataSet with EntityFramework using Visual Studio designer.
After you build the project with your DataSet, the Forms Toolbox will have TableAdapters for binding to user controls like your two DataGridViews.

If you bind them correctly via the foreign key the data will automatically update.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900