Make GridView footer visible when there is no data binding

how to show the footer when there is no data in the gridview to insert data from the footer.

+5
source share
3 answers

The easiest way to do this is to associate an array with a length of one. You can put something in this, you like to identify that it is a dummy string. In the GridViews RowDataBound method, check if the data item is a dummy string (make sure the RowType is a DataRow first before trying to validate the data). If it is a dummy line, set the line visibility to false. The footer and header should now display without any data.

Make sure the ShowFooter property is set to true in your GridView.

eg.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostback)
    {
         myGrid.DataSource = new object[] {null};
         myGrid.DataBind();
    }
}    

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem == null)
        {
             e.Row.Visible = false;
        }
    }
}
+4
source

Here's something easy that I created:

    /// <summary>
    /// Ensures that the grid view will contain a footer even if no data exists.
    /// </summary>
    /// <typeparam name="T">Where t is equal to the type of data in the gridview.</typeparam>
    /// <param name="gridView">The grid view who footer must persist.</param>
    public static void EnsureGridViewFooter<T>(GridView gridView) where T: new()
    {
        if (gridView == null)
            throw new ArgumentNullException("gridView");

        if (gridView.DataSource != null && gridView.DataSource is IEnumerable<T> && (gridView.DataSource as IEnumerable<T>).Count() > 0)
            return;

        // If nothing has been assigned to the grid or it generated no rows we are going to add an empty one.
        var emptySource = new List<T>();
        var blankItem = new T();
        emptySource.Add(blankItem);
        gridView.DataSource = emptySource;

        // On databinding make sure the empty row is set to invisible so it hides it from display.
        gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem == (object)blankItem)
                e.Row.Visible = false;
        };
    }

To call it, you can use the following:

        MyGridView.DataSource = data;
        EnsureGridViewFooter<MyDataType>(MyGridView);
        MyGridView.DataBind();

Hope this helps. Hurrah!

+2
source

Here is an easy way to show the footer when there is empty data in the GridView.

0
source

All Articles