just wanted to know what is the best and easiest way to show a gridview footer for data entry even when the gridview is empty ?
Set your datasource to the type of object you're binding to the GridView with one object filled with empty values, then hide that DataRow.
EDIT: Since you're using a datatable...
DataTable dt = new DataTable();
// Define all of the columns you are binding in your GridView
dt.Columns.Add("AColumnName");
...
...
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
myGridView.DataSource = dt;
myGridView.DataBind();
Another solution is to always add a dummy row in your datasource, "mark" that row with a specific value, then hide the row on RowDataBound.
To be more precise, add the column ", 0 AS dummyRow" to end of your query's SELECT clause, then UNION ALL the full statment to
SELECT NULL AS column1, NULL AS column2,...,NULL AS columnN, 1 AS dummyRow
Once you have the query in place (all of which can be done within your SQLDataSource or in the your DAL object, your code for the grid will look something like this:
Protected Sub MyGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (Not e.Row.DataItem Is Nothing) AndAlso (CInt(e.Row.DataItem("dummyRow")) = 1) Then
e.Row.Visible = False
End If
End Sub
This solution comes with some obvious overhead, since this check will be done for every row of the results, not to mention you have to change your SELECT Query, but it also has the advantage of not requiring to dynamically change the dataset (as in the first example) and not requiring much code or having to deploy custom control libraries for your web-project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With