vb.net Gridview 链接按钮单击事件 - Windows 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/394989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gridview link button click event - Windows application
提问by Sarathy
How to handle click event of the linkbutton in gridview in vb.net (windows application )
如何在vb.net(windows应用程序)中处理gridview中链接按钮的点击事件
Thanks
谢谢
回答by t3rse
You need to consume the DataGridView.CellClick Event and just check that you've got the correct column index. You can do it with the following steps:
您需要使用 DataGridView.CellClick 事件并检查您是否获得了正确的列索引。您可以通过以下步骤来完成:
Create a new Windows Forms application
Drag a DataGridView onto the screen
In the design time properties, add a column to your grid of type DataGridViewLinkColumn
Make the DataPropertyName property to "Link" (no quotes).
In your forms constructor, paste this code under the call to InitializeComponent:
创建新的 Windows 窗体应用程序
将 DataGridView 拖到屏幕上
在设计时属性中,将一列添加到类型为 DataGridViewLinkColumn 的网格中
将 DataPropertyName 属性设为“链接”(无引号)。
在您的表单构造函数中,将此代码粘贴到对 InitializeComponent 的调用下:
Oh yeah, you're doing it in VB.NET, so it would be:
哦,是的,你是在 VB.NET 中做的,所以它会是:
Dim data As New DataTable()
data.Columns.Add(New DataColumn("Link", Type.GetType("System.String")))
Dim newRow As DataRow = data.NewRow()
newRow("Link") = "http://www.stackoverflow.com"
data.Rows.Add(newRow)
DataGridView1.DataSource = data
Consume the DataGridView.CellClick event
使用 DataGridView.CellClick 事件
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If (e.ColumnIndex = 0) Then
Dim link As String = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString()
System.Diagnostics.Process.Start(link)
End If
End Sub
回答by sky patel
Gridview Binding Time
Gridview 绑定时间
LetterB lb = new LetterB();
var letter = lb.GetLetter();
if (letter != null && letter.Count > 0)
{
dgvLetter.DataSource = letter;
DataGridViewLinkColumn lnkEdit = new DataGridViewLinkColumn();
lnkEdit.UseColumnTextForLinkValue = true;
lnkEdit.LinkBehavior = LinkBehavior.SystemDefault;
lnkEdit.HeaderText = "Edit";
lnkEdit.Name = "SiteName";
lnkEdit.LinkColor = Color.Blue;
lnkEdit.TrackVisitedState = true;
lnkEdit.Text = "Edit";
lnkEdit.UseColumnTextForLinkValue = true;
dgvLetter.Columns.Add(lnkEdit);
}
Click Event
点击事件
private void dgvLetter_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewLinkColumn && e.RowIndex >= 0)
{
//your code
}
}