2016-04-13 8 views
0

これを使用してDataGridViewHyperlinkを作成することができます。ハイパーリンクをDatagridviewのボタンに置​​き換えますか?

private void dgvCatalogue_CellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e) 
    { 
     string filename = dgvCatalogue[e.ColumnIndex, e.RowIndex].Value.ToString(); 
     if (e.ColumnIndex == 5 && File.Exists(filename)) 
     { 
      Process.Start(filename); 
     } 

    } 

それは、画像/ボタンでhyperlinkテキストを置換することは可能ですか?

+0

はい、あなたは[適切なを使用します列の種類](https://msdn.microsoft.com/en-gb/library/bxt3k60s(v = vs.110).aspx) – stuartd

答えて

1

これは確かに可能です。それはCellValueが表示されます:今、あなたはButtonTextがどうあるべきかを決定する必要があります

DataGridViewButtonCell bCell = new DataGridViewButtonCell(); 
dataGridView1[col, row] = bCell; 

:ここ

は一つのセルのためにそれをしない例です。これは細かいかもしれないが、あなたのファイル名が長いパスなどが含まれている場合..あなたは短いテキストを表示し、代わりにcell.Tagに名前を保存することもできます。あなたは全体の列がボタンにあなたを表示したい場合は

string someFileName= "D:\\temp\\example.txt"; 

dataGridView1[col, row].Tag = someFileName; 
dataGridView1[col, row].Value = Path.GetFileNameWithoutExtension(someFileName); 

DataGridViewButtonColumn bCol = new DataGridViewButtonColumn(); 
dataGridView1.Columns.Add(bCol); 

ボタン列のセルにも同じコデリが適用されます。

DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex]; 
if (cell.OwningColumn.CellType == typeof(DataGridViewButtonCell)) .. 

をこのようなTag利用コードを経由して値にアクセスするには:あなたはボタン電池である場合、あなたが書くことができますテストする

if (cell.Tag != null) string filename = cell.Tag.ToString(); 
関連する問題