how to give serial no in gridview in asp.net C#

You can get serial no column to only add this code:-


                <asp:TemplateField HeaderText="Serial No.">
                    <ItemTemplate>
                        <asp:Label ID="lbserial" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

For this code, we do not need to any database.

For more understanding, I am giving an example.if any problem then feel to ask with me.

Database (SQL Server):-





Script for creating above table:-

USE [uploaddata]
GO

/****** Object:  Table [dbo].[imgtab]    Script Date: 04/07/2014 16:14:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[imgtab](
          [id] [int] IDENTITY(1,1) NOT NULL,
          [Name] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_imgtab] PRIMARY KEY CLUSTERED
(
          [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO




Source Code:-


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

<!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>
        <asp:FileUpload ID="Fileupload" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />
        <asp:GridView ID="gdfile" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
            GridLines="Horizontal">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
            <Columns>
                <asp:TemplateField HeaderText="Serial No.">
                    <ItemTemplate>
                        <asp:Label ID="lbserial" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lbname" runat="server" Text='<%#Eval("Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Code behind (c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class serialnoingridview : System.Web.UI.Page
{
    string str = "Data Source=NEERAJ-PC; Initial Catalog =uploaddata ;User ID=sa; Password=12345678";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
    }

    private void BindGridviewData()
    {
        using (SqlConnection con = new SqlConnection(str))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select * from imgtab";
                cmd.Connection = con;
                con.Open();
                gdfile.DataSource = cmd.ExecuteReader();
                gdfile.DataBind();
                con.Close();
            }
        }
    }

    protected void btnupload_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(Fileupload.PostedFile.FileName);
        Stream stream = Fileupload.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(stream);
        Byte[] size = br.ReadBytes((int)stream.Length);
        using (SqlConnection con = new SqlConnection(str))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "insert into imgtab (Name) values(@Name)";
                cmd.Parameters.AddWithValue("@Name", filename);
               
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                BindGridviewData();
            }
        }
    }
}

Out-put:-
how to give serial no in gridview in asp.net C# how to give serial no in gridview  in asp.net C# Reviewed by NEERAJ SRIVASTAVA on 4:21:00 PM Rating: 5

No comments:

Powered by Blogger.