选择的项目显示GRID2

问题描述:

从tabele2及其相关项目我试图实现以下目标:选择的项目显示GRID2

我有我的第一选择网格一个时表示从我的分贝table foo,条目,我需要恢复从列[0] cell [0],我将用于后续查询的ID。该查询将使用来自table bar的所有一对多实例填充grid2。但我得到的空点例外,我想不通为什么...

private void button1_Click_1(object sender, EventArgs e) 
     { 
      SqlConnection sc = new SqlConnection(@"Data Source = WARZARU-NB\SQLEXPRESS; Database = proj_1; Integrated Security = True"); 

      dataSet.Clear(); 

      dataAdapter.SelectCommand = new SqlCommand("select * from students WHERE [email protected]", sc); 
     // EXCEPTION here 
      dataAdapter.InsertCommand.Parameters.Add("@index", SqlDbType.Int).Value = dgParent.CurrentCell.Value; 
      dataAdapter.Fill(dataSet, "grades"); 

      dgChild.DataSource = dataSet.Tables["grades"]; 

      dgParent.AutoResizeColumns(); 
      dgParent.AutoResizeRows(); 
     } 
+0

好了,'dgParent.CurrentCell '返回表示当前单元格的DataGridViewCell,如果没有当前单元格,则返回null。默认值是第一列中的第一个单元格,如果控件中没有单元格,则为null。当您调用CurrentCell时,您确定dgParent中至少有一行/单元格吗? – 2013-04-11 14:47:50

+0

嗯,他们打印,因为我可以看到他们在桌子上,我想他们是... :( – 2013-04-11 14:51:26

+0

嗯,尝试也许'dgParent.Rows [0] .Cells [0] .Value'?虽然我认为你会得到相同的例外 – 2013-04-11 14:53:48

还没做正确的方式,但它的作品...

private void dgParent_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      SqlConnection sc = new SqlConnection(@"Data Source = WARZARU-NB\SQLEXPRESS; Database = proj_1; Integrated Security = True"); 
     if (dgParent.SelectedCells.Count > 0) 
     { 
      dgChild.DataSource = null; 
      int selectedrowindex = dgParent.SelectedCells[0].RowIndex; 
      DataGridViewRow selectedRow = dgParent.Rows[selectedrowindex]; 
      string a = Convert.ToString(selectedRow.Cells["id"].Value); 
      MessageBox.Show(a); 

      SqlCommand updateCommand1 = new SqlCommand("Select * FROM tableChild WHERE studentID = @id", sc); 
      updateCommand1.Parameters.Add(new SqlParameter("id", a)); 
      dataAdapter = new SqlDataAdapter(updateCommand1); 

      dataSetChild.Clear(); 
      dataAdapter.Fill(dataSetChild, tableChild); 
      dgChild.DataSource = dataSetChild.Tables[tableChild]; 
     } 

     dgParent.AutoResizeColumns(); 
     dgParent.AutoResizeRows(); 
    }