在我的项目中,我正在从 dataSet
填充 dataGridView
(将 DataGridView
绑定(bind)到 DataSet
) . dataGridView
中的第一列必须是 LinkLabels
,我试图在下面的代码中获取它。
dgvMain.DataSorce = ds.Tables[0];
我试过了:(不工作)
DataGridViewLinkCell lnkCell = new DataGridViewLinkCell();
foreach (DataGridViewRow row in dgvMain.Rows)
{
row.Cells[0] = lnkCell; // (ERROR) Cell provided already belongs to a grid. This operation is not valid.
}
也试过
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.
}
上面的尝试是将 linkLabel
添加到第一个单元格,而不是该列中的所有单元格
当我调试我的代码时,我得出结论,在添加 linkLabel
我在上面的代码中提到的第一个单元格异常错误即将到来,这使得代码无法正常运行。
请给我任何建议,我该怎么办?
编辑:虽然这不是正确的方法,但我通过编写以下代码使列单元格看起来像 Linklabel
:
foreach (DataGridViewRow row in dgvMain.Rows)
{
row.Cells[1].Style.Font = new Font("Consolas", 9F, FontStyle.Underline);
row.Cells[1].Style.ForeColor = Color.Blue;
}
现在的问题是我无法将 Hand
之类的光标添加到唯一的列单元格(对于 LinkLabels 可见)。无论如何要实现它? (我需要回答这两个问题,主要是第一个)。
最佳答案
这就是我在更改单元格类型时一直在做的事情。 使用您的“也尝试过”循环并更改:
dgvMain.Rows[intCount].Cells[0] = lnkCell;
收件人:
foreach (DataGridViewRow r in dgvMain.Rows)
{
DataGridViewLinkCell lc = new DataGridViewLinkCell();
lc.Value = r.Cells[0].Value;
dgvMain[0, r.Index] = lc;
}
第二个问题: 将 dgvMain 事件的 CellMouseLeave 和 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;
}
}
关于c# - 将链接标签添加到绑定(bind)到 DataSet 的 DataGridView 单元格或列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13190590/