How to Dynamically Bind A Gridview From Database in ASP.NET with C#- Example

Introduction:-

In this post I will explain how to dynamically bind gridview in asp.net.

Usually we use the SQLDataSource to bind a Gridview but if we have requirement to bind Gridview dynamically than this code is usefull.

Here I am using ItemTempletField so I have used labels to display data in GridView for binding data as label text, I am using  <%Eval(“TableFieldName”)%>. It is a simple script function to get the data from DataSource of gridview.

Here I am using SQLDataSet to carry data which selected from Database using SqlDataAdapter.

Source Code(in asp.net):-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server" >
<div>
            <asp:gridview id="GridView2" runat="server" autogeneratecolumns="False"
                style="margin-right: 8px; margin-left: 7px;">           
                <Columns>
                <asp:TemplateField HeaderText="Image">
                    <ItemTemplate>
                        <asp:ImageButton ID="Image2" runat="server" ImageUrl='<%#Eval("img")%>' Width="100px"
                            Height="100px" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblnm" runat="server" Text='<%#Eval("product")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:Label ID="lblprice" runat="server" Text='<%#Eval("price")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Qty">
                    <ItemTemplate>
                        <asp:Label ID="lblqty" runat="server" Text='<% # Eval("qty") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tqty" runat="server" Width="20px" Text='<% # Eval("qty") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lbltot" runat="server" Text='<%#Eval("total")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:LinkButton ID="linkupdate" runat="server" CommandName="update" CommandArgument='<% # Eval("pid") %>'>Update</asp:LinkButton>
                        <asp:LinkButton ID="linkcancle" runat="server" CommandName="cancle" CommandArgument='<% # Eval("pid") %>'>Cancle</asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="linkdelete" runat="server" CommandName="delete">Remove</asp:LinkButton>
                        <asp:LinkButton ID="linkedit" runat="server" CommandName="Edit"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:gridview>
        </div>
    </form>
</body>
</html>

Code(In C#):-

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Your Connection String");
        con.Open();
        try
        {
          SqlCommand cmd = new SqlCommand("your select Query", con);
           SqlDataAdapter sd = new SqlDataAdapter();
           DataSet ds = new DataSet();
           sd.SelectCommand = cmd;
            sd.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception err)
        {
            //TODO
        }
        finally
        {
            con.Close();
        }

    }

3 comments

Hi,

I use your code, but I have a problem. What I do wrong ?



Line 36:
Line 37: <
Line 38:
Line 39:
Line 40:

Reply

include this librarys.

using System.Data;
using System.Data.SqlClient;

i hope it will work, and please copy source code again i was removed unnecessary gridview events from aspx page.

Thanks for comment.
If this will not work then fill free to comment me and also metion in which page you getting error C# file or aspx file.

Reply

Post a Comment