3

There is no ShowFooterWhenEmpty property in GridView ;(

When I didn't use ObjectDataSource for data binding it was as simple as:

...
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable data = new DataTable();
    conn.Open();
    adapter.Fill(data);
    conn.Close();

    if (data.Rows.Count > 0)
    {
        grid.DataSource = data;
        grid.DataBind();
    }
    else
    {
        data.Rows.Add(data.NewRow());
        grid.DataSource = data;
        grid.DataBind();

        int TotalColumns = grid.Rows[0].Cells.Count;
        grid.Rows[0].Cells.Clear();
        grid.Rows[0].Cells.Add(new TableCell());
        grid.Rows[0].Cells[0].ColumnSpan = TotalColumns;
        grid.Rows[0].Cells[0].Text = "No Records Found";
    }
...

called by Page_Load() if (!IsPostBack) {...}

Now ObjectDataSource stands for auto-binding and paging.

How am I supposed to render the Footer where my Insert button located?

Tried OnSelected event of ObjectDataSource but I don't know how add a row in there.

protected void ODS_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    DataSet ds = e.ReturnValue as DataSet;
    // Add empty row here or 'No Records Found' string to force Footer show
}

Help, please!

master-lame-master
  • 3,101
  • 2
  • 30
  • 47

1 Answers1

1

As you already know the footer does not display when the GridView is empty (no rows). The work around is to ensure that a dummy row is returned from the ObjectDataSource when there is no real data present.

I would suggest checking the number of rows in the GridView DataBound event, if there are none, then modify the SelectMethod of the ObjectDataSource to return a dummy row of dummy data, and then re-bind the gridview. An example of dummy data could be:

ID : null
Name : 'No data, please add using form below' ...etc...
Description : null
...
etc
...

The GridView will then show the footer after the re-bind.

FastGeek
  • 411
  • 2
  • 7