Display auto serial number in DataGrid in ASP.Net

adnanali
 
on Sep 21, 2018 10:49 PM
Sample_205396.zip
4041 Views

Hello,

I want to show Serial Number in ASP.Net DataGrid but I haven't Serial Number a column in SQL Table so How I can generate Auto Serial Number and show in ASP.Net DataGrid?? I am using #vb.

 

Thanks.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Andrea
 
on Sep 21, 2018 10:49 PM
Hi @adnanali,
Please try the following

Auto generate Serial Number in HTML Table using jQuery

It might help you.

Cheers Andrea.
pandeyism
 
on Sep 23, 2018 11:58 PM

I am checking it, I will get back to you soon.

pandeyism
 
on Sep 24, 2018 01:02 AM
on Sep 24, 2018 01:48 AM

Hi adnanali,

If you are using GridView refer below article.

Add (Show) Serial Number in First Column of GridView in ASP.Net using C# and VB.Net

If you are using DataGrid please refer below sample.

HTML

<asp:DataGrid ID="dgCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundColumn DataField="RowNumber" HeaderText="Sr. Number"></asp:BoundColumn>
        <asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
        <asp:BoundColumn DataField="Country" HeaderText="Country"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>

Namespaces

C#

using System;
using System.Data.SqlClient;
using System.Data;

VB.Net

Imports System
Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
private void BindGrid()
{
    string ConnectionString = @"Server=.\SQL2005;DataBase=Test;UID=user;PWD=password";
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT ROW_NUMBER() OVER(ORDER BY CustomerId) AS RowNumber,* FROM Customers", con))
        {
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            this.dgCustomers.DataSource = dt;
            this.dgCustomers.DataBind();
        }
    }
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.IsPostBack Then
            Me.BindGrid()
        End If
    End Sub

    Private Sub BindGrid()
        Dim ConnectionString As String = "Server=.\SQL2005;DataBase=Test;UID=user;PWD=password"

        Using con As SqlConnection = New SqlConnection(ConnectionString)

            Using cmd As SqlCommand = New SqlCommand("SELECT ROW_NUMBER() OVER(ORDER BY CustomerId) AS RowNumber,* FROM Customers", con)
                Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                da.Fill(dt)
                Me.dgCustomers.DataSource = dt
                Me.dgCustomers.DataBind()
            End Using
        End Using
    End Sub

Screenshot