One of the annoying things about DataGrids and other .NET data controls is that they dissapear when they contain no data.  There is the EmptyDataTemplate but that does not show your table headers and footers.  There are a few solutions around like overriding the GridView control to add a new attribute, but I have found a simpler way. 

If you can change your datasource to always append a blank data row on the end so it always contains data, you can hid that row as it is data bound, leaving the header and footer showing.  I did this by using the SQL UNION keyword in my SelectCommand to return a row with empty values and an ID of 0.  Then in the GridView RowDataBound event you can set the row visibility to 0.
 
SelectCommand="SELECT SomeID, SomeData FROM SomeTable UNION SELECT 0 AS SomeID, null AS SomeData ORDER BY SomeData"

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dr = (DataRowView)
e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow && dr["SomeID"].ToString() == "0"
)
e.Row.Visible = false;
}

I have also used the same idea with FormView controls so that I can use the same form for handling inserts and updates rather than having to duplicate them in the InsertItemTemplate and EditItemTemplate tags.
22/2/2011 09:52:43 am


Please post more of this. I largely enjoyed it.


Comments are closed.