Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--Scenario

When there is no data in the gridview then the message is displayed but ...but the message is displayed within a table/panel.

But i dont want that inside a panel/table...

code is as below

XML
<div class="searchResult">

        <asp:UpdatePanel ID="UpdatePanel12" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView ID="gridShuttleAdmin" runat="server" EmptyDataText="No Records Found..."
                    AutoGenerateColumns="false" DataKeyNames="Shuttle_Facility_Id" OnRowDataBound="gridShuttleAdmin_RowDataBound"
                    OnRowDeleting="gridShuttleAdmin_RowDeleting" OnRowEditing="gridShuttleAdmin_RowEditing"
                    OnRowUpdating="gridShuttleAdmin_RowUpdating" OnRowCancelingEdit="gridShuttleAdmin_RowCancelingEdit">
                    <SelectedRowStyle CssClass="ResultSelectedRow" BackColor="Cyan" />
                    <RowStyle CssClass="ResultRow" BackColor="#EFF3FB" />
                    <PagerStyle BackColor="#999999" ForeColor="AliceBlue" HorizontalAlign="Center" />
                    <HeaderStyle CssClass="searchResultHeader" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="Bisque" />
                    <Columns>
                        <asp:TemplateField HeaderText="Trip Number">
                            <ItemTemplate>
                                <asp:Label ID="lbltrip" runat="server" Text='<%#Eval("Trip_num") %>'></asp:Label>
                                <asp:Label ID="lblshuttlefacilityid" runat="server" Visible="false" Text='<%#Eval("Shuttle_Facility_Id") %>'></asp:Label>
                                <asp:Label ID="lblfacilityid" runat="server" Visible="false" Text='<%#Eval("facility_id") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtshuttletrip" ReadOnly="true" CssClass="textBox" runat="server"
                                    Text='<%#Bind("Trip_num") %>'> </asp:TextBox>
                                <asp:Label ID="lblshuttlefacilityid" runat="server" Visible="false" Text='<%#Eval("Shuttle_Facility_Id") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Shuttle Detail Name">
                            <ItemTemplate>
                                <asp:Label ID="lblshuttledetailname" runat="server" Text='<%#Eval("Shuttle_Facility_DetailName") %>'>
                                </asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                            <EditItemTemplate>
                                <asp:TextBox ID="txtfacilityname" Width="250px" CssClass="textBox" runat="server"
                                    Text='<%#Bind("Shuttle_Facility_DetailName") %>'> </asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Seats Avaliable">
                            <ItemTemplate>
                                <asp:Label ID="lblseats" runat="server" Text='<%#Eval("Shuttle_Seats") %>'>
                                </asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                            <EditItemTemplate>
                                <asp:TextBox ID="txtshuttleseats" CssClass="textBox" runat="server" Text='<%#Bind("Shuttle_Seats") %>'> </asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Shuttle Number">
                            <ItemTemplate>
                                <asp:Label ID="lblshuttlenum" runat="server" Text='<%#Eval("Shuttle_Number") %>'>
                                </asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                            <EditItemTemplate>
                                <asp:TextBox ID="txtshuttlenumber" CssClass="textBox" runat="server" Text='<%#Bind("Shuttle_Number") %>'> </asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Add Shuttle Details">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkviewID" ForeColor="ActiveCaption" Text="Add/Edit" CommandName="View"
                                    runat="server"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Edit">
                            <ItemTemplate>
                                <asp:LinkButton ID="linkedit" ForeColor="ActiveCaption" runat="server" Text="Edit"
                                    CommandName="edit" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="lnkupdate" runat="server" Text="update" CommandName="update" />
                                <asp:LinkButton ID="lnkcancel" runat="server" Text="cancel" CommandName="cancel" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Delete">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkDeleteID" ForeColor="ActiveCaption" Text="Delete" CommandName="Delete"
                                    OnClientClick="return confirm('Are you sure to delete?')" runat="server"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="center" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
Posted
Updated 22-Nov-13 0:18am
v3

Ok So this is the real Problem
try this
C#
// method for showing the Header and Footer of GridView when no data returned in the query
// You may not use it in that case no header and footer will be shown 
private void ShowNoResultFound(DataTable source, GridView gv)
    {
        source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
        // Bind the DataTable which contain a blank row to the GridView
        gv.DataSource = source;
        gv.DataBind();
        // Get the total number of columns in the GridView to know what the Column Span should be
        int columnsCount = gv.Columns.Count;
        gv.Rows[0].Cells.Clear();// clear all the cells in the row
        gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
        gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell

        //You can set the styles here
        gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
        gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
        gv.Rows[0].Cells[0].Font.Bold = true;
        //set No Results found to the new added cell
        gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
    }

// now you have to call this method when DataSource returns nothing.
private void BindGridView()
{ 
        DataTable dt = new DataTable(string user);
        SqlConnection connection = new SqlConnection(GetConnectionString()); 
        try
        {
            connection.Open();
            string sqlStatement = "SELECT* FROM Orders WHERE UserID = @User";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            sqlCmd.Parameters.AddWithValue("@User", user);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0) //Check if DataTable returns data
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            Else //else if no data returned from the DataTable then 
            {    //call the method ShowNoResultFound()
                ShowNoResultFound(dt,GridView1);
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
}
 
Share this answer
 
Comments
Alberto T. Simon 31-Jan-15 16:04pm    
There is a little problem with this solution (the best of the three, IMHO): when the dataset has a no nulllable column it fails when you add a new empty row.
Vishal Pand3y 2-Feb-15 5:41am    
you have to just instantiate it :)
add this in your client side code
C#
<EmptyDataTemplate>
Data not available
</EmptyDataTemplate>

For more details see this GridView.EmptyDataTemplate Property
 
Share this answer
 
v2
Comments
anurag19289 22-Nov-13 7:08am    
Getting this error :

System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'emptydatatemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
anurag19289 22-Nov-13 7:13am    
ohh sorry i was placing it inside column


getting the result now...but the "Data Not avaliable" is coming inside a panel.

How to avoid that
Vishal Pand3y 22-Nov-13 10:54am    
insode a panel means what it will be shown in the grid
anurag19289 22-Nov-13 15:48pm    
means once i search ..if no data is there then it should display as :

No Records found..

but in my case i m getting it as below inside a panel:

-------------------------------------------------------------------
No records found
-------------------------------------------------------------------


Is it possible to avoid that ...or not possible....
Vishal Pand3y 24-Nov-13 11:27am    
Of Course anurag it is possible i would suggest if no data returned from the DataTable then hide the gridview and show message in label
use this

<emptydatatemplate>No Records Found!!!</emptydatatemplate>
 
Share this answer
 
v2
Comments
anurag19289 22-Nov-13 7:08am    
error :
System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'emptydatatemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
anurag19289 22-Nov-13 7:14am    
ohh sorry i was placing it inside column


getting the result ...but the "No Records found" is coming inside a panel.

How to avoid that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900