2010-11-23 73 views
4

我想更新这个DataGridView对象,使得如果一个值==“bob”的名字旁边会有一个按钮,否则我不想出现任何按钮。如何将按钮添加到我的DataGridView中的某些列(不是全部)?

DataGridViewTextBoxColumn valueColumn = new DataGridViewTextBoxColumn(); 
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); 

buttonColumn.ReadOnly = true; 
buttonColumn.Visible = false; 

this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
valueColumn, 
buttonColumn, 
}); 

//elsewhere... 

if(value == "bob") 
{ 
    Button button = new Button() 
    { 
     Text = "null", 
    }; 

    index = dgv.Rows.Add(value, button); 
    DataGridViewButtonCell buttonCell = dgv.Rows[index].Cells[2] as DataGridViewButtonCell; 
    buttonCell.Visible = true; 
} 
else 
{ 
    dgv.Rows.Add(value); 
} 

但是,由于我无法在单元格上设置Visible,因此这不起作用。有没有一种方法来添加一个按钮,只有行的值是==“bob”?

回答

2

这里有两种可能性,一种是丑陋的,另一种来自MSDN。

丑陋:在运行时你DGV添加一个按钮

执行以下操作:
- 未绑定DataGridViewTextBoxColumn添加到您的DGV。请注意,它是DGV中的索引值;这是您放置按钮的位置。
- 用你的DGV的CellFormatting事件,像这样:

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

当用户点击该按钮cellButton_Click事件将触发。这里有一些测试代码:

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

当然这个选项实际上并不删除任何按钮,只能在一定条件禁用他们。但是,对于您的应用程序,这可能会更好。

3

这里是我以前用来完成这个整洁的小黑客:

而不是使用一个DataGridViewButtonColumn的,使用DataGridViewTextBoxColumn并添加DataGridViewButtonCell在适当情况下。

例如

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

在上面的示例中,我有两个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;  
    } 
} 
相关问题