Wednesday 8 October 2014

GridView Bind Using with DataReader in Asp.Net C#

GRIDVIEW BIND

   

              

Html Coding


  <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr><td>
            <asp:Button ID="Button1" runat="server" Text="Select Register Table" OnClick="Button1_Click" />
            </td></tr>
        <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email ID">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("email") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Mobile Number">
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="UserName">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Eval("username") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Password">
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Eval("password") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            &nbsp;</td></tr>
    </table>
    </div>
    </form>
</body>
</html>  

         
 C# Coding


   protected void Button1_Click(object sender, EventArgs e)
    {

        dbcon();
        query = "select * from register";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count>0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
                   ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                   GridView1.DataSource = ds;
                   GridView1.DataBind();
                   int columncount = GridView1.Rows[0].Cells.Count;
                   GridView1.Rows[0].Cells.Clear();
                   GridView1.Rows[0].Cells.Add(new TableCell());
                   GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                   GridView1. Rows[0].Cells[0].Text = "No Records Found";
              }

        }


                                 
                                   




                        
            



                         
  
                                 
                                      
                                                      




       
 

        


             
                                 
                                           


                                        





                                                                     i think useful this post.

0 comments:

Post a Comment