Thursday 23 January 2014

How to display table data in gridview (simple way)

09:15 By

Here i will explain about display table content in simple method using a gridview and  SqlDataSource .
read change gridview field color based on value in database and export gridview data in excel .

the design view is shown below :

and the aspx code shown below

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" 
                ForeColor="#333333" GridLines="None" Width="392px">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
                <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
            </Columns>
          <%-- Grid style--%>
            
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
   ConnectionString="<%$ ConnectionStrings:dreamthewebConnectionString %>" 
  SelectCommand="SELECT [id], [name], [email], [age], [phone] FROM [account]">
</asp:SqlDataSource>
Add connectionstring to web.config file 
 <connectionStrings>
        <add name="dreamthewebConnectionString "
 connectionString="Data Source=JISHNU\SQLEXPRESS;Initial Catalog=dreamtheweb;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
after complete and run the page we well get output shown below


0 comments:

Post a Comment