954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Gridview display for an empty dataset

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

Sarah Lee
Light Poster
45 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

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

sedgey
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

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

sedgey
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

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

Sarah Lee
Light Poster
45 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 13
Solved Threads: 4
 

spot on nikkiH

sedgey
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

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>
znetweb
Newbie Poster
17 posts since May 2008
Reputation Points: 10
Solved Threads: 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.

mail2saion
Posting Whiz in Training
247 posts since Apr 2009
Reputation Points: 26
Solved Threads: 44
 

Dear,

zneweb and mail2saion have solved your problem.

<asp:GridView ID="GridView1" runat="server">
            <EmptyDataTemplate>
                There is no data.
            </EmptyDataTemplate>
        </asp:GridView>
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

argisoft
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

Merage
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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

ajeetkrpra
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 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.

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

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

kurtisnot
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 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

egrullard
Newbie Poster
4 posts since Apr 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You