HOW TO BIND GRIDVIEW IN ASP.NET FROM DATABASE.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  ASP.Net 
 552  View(s)
Ratings:
Rate this:
i want to data bind  in gridview  using c# and asp.net . please help me if you know.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to bind Gridview in asp.net from database.

    The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. It is server control in asp.net.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"

    BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4">

    <Columns>

    <asp:TemplateField HeaderText="Emp_ID">

    <ItemTemplate>

    <%#Eval("Emp_ID") %>

    </ItemTemplate>

    <ControlStyle Width="100px"></ControlStyle>

    <HeaderStyle Width="100px" />

    <ItemStyle HorizontalAlign="Center" />

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Emp_Name">

    <ItemTemplate>

    <%#Eval("Emp_Name") %>

    </ItemTemplate>

    <ControlStyle Width="300px"></ControlStyle>

    <HeaderStyle Width="200px" />

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Emp_Sal">

    <ItemTemplate>

    <%#Eval("Emp_Sal") %>

    </ItemTemplate>

    <ControlStyle Width="200px"></ControlStyle>

    <HeaderStyle Width="200px" />

    </asp:TemplateField>

    </Columns>

    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

    <RowStyle BackColor="White" ForeColor="#330099" />

    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

    <SortedAscendingCellStyle BackColor="#FEFCEB" />

    <SortedAscendingHeaderStyle BackColor="#AF0101" />

    <SortedDescendingCellStyle BackColor="#F6F0C0" />

    <SortedDescendingHeaderStyle BackColor="#7E0000" />

    </asp:GridView>

     

    string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString();

    // connection string

    SqlConnection con = new SqlConnection(constr);

    con.Open();

     

    SqlCommand com = new SqlCommand("select *from Employee", con); // table name

    SqlDataAdapter da = new SqlDataAdapter(com);

    DataSet ds = new DataSet();

    da.Fill(ds); // fill dataset

    GridView1.DataSource = ds.Tables[0]; //set DataSource GridView

    GridView1.DataBind(); //binding GridView

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!