How to Bind a GridView control programmatically ( Asp.net, C# )

 How to Bind a GridView control programmatically ( Asp.net, C# )
On .aspx page :

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:TemplateField HeaderText="col_01" SortExpression="col_01">
                <EditItemTemplate>
                    <asp:Label ID="txtcol_01" runat="server" Text='<%# Eval("col_01") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="txtcol_01" runat="server" Text='<%# Bind("col_01") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="col_02" SortExpression="col_02">
                <EditItemTemplate>
                    <asp:Label ID="txtcol_02" runat="server" Text='<%# Bind("col_02") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="txtcol_02" runat="server" Text='<%# Bind("col_02") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Namespace :

using System.Data.SqlClient;


C# Code :
  protected void datashow()
    {
     
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=your_db;Integrated Security=True; Min pool Size=1;Max Pool Size=600;Pooling=true;Connection Timeout=100 ");
        conn.Open();
        string strsql = "selct col_01,col_02 from Table_Name";
        SqlCommand cmd = new SqlCommand(strsql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
            this.Gridview1.Rows[0].Visible = false;
        }
        conn.Close();

    }
How to Bind a GridView control programmatically ( Asp.net, C# )  How to Bind a GridView control programmatically ( Asp.net, C# ) Reviewed by (C#) Csharp examples on 2:04 AM Rating: 5

No comments:

Facebook

Powered by Blogger.