views:

136

answers:

3

In my dgv I have a DataGridViewButtonColumn which adds a "delete row" button to each row. However, a button also gets created for the "fake" row at the bottom of the dgv, which makes no sense in this context because that row does not correspond to a record yet. What is a good way to hide that button, or at least paint over it?

I came across this page which shows a method that might do the trick, but I was wondering if there is a better way to do this for this particular situation.

A: 

Try to go for RowDataBound event of the grid.

protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        Int32 cellIndex = 1; //Find your delete button cell index
        e.Row.Cells(cellIndex).CssClass = "hidden"; // whatever class you like to hide the cell
    }
}
Vitaly
seriously ... this is not very good. You should find the button and then say: button.Visible = false;
Sem Dendoncker
-1: This is not ASP.NET, neither is this a GridView.
leppie
Visible.False prevents an object from rendering to the response html code. For this particular scenario that's probably ok. And what if you need to show the button back - a postback and a round-trip to the server (no matter full one or ajax)? Compare it to $('#myButtonID').removeClass('hidden');
Vitaly
+1  A: 

There's no better way, you need to extend class DataGridViewButtonCell:

How to: Disable Buttons in a Button Column

Branimir
+1  A: 

It's hard to control the controls in the "fake row". To resolve this problem, you can set dgv.AllowUserToAddRows = false to avoid the "fake row". Then the fake row will not come out by users' input. You can add another button named "Add row" to add a "real row". Also you can add event handler to the last row, e.g. If user inputs something in the last row, a new row (real row) will be added.

Generally speaking, you can use a real row instead of the behavior from the fake row, because a real row is easier to control.

Danny Chen