C# 向绑定到数据集的DataGridView单元格或列添加链接标签
在我的项目中,我正在从C# 向绑定到数据集的DataGridView单元格或列添加链接标签,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,在我的项目中,我正在从dataSet填充dataGridView(将dataGridView绑定到dataSet)。dataGridView中的第一列必须是LinkLabels,我正试图在下面的代码中获取它 dgvMain.DataSorce = ds.Tables[0]; 我试过:(不工作) 也尝试过 for (int intCount = 0; intCount < dgvMain.Rows.Count; intCount++) { dgvMain.Rows[intCount
dataSet
填充dataGridView
(将dataGridView
绑定到dataSet
)。dataGridView
中的第一列必须是LinkLabels
,我正试图在下面的代码中获取它
dgvMain.DataSorce = ds.Tables[0];
我试过:(不工作)
也尝试过
for (int intCount = 0; intCount < dgvMain.Rows.Count; intCount++)
{
dgvMain.Rows[intCount].Cells[0] = lnkCell; // (ERROR) Cell provided already belongs to a grid. This operation is not valid.
}
现在的问题是,我无法将
Hand
like光标添加到唯一的列单元格(链接标签可见)。究竟有没有办法做到这一点?(我需要两个问题的答案,主要是第一个问题)。这就是我在改变电池类型时一直在做的事情。
使用“也尝试过”循环和更改:
dgvMain.Rows[intCount].Cells[0] = lnkCell;
致:
第二个问题:
将dgvMain事件的CellMouseLave和CellMouseMove设置为以下值
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
this.Cursor = Cursors.Default;
}
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 1)
{
this.Cursor = Cursors.Hand;
}
}
您需要为每个“链接”单元格分配一个新的
DataGridViewLinkCell
实例。首先将链接单元格的类型更改为DataGridViewLinkCell
,然后处理对单元格的单击,如下所示:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (System.Uri.IsWellFormedUriString(r.Cells["Links"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Links"] = new DataGridViewLinkCell();
DataGridViewLinkCell c = r.Cells["Links"] as DataGridViewLinkCell;
}
}
}
// And handle the click too
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
{
System.Diagnostics.Process.Start( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
}
}
为什么不在aspx页面中的GridView的ItemTemplate中添加链接,您可以在GridView的ItemDataBound事件中轻松找到代码behaind中的taht控件。这是winforms而不是asp.net.)对不起,我不好,我认为这是一个网页,但你可以从这些链接得到帮助&为什么你不能使用设计器添加链接栏?你需要一个细胞,有时是一个链接,有时不是吗?@helgeheldre我试图建立的细胞应该永远是一个链接。我正在尝试修改由于绑定数据集而创建的现有列。dgvMain没有任何类似
Item
的方法我是否应该导入任何命名空间?@Mr_green。对不起,我有点陷入了VB.NET的思维模式。这是经过测试的,worksr相当于dgvMain。行[intCount]好的,谢谢你,这是有效的,但不幸的是,我也需要第二个问题的答案:(.如果你知道答案,请分享我想你需要检查你的代码,我做了一些更改以使其工作。
foreach (DataGridViewRow r in dgvMain.Rows)
{
DataGridViewLinkCell lc = new DataGridViewLinkCell();
lc.Value = r.Cells[0].Value;
dgvMain[0, r.Index] = lc;
}
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
this.Cursor = Cursors.Default;
}
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 1)
{
this.Cursor = Cursors.Hand;
}
}
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (System.Uri.IsWellFormedUriString(r.Cells["Links"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Links"] = new DataGridViewLinkCell();
DataGridViewLinkCell c = r.Cells["Links"] as DataGridViewLinkCell;
}
}
}
// And handle the click too
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
{
System.Diagnostics.Process.Start( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
}
}