GridView Show Header

A

ABHIJIT B

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
 
L

Larry Bud

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.
 
M

Mark Moeykens

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
 
A

ABHIJIT B

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

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 said:
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 -
 
M

Mark Moeykens

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 said:
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

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 said:
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 -
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top