2010-11-23 3 views
4

このDataGridViewオブジェクトを更新しようとしています。値が== "bob"の場合、その名前の横にある列にボタンが表示されます。そうでなければ、ボタンは表示されません。DataGridViewの列の一部(すべてではない)にボタンを追加するにはどうすればよいですか?

しかし、セルにVisibleを設定できないため、これは機能しません。 Value == "bob"だった行だけにボタンを追加する方法はありますか?

答えて

2

ここには2つの可能性があります。一つは醜いもの、もう一つはMSDNです。

醜い:

は、次の操作を行い、実行時に、あなたのDGVにボタンを追加します。
- あなたのDGVに結合していないDataGridViewTextBoxColumnを追加します。 DGVのインデックス値に注意してください。これがあなたのボタンを置く場所です。
- そのようなあなたのDGVのCellFormattingイベントを使用します。ユーザーはcellButton_Clickイベントが発生しますボタンをクリックすると

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { 
     if (e.ColumnIndex == 0) { // Assumes column 0 has the data that determines if a button should be displayed. 
      if (e.Value.ToString() == "bob") { // Test if a button should be displayed on row. 
       // Create a Button and add it to our DGV. 
       Button cellButton = new Button(); 
       // Do something to identify which row's button was clicked. Here I'm just storing the row index. 
       cellButton.Tag = e.RowIndex; 
       cellButton.Text = "Hello bob"; 
       cellButton.Click += new EventHandler(cellButton_Click); 
       dataGridView1.Controls.Add(cellButton); 
       // Your ugly button column is shown here as having an index value of 3. 
       Rectangle cell = this.dataGridView1.GetCellDisplayRectangle(3, e.RowIndex, true); 
       cellButton.Location = cell.Location; 
      } 
     } 
    } 

。ここにいくつかのテストコードがあります:

void cellButton_Click(object sender, EventArgs e) { 
     Console.WriteLine("Hello from row: {0}", ((Button) sender).Tag); 
    } 

ご覧のとおり、これはあまり洗練されていません。私が見つけたさらに醜いサンプルに基づいています。あなたのニーズに合わせて変更できると確信しています。

MSDN:無効なボタンを条件付きで表示する独自の(拡張)DataGridViewButtonColumnをロールオーバーします。

このオプションで唯一の条件付きは彼ら無効にし、このオプションは、実際に任意のボタンを削除しません。もちろん、How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control

を参照してください。しかし、あなたのアプリケーションのために、これはより良いかもしれません。代わりに、DataGridViewButtonColumnを使用しての

DataGridViewTextBoxColumnを使用し、適切な場合にDataGridViewButtonCellを追加します。ここでは

3

は、私はこれを達成するために前に使用しました小ぎれいなハックです。

上記の例では

private void button1_Click(object sender, EventArgs e) 
    { 
     // Iterate through each of the rows. 
     for (int i = 0; i < dgv.RowCount - 1; i++) 
     { 
      if (dgv.Rows[i].Cells[0].Value.ToString() == "bob") 
      { 
       // Here is the trick. 
       var btnCell = new DataGridViewButtonCell(); 
       dgv.Rows[i].Cells[1] = btnCell; 
      } 
     } 
    } 

、私は2つのDataGridViewTextBoxColumnsを持っていると、ボタンのクリックイベントに行のそれぞれを反復。私は最初の列に "bob"が含まれているかどうか確認し、そうであれば隣の列にボタンを追加します。このトリックは、ボタンのクリック、RowsAddedイベント、CellEndEditイベントなど必要に応じて使用できます。さまざまな方法で試してみてください。これが誰かを助けることを願って!

あなたは、セル絵画イベントでセル画を扱うことができる
1

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if(e.RowIndex>=0 && e.ColumnIndex == indexOfButtonColumn && value[e.RowIndex] != "bob") 
    { 
      e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground & ~DataGridViewPaintParts.ContentBackground); 
      e.Handled = true;  
    } 
} 
関連する問題