Reputation: 41832
In my project, I'm filling the dataGridView
from dataSet
(binding the DataGridView
to DataSet
). The first column in dataGridView
must be LinkLabels
which I'm trying to get in the below code.
dgvMain.DataSorce = ds.Tables[0];
I tried: (not working)
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.
}
also tried
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.
}
The above attempts are adding linkLabel
to the first cell only not all the cells in that column
When I debugged my code, I concluded that after adding the linkLabel
to the first cell exception error is coming which I mentioned in the above code, which is making the code not to run properly.
Please give me any suggestions, what should I do?
EDIT: Though it is not the correct way but I gave the column cells a look like Linklabel
by writing the below code:
foreach (DataGridViewRow row in dgvMain.Rows)
{
row.Cells[1].Style.Font = new Font("Consolas", 9F, FontStyle.Underline);
row.Cells[1].Style.ForeColor = Color.Blue;
}
Now the problem is that I cant add Hand
like cursor to the only column cells(which is visible for LinkLabels). Is there anyway to achieve it? (I need answer for both questions, mainly the first one).
Upvotes: 4
Views: 22170
Reputation: 15055
You need to assign a new instance of DataGridViewLinkCell
to each "link" cell. Firt change the type of the cells that are links to be a DataGridViewLinkCell
and then handle the click on the cell, like this:
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);
}
}
Upvotes: 2
Reputation: 2660
This is what I've been doing when I'm changing type of the cell. Use your "also tried" loop and change:
dgvMain.Rows[intCount].Cells[0] = lnkCell;
To:
foreach (DataGridViewRow r in dgvMain.Rows)
{
DataGridViewLinkCell lc = new DataGridViewLinkCell();
lc.Value = r.Cells[0].Value;
dgvMain[0, r.Index] = lc;
}
Second Question: Set the CellMouseLeave and CellMouseMove of the dgvMain events to the following.
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;
}
}
Upvotes: 7