【问题标题】:how to show popup by clicking on datagridview column in window application using c#?如何通过使用 c# 在窗口应用程序中单击 datagridview 列来显示弹出窗口?
【发布时间】:2011-08-30 04:22:30
【问题描述】:

嗨 我在我的窗口应用程序表单中使用 datagridview。我希望当我单击 datagridview 的特定列时,它会弹出一个窗口,其中包含包含该字段的信息。就像我在 datagridveiw 中拥有有关 stds 的所有数据一样,但不是显示所有我只想显示他/她的名字,其余信息应该通过单击该特定名称来显示。

问候 图瑟夫

【问题讨论】:

    标签: c# winforms visual-studio datagrid datagridview


    【解决方案1】:

    你可以试试:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            String info;
    
            if (e.ColumnIndex == 0) // Here specify the column index on click of which you want to display popup
            {
                //your logic here
                info= dataGridView1.Rows[e.RowIndex].Cells["U_ID"].Value).toString();  // Cells["<specify your cell name for this index>"]
                MessageBox.Show(info);
            }
    
            else if (e.ColumnIndex == 1) // Here specify the column index on click of which you want to display popup
            {
                //your logic here
                info= dataGridView1.Rows[e.RowIndex].Cells["Name"].Value).toString();  // Cells["<specify your cell name for this index>"]
                MessageBox.Show(info);
            }
        }
    

    MSDN

    【讨论】:

      【解决方案2】:

      试试这个

      private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
              {
                  try
                  {
                      Form1 frm = new Form1(DataGridView1.CurrentRow.Cells["ID"].Value.ToString())); 
                      frm .ShowDialog();
      
                  }
                  catch (Exception ex)
                  {
                  }
      
              }
      

      在此代码中,您要创建另一个表单的对象,您希望将其显示为弹出窗口。 //Form1 frm = new Form1.

      并将 ID 值作为构造函数传递给 Form1 并显示为对话框 //DataGridView1.CurrentRow.Cells["ID"].Value

      还将 Form1 ShowInTaskbar 属性设置为 False

      注意:

      您可以从 Form1 访问该构造函数值(ID)并通过 ID 获取所有详细信息并按您的意愿显示

      【讨论】:

      • private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { try { Viewaddeduser frm = new Viewaddeduser(DataGridView.CurrentRow.Cells["FileNo"].Value.ToString()); frm .ShowDialog(); } catch (Exception ex) { MessageBox.Show("Ok" + ex); } }
      • 并在查看添加的用户表单上私有字符串 FileNo; public Viewaddeduser(String FileNo) { InitializeComponent(); } 是正确的方式吗?
      • 表单加载时我正在与 db 建立连接并进行查询.. 以获取基于 fileNo 的数据
      • 异常是找不到列名FileNo ...可能是什么原因?
      • 试试这个 public Viewaddeduser(String FileNo) { InitializeComponent(); this.FileNos=文件编号 }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      相关资源
      最近更新 更多