Show gridview footer on empty grid?

后端 未结 6 2183

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 ?

6条回答
  •  忘掉有多难
    2020-12-06 17:53

    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.

提交回复
热议问题