GridView Row Number Increment Automatically in Asp.Net C#

GridView Row Number Increment Automatically

Gridview Row Number Increment Automatically  Run Time Without Using DataBase Table  but Add DataItemIndex  Automatically Add in Asp.Net C#.



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

<!DOCTYPE html>

<html xmlns="">
<title> </title>
    <form id="form1" runat="server">
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />

<table><tr><td> </td><td>   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                 <AlternatingRowStyle BackColor="#9999FF" />
                     <asp:TemplateField HeaderText="SNO">

               <%# Container.DataItemIndex + 1 %>

                     <asp:BoundField DataField="name" HeaderText="Name" />
                     <asp:BoundField DataField="country" HeaderText="Country" />
                 <HeaderStyle BackColor="#FF3300" />

                             <RowStyle BackColor="#FF99FF" />           

                      C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

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


    protected void Button1_Click(object sender, EventArgs e)
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());

        SqlCommand cmd = new SqlCommand("select * from country",con);           

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        GridView1.DataSource = ds;


First  - Add New WebForm -  Add DataBase Table Values

Next - Add GridView  & Button From ToolBox

Next - GridView - Edit Column - Add Template Field - Change HeaderText

Next - GridView - Edit Column - Add Bound Field - Change HeaderText & DataField 

Next - GridView - Edit Column - Add Bound Field - Change HeaderText & DataField 

Next - Go - To Source - DataItemIndex Increment Add  in ItemTemplate of SNO

Next - Select Command For All Record

Next - Run[F5] The Program - Automatically Increment Row Number in GridView


Post a Comment