0

如果是这样,我们如何分别处理每个的 cellcontentclick 事件?我正在动态添加列。这是我的代码:-
dataGridViewShow.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Date", DataPropertyName = "Date", CellTemplate = new DataGridViewTextBoxCell() });

DataGridViewLinkColumn toComputer = new DataGridViewLinkColumn();
        toComputer.HeaderText = "Status";
        toComputer.Name = "link1";
        toComputer.UseColumnTextForLinkValue = true;
        toComputer.Text = "Save to File";
        toComputer.ActiveLinkColor = Color.White;
        toComputer.LinkColor = Color.Blue;
        toComputer.TrackVisitedState = true;
        toComputer.VisitedLinkColor = Color.Green;
        dataGridViewShow.Columns.Add(toComputer);
        dataGridViewShow.CellContentClick += (SaveFileType_click);  

DataGridViewLinkColumn toNet = new DataGridViewLinkColumn();
        toLinx.HeaderText = " ";
        toLinx.Name = "link2";
        toLinx.UseColumnTextForLinkValue = true;
        toLinx.Text = "Send to Net";
        toLinx.ActiveLinkColor = Color.White;
        toLinx.LinkColor = Color.Blue;
        toLinx.TrackVisitedState = true;
        toLinx.VisitedLinkColor = Color.Green;            
        dataGridViewShow.Columns.Add(toNet);
        dataGridViewShow.CellContentClick += ("Don't know how this gets handled");  

上面的代码确实显示了两个不同的链接类型列,但是我不知道如何分别处理每个事件?

private void SaveFileType_click(object sender, DataGridViewCellEventArgs e)
    {
        selectedRow = e.RowIndex;

        if (e.ColumnIndex == 0)
        {
            //some code goes here
        }if (e.ColumnIndex == 1 && (string)dataGridViewShow[e.ColumnIndex, e.RowIndex].Value != "Complete")
        {
            //some code goes here
        }
    }
4

1 回答 1

2

您不单独处理事件。您需要处理OnCellClickCellContentClick事件。然后使用DataGridViewCellEventArgsobject e,您可以获得被点击的列和行。根据选择的单元格,您以不同的方式处理事件

public void dataGridView1_OnCellClick(object sender,
    DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == YourLinkColumnIndexA)
        //some code goes here.
    else if (e.ColumnIndex == YourLinkColumnIndexB)
        //some code goes here.
}

因此,您可以像以前那样在代码中连接事件,但只连接一次

我希望这有帮助。

于 2013-04-08T20:54:47.280 回答