Вы находитесь на странице: 1из 11

1) Simple Grid View Example

SimpleGV_Example.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleGV_Example.aspx.cs" Inherits="SimpleGV_Example" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div style="height:300px;width:200px; border:10px solid Black;padding:100px;borderradius:25px;"> <asp:Label ID="lblmsg" Text=" Grid View Simple Example " runat="server" /> <br /> <asp:GridView ID="GVSimple" runat="server"> </asp:GridView> </div> </form> </body> </html>

SimpleGV_Example.aspx.cs (Page Behind Code) using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data; System.Data.SqlClient; System.Web.Configuration;

public partial class SimpleGV_Example : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string str = WebConfigurationManager.AppSettings["constring"].ToString(); SqlConnection con = new SqlConnection(str); SqlDataAdapter da = new SqlDataAdapter("select * from Cars",con); DataSet ds = new DataSet(); da.Fill(ds,"car"); GVSimple.DataSource = ds.Tables["car"]; GVSimple.DataBind(); } }

Web.config File <?xml version="1.0"?> <!-For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <appSettings> <add key="constring" value="Data Source=.;Initial Catalog=SampleDB;Integrated Security=True"/> </appSettings> <system.web> <compilation debug="false" targetFramework="4.0" /> </system.web> </configuration>

Out Put:

2) Adding Colors (Auto Format option) for the grid view

SimpleGV_Example.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleGV_Example.aspx.cs" Inherits="SimpleGV_Example" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div style="height:300px;width:200px; border:10px solid Black;padding:100px;borderradius:25px;"> <asp:Label ID="lblmsg" Text=" Grid View Simple Example " runat="server" /> <br /> <asp:GridView ID="GVSimple" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"> <AlternatingRowStyle BackColor="#CCCCCC" /> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" />

</asp:GridView> </div> </form> </body> </html>

SimpleGV_Example.aspx.cs using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data; System.Data.SqlClient; System.Web.Configuration;

public partial class SimpleGV_Example : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string str = WebConfigurationManager.AppSettings["constring"].ToString(); SqlConnection con = new SqlConnection(str); SqlDataAdapter da = new SqlDataAdapter("select * from Cars",con); DataSet ds = new DataSet(); da.Fill(ds,"car"); GVSimple.DataSource = ds.Tables["car"]; GVSimple.DataBind(); } } OutPut

3) Using Data Source to Fetch Data From Database

SimpleGV_UsingDataSource.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleGV_UsingDataSource.aspx.cs" Inherits="SimpleGV_UsingDataSource" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <div style="height:300px;width:200px; border:10px solid Black;padding:100px;borderradius:25px;"> <asp:Label ID="lblmsg" Text=" Grid View Simple Example " runat="server" /> <br /> <asp:GridView ID="GVSimple" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"

ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False" DataKeyNames="CarId" DataSourceID="SqlDataSource1"> <AlternatingRowStyle BackColor="#CCCCCC" /> <Columns> <asp:BoundField DataField="CarId" HeaderText="CarId" InsertVisible="False" ReadOnly="True" SortExpression="CarId" /> <asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" /> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> </Columns> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SampleDBConnectionString %>" SelectCommand="SELECT * FROM [Cars]"></asp:SqlDataSource> </div> </div> </form> </body> </html>

SimpleGV_UsingDataSource.aspx.cs (No Need to write any thing in cs file) using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls;

public partial class SimpleGV_UsingDataSource : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } }

Output: