locked
Message No record found RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below gridview . I want if it is empty then it should display "No record Found"

    $(document).ready(function () {
                $('#gvwProductCategories').dataTable({
                    order: [],
                    columnDefs: [ { orderable: false, targets: [2] } ]
                });
            });
    
    <asp:GridView ID="gvwProductCategories" runat="server" 
                    CssClass="table table-bordered table-striped" AutoGenerateColumns="false"                 HeaderStyle-CssClass="GridHeader" ClientIDMode="Static"
                    EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblCode" Text='<%#Eval("Code") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblDescription" Text='<%#Eval("Description") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    </asp:GridView>

    Thanks

    Monday, September 10, 2018 5:26 AM

All replies

  • User-1716253493 posted

    What the problem?

    You can also use emptydatatemplate to design empty message

    Monday, September 10, 2018 7:21 AM
  • User839733648 posted

    Hi JagjitSingh,

    According to your description and code, I’ve made a sample on my side.

    But I’m sorry that I could not reproduce your issue. The code works well.

    When there is no message, it shows “No Records Found!”

    the result:

    For more details, you could refer to the code below.

    .aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
        <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function () {
                $("<thead class='bg-primary text-white'></thead>").append($("#gvw tr:first")).prependTo($("#gvw"));
                $("#gvw").DataTable({
                    order: [],
                    columnDefs: [ { orderable: false, targets: [2] } ]
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="gvw" runat="server" CssClass="table table-bordered table-striped" AutoGenerateColumns="false"
                    HeaderStyle-CssClass="GridHeader" ClientIDMode="Static"
                    EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty">
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        <asp:TemplateField HeaderText="Eid">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Eid") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle Font-Underline="True" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Ename">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Ename") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

    code behind.

    Code behind.
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    string constr = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT Eid,Ename FROM tb_testEmpty", con))
                        {
                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                            {
                                cmd.CommandType = CommandType.Text;
                                DataTable dt = new DataTable();
                                sda.Fill(dt);
                                gvw.DataSource = dt;
                                gvw.DataBind();
                            }
                        }
                    }
                }
            }
    

    Best Regards,

    Jenifer

    Tuesday, September 11, 2018 6:06 AM