3

私のプロジェクトでは、dataGridViewfromを埋めています ( to をdataSetバインドしています)。の最初の列は、以下のコードで取得しようとしているものでなければなりません。DataGridViewDataSetdataGridViewLinkLabels

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正しく実行しないでください。

何か提案があれば教えてください。どうすればよいですか?

EDIT:正しい方法ではありませんが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 に表示される) に追加できないことです。とにかくそれを達成することはありますか?(両方の質問、主に最初の質問への回答が必要です)。

4

2 に答える 2

6

これは、セルのタイプを変更するときに行ってきたことです。「また試した」ループを使用して変更します。

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;
  }

2 番目の質問: 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;
    }
}
于 2012-11-02T09:53:29.533 に答える