【问题标题】:How to get Selected Row Contents from DataGridView in C#?如何从 C# 中的 DataGridView 获取选定的行内容?
【发布时间】:2015-01-09 04:39:08
【问题描述】:

如何在 C# 中从 DataGridView 中获取选定的行内容

我有一个 9 列的 DataGridView。我从一个有 20 列的 Stdn_Registration_tbl(表)中填充这些列(我的意思是我只为 GridView 选择了特定的列)。 现在我想当有人在选定行上单击鼠标右键时,它会弹出一个 ContextMenuStrip。

if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuStrip1.Show(searchStdn_dtGv, e.Location);
            }

点击查看(上下文菜单项)后,它会以另一种形式(form2)显示来自 Stdn_Registration_tbl 的所有数据。

【问题讨论】:

    标签: c# gridview datagridview


    【解决方案1】:

    将 DataGridViewRow 从 Form1 传递到 Form2:

    //In Form1
    DataGridViewRow r = dataGridView1.SelectedRows[0];
    Form2 f = new Form2();
    f.row = r;
    f.ShowDialog();
    
    //In Form2
    public DataGridViewRow row;
    
    private void Form2_Load(object sender, EventArgs e)
    {
      label1.text = row.cell[0].ToString();
      .
      .
      .
      label9.text = row.cell[8].ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2011-04-14
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多