0

我正在努力实现以下目标:

I have my first grid which shows the entries from my db table foo, when selecting one, I need to recover from column[0] cell[0], an id which I'll use for a query afterwards. 此查询将使用来自 的所有一对多实例填充 grid2 table bar。但我得到一个空点异常,我不知道为什么......

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 id=@index", 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();
        }
4

1 回答 1

0

仍然不是正确的方法,但它有效......

  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();
    }
于 2013-04-15T16:15:22.540 回答