Tuesday, 7 August 2012

How to Create Serial Number in Gridview First Column


How  to Create Serial Number in Gridview









<%@ Page Language="C#" AutoEventWireup="true" CodeFile="stylishgridview.aspx.cs" Inherits="stylishgridview" %>


<!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>Untitled Page</title>
   
   
  
  
    </head>
<body >
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
   
    <asp:GridView ID="grd1" runat ="server" Width="600px" Height ="600px" ShowHeader="true"
    ShowFooter="true" AutoGenerateColumns ="false" AllowPaging="true"
            onpageindexchanging="grd1_PageIndexChanging"
            onrowcreated="grd1_RowCreated" Font-Bold="True" Font-Size="Medium"
            ForeColor="Black"  AllowSorting="true"
        OnRowDataBound  ="grd1_RowDataBound" Font-Names="Verdna" 
            >
    <Columns >
    <asp:TemplateField >
    <HeaderStyle  ForeColor="Blue" Font-Bold ="true"  />
    <HeaderTemplate >
    Sr. No
   
    </HeaderTemplate>
   
    <ItemTemplate>
    <div style="width :100%;">
    <asp:Label ID="srno" runat ="server" Text='<%# Container.DataItemIndex +1 %>' />
    </div>
    </ItemTemplate>
   
   
    </asp:TemplateField>


<asp:TemplateField >
     <HeaderStyle  ForeColor="Blue" Font-Bold ="true" />
    <HeaderTemplate>
    Roll
   
    </HeaderTemplate>
    <EditItemTemplate>
  
    <asp:TextBox ID="txtrollno" runat ="server" Text='<%# Bind("Roll") %>'  />
  
    </EditItemTemplate>
    <ItemTemplate>
  
    <asp:Label  ID="lblrollno" runat ="server" Text='<%# Bind("Roll") %>'  />
  
    </ItemTemplate>
   
   
    </asp:TemplateField>
    

    </Columns>
        <PagerStyle BackColor="#3366CC" ForeColor="#000099" />
        <HeaderStyle BackColor="#9966FF" />
    </asp:GridView>
    <div>
    </div>
   
    </form>
   
</body>
</html>
C# Code

string strcon = "Data Source=FWSI-C25\\SqlExpress;Initial Catalog=Employee;Integrated Security=True";
    SqlConnection con;
  static   DataSet ds;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        da = new SqlDataAdapter("select * from stu_info", strcon);
        ds = new DataSet();
        da.Fill(ds);
        grd1.DataSource = ds.Tables[0];
        grd1.DataBind();
    }

0 comments: