【问题标题】:How to customize DataGridViewButton cell according to the another column value in a dataGridView如何根据dataGridView中的另一列值自定义DataGridView按钮单元格
【发布时间】:2019-02-17 03:51:19
【问题描述】:

我创建了一个 DataGridView,然后在编码中手动为其检索数据。另外,我添加了一个 DataGridViewButtonCell,它工作正常。但我想根据另一列(状态列索引为 4)值对其进行自定义。

例如。
- 如果状态列值为“新建”,则希望显示 form1
- 如果状态列值为“打开”,则希望显示 form2
- 如果状态列值为“已分配”,则希望显示 form3
- 如果状态列值为“重复”、“推迟”和“重复”想显示 form4

我该如何做这个过程?这是数据网格视图的图像

![1]: https:C:\Users\kularathna\Desktop\New.PNG

这是将数据检索到 dataGridView 的代码

private void TesterHome_Load(object sender, EventArgs e)
    {
        // Retriewe Data from database to the grid view
        SqlConnection con = new SqlConnection("Data Source= LAPTOP-J70EHC58 ; Initial Catalog= Defect_Management_system ; Integrated Security = True ; Connect Timeout = 30 ; ");
        SqlDataAdapter adapter = new SqlDataAdapter();
        string sqlstmt = SELECT * FROM defect WHERE tester_id = '"+ 5 +"' ;
        DataSet dataSet = new DataSet();

        try
        {
            con.Open();
            adapter = new SqlDataAdapter(sqlstmt, con);
            adapter.Fill(dataSet);
            con.Close();
            dgvTester.DataSource = dataSet.Tables[0];
        }
        catch (Exception error)
        {
            MessageBox.Show(error + "Invalid");
        }

        // Insert Data Grid View Button
        DataGridViewButtonColumn dgvbtnColumn = new DataGridViewButtonColumn();
        dgvTester.Columns.Add(dgvbtnColumn);
        dgvbtnColumn.HeaderText = " ";
        dgvbtnColumn.Text = "View";
        dgvbtnColumn.Name = "dgvBtn";
        dgvbtnColumn.UseColumnTextForButtonValue = true;
    }

    // Code for grid view button cell
    private void dgvTester_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            var senderGrid = (DataGridView)sender;
            if (senderGrid[e.ColumnIndex, e.RowIndex] is DataGridViewButtonCell)
            {
               //if(senderGrid[e.ColumnIndex, e.RowIndex] is DataGridViewTextBoxCell.Value  = "New")
                {
                    QA_ReviewForm qa = new QA_ReviewForm();
                    qa.Show();
                }

            }
        }
        catch (Exception error)
        {
            MessageBox.Show(error + "Invalid");
        }

    }

我尝试过使用
if(senderGrid[e.ColumnIndex, e.RowIndex] 是 DataGridViewTextBoxCell.Value = "New") 行
但它不起作用

【问题讨论】:

  • it is not working 是什么意思?你有什么错误吗?
  • Yes 在该行显示错误
  • 显示什么错误?
  • 代码错了吗?如何根据状态列值自定义
  • 如果它没有按预期工作那么它是错误的。如果您分享您遇到的错误,那么可以建议适当的解决方案。

标签: c# sql-server


【解决方案1】:

在您共享的代码中,也存在构建错误和逻辑错误。

您正在将数据表绑定到 GridView 并将一个 ButtonColumn 添加到 GridView。单击该列上的按钮时,您希望根据“状态”列的值显示不同的表单。

在您的代码中,您尝试比较 ButtonColumn 的值。 ButtonColumn 没有任何值,因为它没有绑定到 DataTable 中的任何列。

您需要获取GridView的“状态”列的值,然后比较它的值。

您需要在CellContentClick 事件处理程序中有以下代码。该代码是示例代码。您需要根据需要进行更改。

private void dgvTester_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        var senderGrid = (DataGridView)sender;
        if (senderGrid[e.ColumnIndex, e.RowIndex] is DataGridViewButtonCell)
        {
             // "3" in following `if` condition is the column index of "Status" column.
             // You need to put appropriate column index here.
               var selectedStatus = senderGrid[3, e.RowIndex].Value.ToString();
                if (selectedStatus  == "New")
                {
                    MessageBox.Show("New");
                }
                else if (selectedStatus == "Open")
                {
                    MessageBox.Show("Open");
                }
                else if (selectedStatus == "Assigned")
                {
                    MessageBox.Show("Assigned");
                }
                else if (selectedStatus == "Duplicate")
                {
                    MessageBox.Show("Duplicate");
                }
         }
     }
     catch (Exception error)
     {
         MessageBox.Show(error + "Invalid");
     }
 }

这应该可以帮助您解决问题。

【讨论】:

  • 我替换了它,但是当我点击按钮时没有任何反应。我现在能做什么
  • 我更新了代码。调试代码并检查selectedStatus 变量中的值。
  • 当点击按钮什么都没有发生
  • 你知道如何调试代码吗? dgvTester_CellContentClick_1 根本没有被执行,或者selectedStatus 有一些完全不同的值。调试只能在这里为您提供帮助。
  • 非常感谢。我输入了错误的索引,它应该是 5,而不是 4。现在它可以正常工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
相关资源
最近更新 更多