This is a migrated thread and some comments may be shown as answers.

grid header without an initial databind

5 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ChrisWalker
Top achievements
Rank 1
ChrisWalker asked on 31 Jul 2008, 07:40 PM
I have a databound grid that works fine when there is data returned in the selectcommand of its sqldatasource. However on the inital load of the page I need to show the grid with no data (empty) but the column headers showing. If I remove the selectcommand of the sqldatasource just an empty block appears with no column headers. If I create a selectcommand in the datasource where nothing will be returned ie. "Select ... where 1=2" then I get the desired result. However this requires the grid to do an actual query to the db which seems a little wasteful to me. Is there a way to have this databound grid (I am using telerik:GridBoundColumn for all columns) show the column header without this initial databind?

5 Answers, 1 is accepted

Sort by
0
Sean
Top achievements
Rank 2
answered on 31 Jul 2008, 08:51 PM
I would just add an empty row of data. Like such:

DataSet ds = GetSomeData();
if (ds.Tables[0].Rows.Count == 0)
{
     ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
}

     RadGrid1.DataSource = ds;
     RadGrid1.DataBind();

Sean~
0
Xcalibur
Top achievements
Rank 1
answered on 01 Aug 2008, 09:15 AM
Unless i misunderstand you this can be achieved using the 

ShowHeadersWhenNoRecords and

EnableNoRecordsTemplate

properties

0
Sean
Top achievements
Rank 2
answered on 01 Aug 2008, 01:30 PM
Good call.. I've scrolled through the various properties and never even noticed that one.

Thanks!

Sean
0
ChrisWalker
Top achievements
Rank 1
answered on 01 Aug 2008, 03:12 PM

Sean, Xcalibur

Thank you both for the hints. Using both of your suggestions I did the following and included the steps and code snippets  for anyone else who might need to do this:

1- Removed the DataSourceID attribute from the <radGrid> definition. This was done so that I could have the OnNeedDataSource event fired.
2- Added the OnNeedDataSource attribute to the <RadGrid>.
3- Added the <NoRecordsTemplate> to override the deafault 'No Records to display' message
4- I did not need to alter the ShowRecordsWithNoHeaders property because it defaults to true.
5- Created the NeedDataSource event to handle the initalLoad and assign it the empty dataset

Code snippets 
*  my RadGrid definition is now (with DataSourceID removed and OnNeedDataSource added):
<telerik:RadGrid ID="GridMedia" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" AllowMultiRowSelection="false" Skin="WebBlue" OnNeedDataSource="Grid_NeedDataSource" OnItemDataBound="Grid_ItemDataBound" OnItemCommand="Grid_ItemCommand" AllowSorting="true" PageSize="10" Height="200px">

* my needDataSource event which creates an empty dataset on the initialload
 and will show the grid with no rows

protected void Grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

    if (e.RebindReason == GridRebindReason.InitialLoad)
    {
            DataTable emptyTable = new DataTable();
            GridMedia.DataSource = emptyTable;
    }
    else
    {
            GridMedia.DataSourceID =
"MediaSQLDataSource";
    }
}

* a button click event that will use the datasource that returns my data

protected

void btnSearch_Click(object sender, EventArgs e)
{
    // if my datasource with data has not yet been assigned,
    // set it now
    if (GridMedia.DataSourceID.Length == 0 )
    {
        GridMedia.DataSource =
null;
        GridMedia.DataSourceID =
"MediaSQLDataSource";
    }
    else
    {
        GridMedia.DataBind();
    }
}


0
Xcalibur
Top achievements
Rank 1
answered on 04 Aug 2008, 01:33 AM
np guys, its always good to be able to help.  Chris thanks for posting your solution, its always a good practise for others who might face the same issue in the future.
Tags
Grid
Asked by
ChrisWalker
Top achievements
Rank 1
Answers by
Sean
Top achievements
Rank 2
Xcalibur
Top achievements
Rank 1
ChrisWalker
Top achievements
Rank 1
Share this question
or