如何在vb.net(windows应用程序)的gridview中处理linkbutton的click事件
由于
答案 0 :(得分:4)
您需要使用DataGridView.CellClick事件,并检查您是否获得了正确的列索引。您可以通过以下步骤完成此操作:
创建新的Windows窗体应用程序
将DataGridView拖到屏幕上
在设计时属性中,向DataGridViewLinkColumn类型的网格添加一列
将DataPropertyName属性设置为“Link”(无引号)。
在表单构造函数中,将此代码粘贴到InitializeComponent:
的调用下
哦,是的,你是在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
使用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
答案 1 :(得分:1)
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);
}
点击活动
private void dgvLetter_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewLinkColumn && e.RowIndex >= 0)
{
//your code
}
}