1,105,290 Community Members

Gridview display for an empty dataset

Member Avatar
Sarah Lee
Light Poster
45 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi Everybody

How can i display in the gridview.
"there is no data for this request" if the binded dataset is empty?

Can anyone give some ideas?

Thanks

Member Avatar
sedgey
Junior Poster
130 posts since Jan 2007
Reputation Points: 58 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

why not check whether the DataSet has any rows in code before binding then show/hide a label with that text?

Member Avatar
sedgey
Junior Poster
130 posts since Jan 2007
Reputation Points: 58 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

alternatively if it must appear in the grid view, why not build a data table with one column called result and add a row who's value is your text, then bind it to your grid view

Member Avatar
Sarah Lee
Light Poster
45 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks
First one seems to be more easy
I will try for that

Member Avatar
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 
Member Avatar
sedgey
Junior Poster
130 posts since Jan 2007
Reputation Points: 58 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

spot on nikkiH

Member Avatar
znetweb
Newbie Poster
17 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

If there is No Data in Gridview and we want to show a message that “No data available”.
We can do this by using the following tag in gridview.

<EmptyDataTemplate>

No Data Available

</EmptyDataTemplate>
Member Avatar
mail2saion
Posting Whiz in Training
247 posts since Apr 2009
Reputation Points: 14 [?]
Q&As Helped to Solve: 44 [?]
Skill Endorsements: 0 [?]
 
0
 

JUST ADD THE EmptyDataText PROPERTY IN THE FOLLOWING WAY:

<asp:GridView runat="server" ID="gvCaseDetail" Width="100%" 
    EmptyDataText="there is no data for this request"
......................................

YOU CAN ALSO RENDER HTMEL TEXT LIKE:

<asp:GridView runat="server" ID="gvCaseDetail" Width="100%" 
    EmptyDataText="<table cellspacing=1 cellpadding=4 rules=rows border=0 style=color:Black;font-family:Tahoma;font-size:1em;width:730px;border-collapse:collapse;><tr style=color:Black;background-color:#00BFFF;font-weight:bold;><th scope=col>Case ID</th><th align=center scope=col>Subject</th><th align=center scope=col>Status</th><th align=center scope=col>Date</th><th align=center scope=col>Action</th></tr></table>"
..............................

ONE THING YOU HAVE TO KEEP IN MIND THAT IF YOU DIDN'T BIND ANY DATASOURCE YOU WILL GET BLANK.

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Dear,

zneweb and mail2saion have solved your problem.

<asp:GridView ID="GridView1" runat="server">
            <EmptyDataTemplate>
                There is no data.
            </EmptyDataTemplate>
        </asp:GridView>
Member Avatar
argisoft
Newbie Poster
1 post since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

try with this. My english is poor ok?

Public Sub CheckEmptyGrid(ByVal grid As WebControls.GridView)

            If grid.Rows.Count = 0 And Not grid.DataSource Is Nothing Then
                Dim dt As Object = Nothing
                If grid.DataSource.GetType Is GetType(Data.DataSet) Then
                    dt = New System.Data.DataSet
                    dt = CType(grid.DataSource, System.Data.DataSet).Tables(0).Clone()
                ElseIf grid.DataSource.GetType Is GetType(Data.DataTable) Then
                    dt = New System.Data.DataTable
                    dt = CType(grid.DataSource, System.Data.DataTable).Clone()
                ElseIf grid.DataSource.GetType Is GetType(Data.DataView) Then
                    dt = New System.Data.DataView
                    dt = CType(grid.DataSource, System.Data.DataView).Table.Clone
                End If
                dt.Rows.Add(dt.NewRow())
                grid.DataSource = dt
                grid.DataBind()
                grid.Rows(0).Visible = False
                grid.Rows(0).Controls.Clear()
            End If
        End Sub

and the call

CheckEmptyGrid(Grid)

With this the headers appers ok

Member Avatar
Merage
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

thanks for the help. I've signed in here because of that :)..
I'm a beginner at asp.net and i'll need a lot of help

Member Avatar
ajeetkrpra
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

there is a empty template in grid view to show message when data not available you can use it

Member Avatar
vuyiswamb
Posting Whiz
316 posts since Mar 2007
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

try this

if (ds.Tables[0].Rows.Count > 0)
            {
                //bind the Data
            }
            else
            {
             lblMessage.Text = "No Data to Display";
            }

a gridview is not visible when there no data, so you need to add a label that will display the message.

Member Avatar
kurtisnot
Newbie Poster
1 post since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

There's a ShowHeaderWhenEmpty property on a GridView that shows the headers even if the datasource contains no rows.

Member Avatar
egrullard
Newbie Poster
4 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Everybody

How can i display in the gridview.
"there is no data for this request" if the binded dataset is empty?

Can anyone give some ideas?

Thanks

Hola,

Good morning


>How can i display in the gridview.
>
>"there is no data for this request" if the binded dataset is empty?
>
Can anyone give some ideas?
>
>
>why not check whether the DataSet has any rows in code before binding
>
>then show/hide a label with >that text?


Here's an example of how you can do

exmple

step 1 You add a method Returns an DataTable

Public Function FillGridView() As DataTable

        Dim dt As New DataTable
        Using cn As New SqlConnection("conection")

            Dim sql As String = "select * from tabla where campo =@valor"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@valor", valor)
            cn.Open()

            Dim da As New SqlDataAdapter(cmd)

            Try
                da.Fill(dt)
            Catch ex As Exception

            Finally
                cn.Close()
            End Try

        End Using

        Return dt

    End Function

step 2 Where we call the method FillGridView

Dim dts As DataTable
        dts = FillGridView()

        If dts.Rows.Count > 0 Then

            With Me.GridView1
                .DataSource = dts
                .DataBind()
            End With
        End If

greeting

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article