grid header without an initial databind

6 posts, 0 answers
  1. ChrisWalker
    ChrisWalker avatar
    21 posts
    Member since:
    May 2007

    Posted 31 Jul 2008 Link to this post

    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?
  2. Sean
    Sean  avatar
    54 posts
    Member since:
    Apr 2008

    Posted 31 Jul 2008 Link to this post

    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~
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Xcalibur
    Xcalibur avatar
    11 posts
    Member since:
    Jul 2007

    Posted 01 Aug 2008 Link to this post

    Unless i misunderstand you this can be achieved using the 

    ShowHeadersWhenNoRecords and

    EnableNoRecordsTemplate

    properties

  5. Sean
    Sean  avatar
    54 posts
    Member since:
    Apr 2008

    Posted 01 Aug 2008 Link to this post

    Good call.. I've scrolled through the various properties and never even noticed that one.

    Thanks!

    Sean
  6. ChrisWalker
    ChrisWalker avatar
    21 posts
    Member since:
    May 2007

    Posted 01 Aug 2008 Link to this post

    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();
        }
    }


  7. Xcalibur
    Xcalibur avatar
    11 posts
    Member since:
    Jul 2007

    Posted 03 Aug 2008 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017