Please visit my new Web Site https://coderstechzone.com
In many Asp.net forums i found the question "How to generate a column as serial number at runtime of a gridview?". Also they have another concern was paging. Thats why here i want to share a simple code snippet with all. Which will help you to add a autogenerate number or sequential number or serial number in your GridView with less effort. Mind it the serial number will not come from Database. We will generate automatically in runtime or in design time. No extra server side code is required.
Fig: Output
To do that just add a GridView control in your aspx page. Then create a TemplateField column in the GridView and add Container.DisplayIndex+1 as inline code. Will generate your required output. Here i will show you step by step code. Hope you don't need to write a line of code. Just copy my codes step by step. After that run your project.
GridView HTML Code:
Fig: Output
To do that just add a GridView control in your aspx page. Then create a TemplateField column in the GridView and add Container.DisplayIndex+1 as inline code. Will generate your required output. Here i will show you step by step code. Hope you don't need to write a line of code. Just copy my codes step by step. After that run your project.
GridView HTML Code:
<b>List of suppliers:</b>
<asp:GridView runat="server" ID="gvEdit">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%# Container.DisplayIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Code">
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name">
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address">
</asp:BoundField>
<asp:BoundField DataField="Contact" HeaderText="Contact no">
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:GridView runat="server" ID="gvEdit">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%# Container.DisplayIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Code">
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name">
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address">
</asp:BoundField>
<asp:BoundField DataField="Contact" HeaderText="Contact no">
</asp:BoundField>
</Columns>
</asp:GridView>
Populate or Bind GridView with sample Data:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dtSupplier = new DataTable("Supplier");
dtSupplier.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
dtSupplier.Columns.Add(new DataColumn("Code"));
dtSupplier.Columns.Add(new DataColumn("Name"));
dtSupplier.Columns.Add(new DataColumn("Address"));
dtSupplier.Columns.Add(new DataColumn("Contact"));
dtSupplier.Rows.Add(1, "st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
dtSupplier.Rows.Add(2, "ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
dtSupplier.Rows.Add(3, "cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
dtSupplier.Rows.Add(4, "er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
dtSupplier.Rows.Add(5, "bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");
gvEdit.DataSource = dtSupplier;
gvEdit.DataBind();
}
}
{
if (!IsPostBack)
{
DataTable dtSupplier = new DataTable("Supplier");
dtSupplier.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
dtSupplier.Columns.Add(new DataColumn("Code"));
dtSupplier.Columns.Add(new DataColumn("Name"));
dtSupplier.Columns.Add(new DataColumn("Address"));
dtSupplier.Columns.Add(new DataColumn("Contact"));
dtSupplier.Rows.Add(1, "st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
dtSupplier.Rows.Add(2, "ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
dtSupplier.Rows.Add(3, "cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
dtSupplier.Rows.Add(4, "er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
dtSupplier.Rows.Add(5, "bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");
gvEdit.DataSource = dtSupplier;
gvEdit.DataBind();
}
}
Now run your application. Hope you will see the serial number as a first column in your aspx page. No Database connectivity required here to ease the example. Also you can visit GridView & Asp.Net labels to read more.
3 comments:
Dear Sir,
I struggled for about 3 hours searching for the answer to the "serial number" issue addressed here. Worked like a charm. Thank you very much.
How can the serial number will be auto generated your are only providing this is gidview column.
pls any help..
Sorry mr. noor i didn't get your requirement. plz clarify.
Thanks.
I WOULD BE DELIGHTED TO HEAR FROM YOU