0

I have a DataGridView and I want in my grid a column having some cells that display a button and some cells that do not contain the button. In order to solve this problem I've added a DataGridViewButtonColumn and I've written this method that I call to add the rows to my column:

 private void AttachNewRow(bool validRow)
 {
     DataGridViewRow newRow = GetNewRow();
     if (validRow)
     {
         newRow.Cells["Info"].Value = "Click me";
     }
     else
     {
         // Here I would like to hide the button in the cell
         newRow.Cells["Info"].Value = null;
     }
 }

The problem is that when I set the cell value to null I receive an exception. How can I display some of these cells without the button inside? Thank you

Martina
  • 791
  • 1
  • 14
  • 26

2 Answers2

2

Looks like the GetNewRow() returns the Row you are talking about already inserted in the DGV.

If that function has the knowledge if the 'Info' Column shall contain a Button, it can pass it, maybe in the Tag:

if (somecondiiotn) newRow.Columns["Info"].Tag = "Button";

Then you can write:

private void AttachNewRow()
{
    DataGridViewRow newRow = GetNewRow();
    if ( newRow.Cells["Info"].Tag == null || 
         newRow.Cells["Info"].Tag != "Button") return;
    //..

If otoh the code that calls AttachNewRow() has the required knowledge, it could instead pass it on in a parameter:

private void AttachNewRow(bool infoButton)

If the knowlegde is available only later you can still change individual cells.

Update:

since you are now passing the condition into the method you can act accordingly.

I don't know why you get an exception in your code - I don't. But to really hide the Button you should change the cell to be a 'normal' DataGridViewTextBoxCell:

else
{
   DataGridViewCell cell = new DataGridViewTextBoxCell();
   newRow.Cells["Info"] = cell;
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Hi TaW, I've tried to rewrite the question because of your reply I realized it was not clear. It's clear now what I mean?Thank you very much – Martina Jan 16 '15 at 09:00
  • 1
    See the updated answer. How come you crash? On which line? Have you changed the code, so that the info column is a button column to start with? I have assumed you do that now and simply change the invalid rows back. You can switch them back to Button in the same way if and when they get valid.. – TaW Jan 16 '15 at 12:20
  • The exception was on the line newRow.Cells["Info"].Value = null; your answer solved my problem, thank you very much – Martina Jan 16 '15 at 14:42
  • 1
    Strange, I could do that.. But if it works as you need it, all is well. btw: danish got a good point inserting `ReadOnly = true;` – TaW Jan 16 '15 at 15:02
1

You could try something like this in RowAdded event handler. Here it is assumed that third column is button column:

if (condition)
        {

            dataGridView1.Rows[e.RowIndex].Cells[2]  = new DataGridViewTextBoxCell();
            dataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
        }
danish
  • 5,550
  • 2
  • 25
  • 28