Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Phagu Mahato
More...


Resources » Code Snippets » ASP.NET GridView

How to generate the row number in gridview using asp.net


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 5


How to generate the row number in gridview using asp.net



 


How to generate the row number in grid view using asp.net
Abstract
This is the common requirement to show the renumber in grid view so that the user come to know how many records the grid view have.
Example :-
Here i will give one example how to generate the row number in grid view using vb.net.Just Drag and drop the one grid view and button control to your asp.net.
Add one button control to your asp.net page to add the values to grid view control.
In Run time if your give any value in text box,that value will added in grid view with serial or record no.
Code Behind File:-

Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim dtrow As DataRow
Dim dstDataSet As DataSet
Public Shared Function GetFieldType(ByVal FieldType As String) As System.Type
Select Case FieldType
Case "int"
Return Type.GetType("System.Int32")
Case "varchar"
Return Type.GetType("System.String")
Case "datetime"
Return Type.GetType("System.DateTime")
Case "float"
Return Type.GetType("System.Double")
End Select
Return Type.GetType("System.Object")
End Function

Public Shared Function DefineDataset(ByVal Field() As String, ByVal FieldType() As String, ByVal PrimaryKey As String) As DataSet
Dim dstDataset As New DataSet
Dim dtTable As New DataTable
Dim i As Integer
For i = 0 To FieldType.Length - 1
dtTable.Columns.Add(New DataColumn(Field(i), GetFieldType(FieldType(i))))
If Field(i) = PrimaryKey Then
dtTable.Constraints.Add("c" & i, dtTable.Columns(i), True)
End If
Next
dstDataset.Tables.Add(dtTable)
Return (dstDataset)
End Function
Public Property GridBindDataSet() As DataSet
Get
If Not (ViewState("GridDataset") Is Nothing) Then
dstDataSet = CType(ViewState("GridDataset"), DataSet)
End If
If dstDataSet Is Nothing Then
dstDataSet = DefineDataset(New String() {"EmpID", "EmpName"}, New String() {"varchar", "varchar"}, "")
ViewState.Add("GridDataset", dstDataSet)
End If
Return dstDataSet
End Get

Set(ByVal value As DataSet)
dstDataSet = value
If Not (ViewState("GridDataset") Is Nothing) Then
ViewState("GridDataset") = dstDataSet
Else
ViewState.Add("GridDataset", dstDataSet)
End If
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim drwRow As DataRow = GridBindDataSet.Tables(0).NewRow()
drwRow("EmpID") = ""
drwRow("EmpName") = ""
GridBindDataSet.Tables(0).Rows.Add(drwRow)
GridView1.DataSource = GridBindDataSet
GridView1.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If GridBindDataSet.Tables(0).Rows(0)("EmpID") = "" Then
GridBindDataSet.Tables(0).Rows(0)("EmpID") = txtEmpID.Text.ToString
GridBindDataSet.Tables(0).Rows(0)("EmpName") = txtEmpName.Text.ToString
Else
Dim drwRow As DataRow = GridBindDataSet.Tables(0).NewRow()
drwRow("EmpID") = txtEmpID.Text.ToString
drwRow("EmpName") = txtEmpName.Text.ToString()
GridBindDataSet.Tables(0).Rows.Add(drwRow)
End If
GridView1.DataSource = GridBindDataSet
GridView1.DataBind()
End Sub
End Class

Aspx File:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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">
<div>
Empoyee ID :<asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
Empoyee Name :<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Employee ID" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblEmployeeID" runat="server" EnableTheming="false" Text='<%# Bind("EmpID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Employee Name" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblEmployeeName" runat="server" EnableTheming="false" Text='<%# Bind("EmpName")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

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






Did you like this resource? Share it with your friends and show your love!


Responses to "How to generate the row number in gridview using asp.net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Select a row without using Select Command
    Previous Resource: Search and Highlight Results
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to generate the row number in gridview using asp.net  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.