GridView Show Header

Discussion in 'ASP .Net' started by ABHIJIT B, Feb 6, 2008.

  1. ABHIJIT B

    ABHIJIT B Guest

    Hi,

    I am using GridView in my web form and binding DataTable in code
    behind file as given below.

    gvUsersList.DataSource = dtUserList;
    gvUsersList.DataBind();

    If no records exists in DataTable GridView should display Coulmn
    Header text.
    In my case I have enabled following property,

    gvUsersList.ShowHeader = true;

    It is not working so tried following code which works for DataGrid but
    not for GridView


    dtUserList.Columns.Add(new DataColumn("LoginID",
    typeof(string)));
    dtUserList.Columns.Add(new DataColumn("FirstName",
    typeof(string)));
    dtUserList.Columns.Add(new DataColumn("LastName",
    typeof(string)));

    gvUsersList.DataSource = dtUserList;
    gvUsersList.DataBind();
    gvUsersList.ShowHeader = true;


    Kindly help me where I am doing mistake or GridView property which
    needs to be set.

    Regards,
    Abhijit B
    ABHIJIT B, Feb 6, 2008
    #1
    1. Advertising

  2. ABHIJIT B

    Larry Bud Guest

    On Feb 6, 12:51 pm, ABHIJIT B <> wrote:
    > Hi,
    >
    > I am using GridView in my web form and binding DataTable in code
    > behind file as given below.
    >
    >  gvUsersList.DataSource = dtUserList;
    >  gvUsersList.DataBind();
    >
    > If no records exists in DataTable GridView should display Coulmn
    > Header text.
    > In my case I have enabled following property,
    >
    > gvUsersList.ShowHeader = true;
    >
    > It is not working so tried following code which works for DataGrid but
    > not for GridView
    >
    >                     dtUserList.Columns.Add(new DataColumn("LoginID",
    > typeof(string)));
    >                     dtUserList.Columns.Add(new DataColumn("FirstName",
    > typeof(string)));
    >                     dtUserList.Columns.Add(new DataColumn("LastName",
    > typeof(string)));
    >
    >                     gvUsersList.DataSource = dtUserList;
    >                     gvUsersList.DataBind();
    >                     gvUsersList.ShowHeader = true;
    >
    > Kindly help me where I am doing mistake or GridView property which
    > needs to be set.


    Unfortunately, that's how it works. A GridView won't show the header
    unless there is data.

    There is an EmptyDataText property which will display in the event
    there is no data.

    I haven't tried it, but your datasource could add an empty row of data
    in case there are no rows.
    Larry Bud, Feb 6, 2008
    #2
    1. Advertising

  3. I tried what Larry Bud wrote with the GridView.EmptyDataText property. It
    seems like it only shows just that text with no header. Probably used if you
    want to give the user feedback if no data returned. It looked like this:

    gvUsersList.EmptyDataText = "No Data Returned";
    gvUsersList.DataSource = dtUserList;
    gvUsersList.DataBind();

    and rendered out as:
    <table cellspacing="0" rules="all" border="1" id="gvUsersList"
    style="border-collapse:collapse;">
    <tr>
    <td>No Data Returned</td>
    </tr>
    </table>

    So I think you'll need to check for no rows and add an empty row:

    if (dtUserList.Rows.Count == 0)
    {
    dtUserList.Rows.Add(new object[] { "" });
    }

    gvUsersList.DataSource = dtUserList;
    gvUsersList.DataBind();

    So you will see the GridView show up with a blank row as well.

    Hope this helps,
    Mark Moeykens

    "ABHIJIT B" wrote:

    > Hi,
    >
    > I am using GridView in my web form and binding DataTable in code
    > behind file as given below.
    >
    > gvUsersList.DataSource = dtUserList;
    > gvUsersList.DataBind();
    >
    > If no records exists in DataTable GridView should display Coulmn
    > Header text.
    > In my case I have enabled following property,
    >
    > gvUsersList.ShowHeader = true;
    >
    > It is not working so tried following code which works for DataGrid but
    > not for GridView
    >
    >
    > dtUserList.Columns.Add(new DataColumn("LoginID",
    > typeof(string)));
    > dtUserList.Columns.Add(new DataColumn("FirstName",
    > typeof(string)));
    > dtUserList.Columns.Add(new DataColumn("LastName",
    > typeof(string)));
    >
    > gvUsersList.DataSource = dtUserList;
    > gvUsersList.DataBind();
    > gvUsersList.ShowHeader = true;
    >
    >
    > Kindly help me where I am doing mistake or GridView property which
    > needs to be set.
    >
    > Regards,
    > Abhijit B
    >
    Mark Moeykens, Feb 6, 2008
    #3
  4. ABHIJIT B

    ABHIJIT B Guest

    Hi Larry ,Mark,

    Thanks for reply.I agree with Mark by adding dummy row without any
    data we can show header text in GridView.The Problem in my code is
    that I am using checkbox as Template column.If I addd blank row it
    shows other fields blank but checkbox visible.

    Kindly need assistance from anyone.Is it possible to solve using AJAX.

    Regards,
    Abhijit B

    On Feb 6, 2:51 pm, Mark Moeykens
    <> wrote:
    > I tried what Larry Bud wrote with the GridView.EmptyDataText property. It
    > seems like it only shows just that text with no header.  Probably used if you
    > want to give the user feedback if no data returned.  It looked like this:
    >
    > gvUsersList.EmptyDataText = "No Data Returned";
    > gvUsersList.DataSource = dtUserList;
    > gvUsersList.DataBind();
    >
    > and rendered out as:
    > <table cellspacing="0" rules="all" border="1" id="gvUsersList"
    > style="border-collapse:collapse;">
    >     <tr>
    >         <td>No Data Returned</td>
    >     </tr>
    > </table>
    >
    > So I think you'll need to check for no rows and add an empty row:
    >
    > if (dtUserList.Rows.Count == 0)
    > {
    >     dtUserList.Rows.Add(new object[] { "" });
    >
    > }
    >
    > gvUsersList.DataSource = dtUserList;
    > gvUsersList.DataBind();
    >
    > So you will see the GridView show up with a blank row as well.
    >
    > Hope this helps,
    > Mark Moeykens
    >
    >
    >
    > "ABHIJITB" wrote:
    > > Hi,

    >
    > > I am using GridView in my web form and binding DataTable in code
    > > behind file as given below.

    >
    > >  gvUsersList.DataSource = dtUserList;
    > >  gvUsersList.DataBind();

    >
    > > If no records exists in DataTable GridView should display Coulmn
    > > Header text.
    > > In my case I have enabled following property,

    >
    > > gvUsersList.ShowHeader = true;

    >
    > > It is not working so tried following code which works for DataGrid but
    > > not for GridView

    >
    > >                     dtUserList.Columns.Add(new DataColumn("LoginID",
    > > typeof(string)));
    > >                     dtUserList.Columns.Add(new DataColumn("FirstName",
    > > typeof(string)));
    > >                     dtUserList.Columns.Add(new DataColumn("LastName",
    > > typeof(string)));

    >
    > >                     gvUsersList.DataSource = dtUserList;
    > >                     gvUsersList.DataBind();
    > >                     gvUsersList.ShowHeader = true;

    >
    > > Kindly help me where I am doing mistake or GridView property which
    > > needs to be set.

    >
    > > Regards,
    > >AbhijitB- Hide quoted text -

    >
    > - Show quoted text -
    ABHIJIT B, Feb 8, 2008
    #4
  5. Ok, here's what you can do with the column that contains the checkbox.
    Convert it to a template column. Then edit that column and give the checkbox
    a meaningful name, say for example, "myCheckbox". Now, you want to make that
    checkbox invisible if you added a blank row so you'll have to modify your
    code to look something like this:

    bool hideCheckBox = false;

    if (dtUserList.Rows.Count == 0)
    {
    dtUserList.Rows.Add(new object[] { "" });
    hideCheckBox = true;
    }

    gvUsersList.DataSource = dtUserList;
    gvUsersList.DataBind();

    ((CheckBox)gvUsersList.Rows(0).FindControl("myCheckbox")).Visible =
    hideCheckBox;

    This could do it.
    Mark Moeykens

    "ABHIJIT B" wrote:

    > Hi Larry ,Mark,
    >
    > Thanks for reply.I agree with Mark by adding dummy row without any
    > data we can show header text in GridView.The Problem in my code is
    > that I am using checkbox as Template column.If I addd blank row it
    > shows other fields blank but checkbox visible.
    >
    > Kindly need assistance from anyone.Is it possible to solve using AJAX.
    >
    > Regards,
    > Abhijit B
    >
    > On Feb 6, 2:51 pm, Mark Moeykens
    > <> wrote:
    > > I tried what Larry Bud wrote with the GridView.EmptyDataText property. It
    > > seems like it only shows just that text with no header. Probably used if you
    > > want to give the user feedback if no data returned. It looked like this:
    > >
    > > gvUsersList.EmptyDataText = "No Data Returned";
    > > gvUsersList.DataSource = dtUserList;
    > > gvUsersList.DataBind();
    > >
    > > and rendered out as:
    > > <table cellspacing="0" rules="all" border="1" id="gvUsersList"
    > > style="border-collapse:collapse;">
    > > <tr>
    > > <td>No Data Returned</td>
    > > </tr>
    > > </table>
    > >
    > > So I think you'll need to check for no rows and add an empty row:
    > >
    > > if (dtUserList.Rows.Count == 0)
    > > {
    > > dtUserList.Rows.Add(new object[] { "" });
    > >
    > > }
    > >
    > > gvUsersList.DataSource = dtUserList;
    > > gvUsersList.DataBind();
    > >
    > > So you will see the GridView show up with a blank row as well.
    > >
    > > Hope this helps,
    > > Mark Moeykens
    > >
    > >
    > >
    > > "ABHIJITB" wrote:
    > > > Hi,

    > >
    > > > I am using GridView in my web form and binding DataTable in code
    > > > behind file as given below.

    > >
    > > > gvUsersList.DataSource = dtUserList;
    > > > gvUsersList.DataBind();

    > >
    > > > If no records exists in DataTable GridView should display Coulmn
    > > > Header text.
    > > > In my case I have enabled following property,

    > >
    > > > gvUsersList.ShowHeader = true;

    > >
    > > > It is not working so tried following code which works for DataGrid but
    > > > not for GridView

    > >
    > > > dtUserList.Columns.Add(new DataColumn("LoginID",
    > > > typeof(string)));
    > > > dtUserList.Columns.Add(new DataColumn("FirstName",
    > > > typeof(string)));
    > > > dtUserList.Columns.Add(new DataColumn("LastName",
    > > > typeof(string)));

    > >
    > > > gvUsersList.DataSource = dtUserList;
    > > > gvUsersList.DataBind();
    > > > gvUsersList.ShowHeader = true;

    > >
    > > > Kindly help me where I am doing mistake or GridView property which
    > > > needs to be set.

    > >
    > > > Regards,
    > > >AbhijitB- Hide quoted text -

    > >
    > > - Show quoted text -

    >
    >
    Mark Moeykens, Feb 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt

    TO show or NOT to show

    Matt, May 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    382
  2. =?Utf-8?B?RGFiYmxlcg==?=

    show header when gridview is empty

    =?Utf-8?B?RGFiYmxlcg==?=, Mar 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    21,988
    thienle282
    Jan 13, 2012
  3. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,962
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  4. rote
    Replies:
    1
    Views:
    608
    Munna
    Jul 14, 2008
  5. mlt
    Replies:
    2
    Views:
    832
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page