DataGridView
には、これに対応する列タイプのDataGridViewLinkColumn
があります。
DataPropertyName
は、グリッドのデータソースにに結合する列を設定し、手動でこの列の型をデータバインドする必要が
、:
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.DataPropertyName = "Contact";
col.Name = "Contact";
dataGridView1.Columns.Add(col);
あなたはまたの連絡先のプロパティから来て自動生成されたテキスト列を非表示にするだろうグリッド。
また、DataGridViewButtonColumn
の場合と同様に、CellContentClick
イベントに応答して、ユーザーの操作を自分で処理する必要があります。
ハイパーリンクではないセル値をプレーンテキストに変更するには、リンクセルタイプをテキストボックスセルに置き換える必要があります。例では、私はDataBindingComplete
イベント中にこれをやったの下:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (!System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Contact"] = new DataGridViewTextBoxCell();
}
}
}
をあなたが適用する必要がありますので、あなたはまた、私はこの第二の提案DataGridViewLinkCell
にDataGridViewTextBoxCell
を変更し、他の方向からこれを行うことができます各セルへのすべてのリンクに適用される変更。
これには利点がありますが、自動生成された列を非表示にする必要はないため、最適な場合があります。
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (System.Uri.IsWellFormedUriString(r.Cells["Contact"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Contact"] = new DataGridViewLinkCell();
// Note that if I want a different link colour for example it must go here
DataGridViewLinkCell c = r.Cells["Contact"] as DataGridViewLinkCell;
c.LinkColor = Color.Green;
}
}
}
私はより良いだけでなく、あなたがテキスト列を保つの答え上の第二の変形例を提供する(1列を隠し、DataPropertyNameを使用して)あなたのケースで私の最初のオプションを使用する方法を説明し、私の答えを編集しました。 –