C# 在win窗体中添加类似超链接的功能

C# 在win窗体中添加类似超链接的功能,c#,windows,winforms,events,datagridview,C#,Windows,Winforms,Events,Datagridview,我的动机是显示一些信息作为链接,点击它,我应该能够得到点击项目的id,并打开新的窗口与项目的详细信息。由于我是win表单的新手,但我做了一些研究,可能的选择是,但我无法将id与列数据链接,并单击打开新窗口的事件 或者还有其他更好的方法吗?我假设您使用的是元素 你能做的就是使用这个对象。它将传递一个对象,对象上有一个ColumnIndex属性和一个RowIndex属性。通过这种方式,您可以找出用户在数据网格中单击的位置 例如,您可以使用该信息查找id或其他信息,因为您现在知道了用户单击的行和单元格

我的动机是显示一些信息作为链接,点击它,我应该能够得到点击项目的id,并打开新的窗口与项目的详细信息。由于我是win表单的新手,但我做了一些研究,可能的选择是,但我无法将id与列数据链接,并单击打开新窗口的事件


或者还有其他更好的方法吗?

我假设您使用的是元素

你能做的就是使用这个对象。它将传递一个对象,对象上有一个ColumnIndex属性和一个RowIndex属性。通过这种方式,您可以找出用户在数据网格中单击的位置

例如,您可以使用该信息查找id或其他信息,因为您现在知道了用户单击的行和单元格

任意的,例如:

// wire up the event handler, this could be anywhere in your code
dataGridView.CellClick += dataGridView_CellClick; // when the event fires, the method dataGridView_CellClick (as shown below) will be executed

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var rowIndex = e.RowIndex;
    var columnIndex = e.ColumnIndex; // = the cell the user clicked in

    // For example, fetching data from another cell
    var cell = dataGridView.Rows[rowIndex].Cells[columnIndex];

    // Depending on the cell's type* (see a list of them here: http://msdn.microsoft.com/en-us/library/bxt3k60s(v=vs.80).ASPX) you could cast it
    var castedCell = cell as DataGridViewTextBoxColumn;

    // Use the cell to perform action
    someActionMethod(castedCell.Property);
}

(*DataGridViewCell类型:)

我假设您正在使用一个元素

你能做的就是使用这个对象。它将传递一个对象,对象上有一个ColumnIndex属性和一个RowIndex属性。通过这种方式,您可以找出用户在数据网格中单击的位置

例如,您可以使用该信息查找id或其他信息,因为您现在知道了用户单击的行和单元格

任意的,例如:

// wire up the event handler, this could be anywhere in your code
dataGridView.CellClick += dataGridView_CellClick; // when the event fires, the method dataGridView_CellClick (as shown below) will be executed

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var rowIndex = e.RowIndex;
    var columnIndex = e.ColumnIndex; // = the cell the user clicked in

    // For example, fetching data from another cell
    var cell = dataGridView.Rows[rowIndex].Cells[columnIndex];

    // Depending on the cell's type* (see a list of them here: http://msdn.microsoft.com/en-us/library/bxt3k60s(v=vs.80).ASPX) you could cast it
    var castedCell = cell as DataGridViewTextBoxColumn;

    // Use the cell to perform action
    someActionMethod(castedCell.Property);
}

(*datagridview单元格类型:)

当您有datagridview时,可以按如下方式获取单元格的值:

首先,创建一个cellclick事件

datagridview1.CellClick+= CellClickEvent;
DataGridViewCellEventArgs包含一些属性,这些属性是rowindex(您单击的行)和columnindex(您单击的列)以及其他一些

创建一个包含2列的datagrid,第0列保存行的Id,第1列保存链接

void CellClickEvent(object sender, DataGridViewCellEventArgs e)
{
 if(e.ColumnIndex == 1) // i'll take column 1 as a link
 {
  var link = datagridview1[e.columnindex, e.rowindex].Value;
  var id = datagridview1[0, e.rowindex].Value;

  DoSomeThingWithLink(link, id);
 }
}

void DoSomeThingWithLink(string link, int id)
{
 var myDialog = new Dialog(link,id);
 myDialog.ShowDialog();
 myDialog.Dispose(); //Dispose object after you have used it
}

使用datagridview时,可以按如下方式获取单元格的值:

首先,创建一个cellclick事件

datagridview1.CellClick+= CellClickEvent;
DataGridViewCellEventArgs包含一些属性,这些属性是rowindex(您单击的行)和columnindex(您单击的列)以及其他一些

创建一个包含2列的datagrid,第0列保存行的Id,第1列保存链接

void CellClickEvent(object sender, DataGridViewCellEventArgs e)
{
 if(e.ColumnIndex == 1) // i'll take column 1 as a link
 {
  var link = datagridview1[e.columnindex, e.rowindex].Value;
  var id = datagridview1[0, e.rowindex].Value;

  DoSomeThingWithLink(link, id);
 }
}

void DoSomeThingWithLink(string link, int id)
{
 var myDialog = new Dialog(link,id);
 myDialog.ShowDialog();
 myDialog.Dispose(); //Dispose object after you have used it
}

你所说的数据,是否已经在数据网格中?是的,我将数据网格中的数据显示为链接,但不知道下一步要做什么。好的,基于此,我添加了一个答案-不知道它是否反映了你想要做的事。如果没有,请提供更多信息。您所说的数据是否已经在数据网格中?是的,我将数据网格中的数据显示为链接,但不知道下一步要做什么。好的,基于此,我添加了一个答案-不知道它是否反映了您想要做的事情。如果没有,请提供更多信息。但对于您来说,我认为通过显示添加事件处理程序的示例,您的信息更加完整。我在我的答案中进行了编辑以使其完整,但这里将给出应有的评价。但对您来说也是一样,我认为通过显示添加事件处理程序的示例,您的答案更完整。我编辑了我的答案以使其完整,但在此将给出应有的评价。