简体   繁体   English

当我在 C# 中有多个 datagridviews 时,如何获取当前选定的 datagridview 的名称?

[英]How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#?

I have 5 datagridviews and I want to get details from the selected datagridview.我有 5 个数据网格视图,我想从选定的数据网格视图中获取详细信息。
Like when selecting the gridview row, I want to take that row details with the gridview name.就像选择 gridview 行时一样,我想使用 gridview 名称获取该行的详细信息。

private void assigncontrctbtn_Click(object sender, EventArgs e)
{

        SqlConnection conn;
        conn = ConnectionManager.GetConnection();
        SqlCommand newCmd = conn.CreateCommand();
        newCmd.Connection = conn;
        newCmd.CommandType = CommandType.Text;
        conn.Open();
        string hh = "SELECT contractstatus from dbo.contracts where "here i need gridview name";
        SqlCommand cmd = new SqlCommand(hh, conn);
        string dd = Convert.ToString(cmd.ExecuteScalar());

        if ((dd == "Contract logged") && (cmbCategory.Text != "Calculate Contract") && (cmbCategory.Text == "Verify Contract"))
        {
            MessageBox.Show("no");
        }
        else if ((dd == "Calculate Contract") && (cmbCategory.Text != "Verify Contract") && (cmbCategory.Text == "Calculate Contract"))
        {
            MessageBox.Show("verify");
        }

        else
        {
            if (dba.logcontract(cntrctnmecmb.Text, Convert.ToInt32(prcecodecmb.Text), seasoncmb.Text, cmplexitycmb.Text, revisecmb.Text, logdatetime, condatefrm.Value.Date.ToString("MM/dd/yyyy"), condteto.Value.Date.ToString("MM/dd/yyyy"), vatcmb.Text, statuscmb.Text, lstcmnt.Text, apprvedbycmb.Text, cmbCategory.Text))
            {
                MessageBox.Show("Successfully Added");
                FillGridCal();
                FillGridVer();
            }
            else
            {
                MessageBox.Show("Error occured");
            }
        }
    }

You can have an an event say CellClick for each of your DataGridView.您可以为每个 DataGridView 设置一个事件,例如CellClick When a row is clicked, you can get the details that you want.单击一行时,您可以获得所需的详细信息。 I'm assuming that what you mean by take that row details is take the cell values for the selected row .我假设您所说的take that row details意思是take the cell values for the selected row

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        string firstColumn = (string)dataGridView1.Rows[e.RowIndex].Cells[firstColumn.Name].Value;//I'm getting 1 column from the selected row, you can do a loop if you want to get all cells
        string dataGridView = dgv.Name;//This is the DataGridView name
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在第三个DataGridView中选择时,如何在2个DataGridViews中选择一行? - How can I select a row in 2 DataGridViews when selected in a third DataGridView? 如何从 c# 中的 datagridview 通过列名从选定行中获取数据 - how do I get the data from selected row by its column name from datagridview in c# 如何在C#中获取当前(WLAN)网络的名称? - How can I get the name of the current (WLAN) network in C#? 如何在C#Windows应用程序中将选中的列表框的选定项添加到datagridview中的特定列? - how can i get the selected items of a checked listbox to a particular column in datagridview in c# windows application? 我怎样才能从datagridview中的选定行获得整个对象而不显示对象ID -c# - how i can get the whole objected from the selected row in datagridview without displaying the object ID -c# 如何在C#中获取当前方法的名称及其参数(名称和值) - How Can I Get Current Method's Name And Its Arguments(name and value) in C# 在C#中使用ReadBlock方法时,如何知道何时到达文件末尾? - How can I tell when I've reached the end of the file when using the ReadBlock method in C#? 如何通过 C# 获取我刚刚插入到文档 (MS Word) 中的图片的名称和路径? - How to get the name and the path of the picture I've just inserted to document (MS Word) by C#? 如何在C#中打印DataGridView? - How can i print DataGridView in C# ? 如何获取 C# 中当前可执行文件的名称? - How do I get the name of the current executable in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM