如何在选择更改时获取 DataGridView 中的特定单元格

发布于 2024-07-10 19:56:09 字数 429 浏览 3 评论 0原文

对于列表框,我有以下代码来提取所选的项目:

    private void inventoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        String s = inventoryList.SelectedItem.ToString();
        s = s.Substring(0, s.IndexOf(':'));
        bookDetailTable.Rows.Clear();
        ...
        more code
        ...
    }

我想对 DataGridView 执行类似的操作,即当选择更改时,检索所选行中第一个单元格的内容。 问题是,我不知道如何访问该数据元素。

任何帮助是极大的赞赏。

With a listbox, I have the following code to extract the item selected:

    private void inventoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        String s = inventoryList.SelectedItem.ToString();
        s = s.Substring(0, s.IndexOf(':'));
        bookDetailTable.Rows.Clear();
        ...
        more code
        ...
    }

I want to do something similar for a DataGridView, that is, when the selection changes, retrieve the contents of the first cell in the row selected. The problem is, I don't know how to access that data element.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-07-17 19:56:09

我认为这就是您正在寻找的。 但如果没有,希望它能给你一个开始。

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    DataGridView dgv = (DataGridView)sender;

    //User selected WHOLE ROW (by clicking in the margin)
    if (dgv.SelectedRows.Count> 0)
       MessageBox.Show(dgv.SelectedRows[0].Cells[0].Value.ToString());

    //User selected a cell (show the first cell in the row)
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString());

    //User selected a cell, show that cell
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.SelectedCells[0].Value.ToString());
}

I think that this is what you're looking for. But if not, hopefully it will give you a start.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    DataGridView dgv = (DataGridView)sender;

    //User selected WHOLE ROW (by clicking in the margin)
    if (dgv.SelectedRows.Count> 0)
       MessageBox.Show(dgv.SelectedRows[0].Cells[0].Value.ToString());

    //User selected a cell (show the first cell in the row)
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString());

    //User selected a cell, show that cell
    if (dgv.SelectedCells.Count > 0)
        MessageBox.Show(dgv.SelectedCells[0].Value.ToString());
}
您的好友蓝忘机已上羡 2024-07-17 19:56:09

这是使用列名称解决此问题的另一种方法。

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   var senderGrid = (DataGridView)sender;
   if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
   {
      if (e.ColumnIndex == dataGridView1.Columns["ColumnName"].Index)
      {
        var row = senderGrid.CurrentRow.Cells;
        string ID = Convert.ToString(row["columnId"].Value); //This is to fetch the id or any other info
        MessageBox.Show("ColumnName selected");
      }
   }
}

如果您需要传递所选行中的数据,可以通过这种方式将其传递到其他表单。

Form2 form2 = new Form2(ID);
form2.Show();

This is another way of approaching to this question using the column name.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   var senderGrid = (DataGridView)sender;
   if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
   {
      if (e.ColumnIndex == dataGridView1.Columns["ColumnName"].Index)
      {
        var row = senderGrid.CurrentRow.Cells;
        string ID = Convert.ToString(row["columnId"].Value); //This is to fetch the id or any other info
        MessageBox.Show("ColumnName selected");
      }
   }
}

If you need to pass the data from that selected row you can pass it this way to the other form.

Form2 form2 = new Form2(ID);
form2.Show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文