Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下一行的Datagridview cellclick事件_C#_Winforms_Datagridview - Fatal编程技术网

C# 下一行的Datagridview cellclick事件

C# 下一行的Datagridview cellclick事件,c#,winforms,datagridview,C#,Winforms,Datagridview,我在datagridview上有两个不同的按钮,如快照中所示,第一次单击时操作正确执行,但在第二次单击时,如果已执行saccessed或unsaccessed,则单击下面的所有行,直到最后执行相同的操作 void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { // Ignore clicks that are not on button cells. if (e.RowIndex <

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

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;
    }
}
void dataGridView\u CellClick(对象发送者,DataGridViewCellEventArgs e)
{
//忽略不在按钮单元格上的单击。
if(e.RowIndex<0 | | e.ColumnIndex==dataGridView1.Columns[“状态取消”].Index)
{
不受制裁的地位(e);
dataGridView1.ClearSelection();
GetData();
返回;
}
if(e.RowIndex<0 | | e.ColumnIndex==dataGridView1.Columns[“状态确认”].Index)
{
确认假(e);
dataGridView1.ClearSelection();
GetData();
返回;
}
其他的
{
返回;
}
}

我根本不知道如何正确执行操作,因为代码段中的
if
条件都是错误的。他们应该是

if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Cancel"].Index)
{
    // ...
}
if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Confirm"].Index)
{
    // ...
}
void dataGridView\u CellClick(对象发送者,DataGridViewCellEventArgs e)
{
//如果单击是在新行或标题行上
如果(e.RowIndex==dataGridView1.NewRowIndex | | e.RowIndex<0)
返回;
//处理第一个按钮单击
if(e.ColumnIndex==dataGridView1.Columns[“您的第一个按钮”].Index)
{
//做第一个按钮点击的东西
MessageBox.Show(“单击的第一个按钮”);
}
//点击第二个按钮
if(e.ColumnIndex==dataGridView1.Columns[“您的第二个按钮”].Index)
{
//做第二个按钮点击的东西
MessageBox.Show(“单击第二个按钮”);
}
}

我已尝试了这两种解决方案,但相同的问题仍然存在。@用户3089887请确保您正确使用了代码,然后,例如,如果不是
执行第一个按钮单击的操作,而是将
消息框。显示(“第一个按钮单击”)
而不是
为第二个按钮点击
你把
MessageBox.Show(“点击的第二个按钮”)你收到了什么?(如回答中所示)@user3089887测试结果是什么?我已经尝试使用messagebox继续smae的事情。对于1“确认”或“取消”,单击它将进入无限循环。我有getDataMethod(),每次单击后都会加载datagridview。这是因为GetData Method吗?@user3089887您可以对它进行注释,看看它是否会被解决。如果您提供一个独立的测试,那么解决方案肯定会起作用。但在你的情况下,问题可能出在其他地方。
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");
    }

}