How to hide button in last("fake") row of Datagridview?
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 be开发者_如何学编程tter way to do this for this particular situation.
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.
There's no better way, you need to extend class DataGridViewButtonCell:
How to: Disable Buttons in a Button Column
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
}
}
精彩评论