【问题标题】:Datagridview cellclick event for next rows下一行的 Datagridview cellclick 事件
【发布时间】:2015-09-14 10:37:13
【问题描述】:

我在 datagridview 上有两个不同的按钮,如第一次单击时的快照所示,该操作正确执行,但在第二次单击时,如果已批准或未批准,则单击单击下方的所有行,直到最后执行相同的操作。

void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Ignore clicks that are not on button cells.
    if (e.RowIndex < 0 || e.ColumnIndex == dataGridView1.Columns["Status Cancel"].Index)
    {
        statusNotSanctioned(e);
        dataGridView1.ClearSelection();
        GetData();
        return;
    }

    if (e.RowIndex < 0 || e.ColumnIndex == dataGridView1.Columns["Status Confirm"].Index)
    {
        confirmLeave(e);
        dataGridView1.ClearSelection();
        GetData();
        return;
    }
    else
    {
        return;
    }
}

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:
    void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        //if click is on new row or header row
        if( e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
            return;
    
        //Handle First Button Click
        if( e.ColumnIndex  == dataGridView1.Columns["Your First Button"].Index)
        {
            //Do the stuff for first button click
            MessageBox.Show("First Button Clicked");
        }
    
        //Handle Second Button Click
        if( e.ColumnIndex == dataGridView1.Columns["Your Second Button"].Index)
        {
            //Do the stuff for second button click
            MessageBox.Show("Seccond Button Clicked");
        }
    
    }
    

    【讨论】:

    • 我已经尝试了两种解决方案,但同样的问题仍然存在。
    • @user3089887 请确保您正确使用了代码,然后例如,如果您使用MessageBox.Show("First Button Clicked"); 而不是Do the stuff for first button click 而不是Do the stuff for second button click,您使用MessageBox.Show("Second Button clicked"); 您会收到什么? (就像在答案中一样)
    • @user3089887 测试结果如何?
    • 我已经尝试使用消息框,smae 事情继续。对于 1 确认或取消点击它进入无限循环。我有 getDataMethod() 每次点击后都会加载 datagridview 是因为 GetData 方法吗?
    • @user3089887 可以评论看看能不能解决。当然,如果您提供独立测试,该解决方案将起作用。但在你的情况下,可能是其他地方的问题。
    【解决方案2】:

    我根本看不到如何正确执行该操作,因为 sn-p 中的两个if 条件都是错误的。他们应该是

    if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Cancel"].Index)
    {
        // ...
    }
    if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Confirm"].Index)
    {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2015-01-13
      • 2019-09-24
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      相关资源
      最近更新 更多