Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two possibilities here, one ugly and one from MSDN.</p> <p><strong>The Ugly:</strong> Add a button to your DGV at runtime</p> <p>Do the following:<br> - Add an unbound DataGridViewTextBoxColumn to your DGV. Note it's index value in your DGV; this is where you'll put your button.<br> - Use your DGV's CellFormatting event like so:</p> <pre><code> 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; } } } </code></pre> <p>When a user clicks the button the cellButton_Click event will fire. Here's some test code:</p> <pre><code> void cellButton_Click(object sender, EventArgs e) { Console.WriteLine("Hello from row: {0}", ((Button) sender).Tag); } </code></pre> <p>As you can see this isn't very refined. I based it on an even uglier sample I found. I'm sure you can modify it to suit your needs.</p> <p><strong>From MSDN:</strong> Roll your own (extend) DataGridViewButtonColumn that conditionally displays a disabled button.</p> <p>For this option see <a href="http://msdn.microsoft.com/en-us/library/ms171619%28VS.90%29.aspx" rel="nofollow">How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control</a></p> <p>Of course this option doesn't actually <em>remove</em> any buttons, only conditionally <em>disables</em> them. For your application however, this might be better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload