1.11M Members

Gridview display for an empty dataset

 
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

 
0
 

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

 
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

 
0
 

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

 
0
 

spot on nikkiH

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

 
0
 

Dear,

zneweb and mail2saion have solved your problem.

<asp:GridView ID="GridView1" runat="server">
            <EmptyDataTemplate>
                There is no data.
            </EmptyDataTemplate>
        </asp:GridView>
 
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

 
-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

 
0
 

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

 
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.

 
0
 

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

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article