Several ways to display the DataGrid control column ordinal in asp.net

Source: Internet
Author: User
Tags empty expression tostring visual studio
Asp.net|datagrid|datagrid Controls | Several ways to display the DataGrid control column ordinal in asp.net

Author: Zhengzo 2004-9-10

In the aps.net most of the binding control, the function of the DataGrid should be the most complete, but it does not provide a ready-made record number of functions, but we can use some of its parameters to get the serial number, the following see how to get and display ordinal value calculation method is as follows:

(1) in the background

Datagrid.currentpageindex * datagrid.pagesize + e.item.itemindex + 1

(2) at the front desk

Datagrid1.currentpageindex * datagrid1.pagesize + container.itemindex + 1

Description

E represents an instance of the System.Web.UI.WebControls.DataGridItemEventArgs parameter class;

DataGrid1 here represents an example of the foreground;

Datagrid.currentpageindex: Gets or sets the index of the currently displayed page;

Datagrid.pagesize: Gets or sets the number of items to display on a single page of the DataGrid control.



I've used 4 methods to display the serial number in the foreground, but it all revolves around the formula above.

(1) Use the itemcreated of the DataGrid to set the value, and the foreground cell can be a bound column or a template column (including an empty template);

(2) Use the ItemDataBound of the DataGrid to set the value, and the foreground cell can be a bound column or a template column (including an empty template);

(3) Directly binding the computation expression in the foreground;

(4) Writing a method evaluation expression in the background class inherits the call from the foreground page class.

Note: Set additional ordinal columns when fetching data in the database I think this is the worst way to do this without discussion.

The following is a column that gets the data for the Customers table of the Northwind database, as shown below:

Serial Number 1
Serial Number 2
Serial Number 3
Serial Number 4
Serial Number 5
CustomerID

51
51
51
51
51
Lonep

52
52
52
52
52
Magaa

53
53
53
53
53
Maisd

54
54
54
54
54
Merep

55
55
55
55
55
Morgk

56
56
56
56
56
Norts

57
57
57
57
57
OCEAN

58
58
58
58
58
Oldwo

59
59
59
59
59
Ottik

60
60
60
60
60
PARIS

1 2 3 4 5 6 7 8 9 10






Here is the webformpaging.aspx file code,

<%@ Page language= "C #" codebehind= "WebFormPaging.aspx.cs" autoeventwireup= "false" inherits= " Aspnetpaging.webform1 "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<title>WebForm1</title>

<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >

<meta content= "C #" Name= "Code_language" >

<meta content= "JavaScript" name= "vs_defaultClientScript" >

<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >

</HEAD>

<body>

<form id= "Form1" method= "POST" runat= "Server" >

<table id= "Table1" cellspacing= "1" cellpadding= "1" width= "-" align= "center" border= "1" >

<TR>

<td><asp:datagrid id= "DATAGRID1" runat= "Server" autogeneratecolumns= "False" width= "100%" True ">

<Columns>

<asp:boundcolumn headertext= "serial number 1" ></asp:BoundColumn>

<asp:templatecolumn headertext= "Serial number 2" ></asp:TemplateColumn>

<asp:templatecolumn headertext= "serial number 3" >

<ItemTemplate>

<asp:label id= "ItemIndex" runat= "Server" ></asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:templatecolumn headertext= "serial number 4" >

<ItemTemplate>

<%# (Datagrid1.currentpageindex * datagrid1.pagesize + container.itemindex + 1)%>

</ItemTemplate>

</asp:TemplateColumn>

<asp:templatecolumn headertext= "serial number 5" >

<ItemTemplate>

<%# Getrecordindex (Container.itemindex)%>

</ItemTemplate>

</asp:TemplateColumn>

<asp:boundcolumn datafield= "CustomerID" headertext= "CustomerID" ></asp:BoundColumn>

</Columns>

<pagerstyle mode= "NumericPages" ></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD></TD>

</TR>

<TR>

<TD></TD>

</TR>

</TABLE>

</form>

</body>

</HTML>



The background WebFormPaging.aspx.cs code is as follows:

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Web;

Using System.Web.SessionState;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.UI.HtmlControls;



Namespace Aspnetpaging

{

public class WebForm1:System.Web.UI.Page

{

private int recordCount = 0;



protected System.Web.UI.WebControls.DataGrid DataGrid1;



private void Page_Load (object sender, System.EventArgs e)

{

if (! Page.IsPostBack)

{

Datagriddatabind ();

}

}



Binding Data

private void Datagriddatabind ()

{

DataSet ds = Dataaccess.getcustomersdata ();

This. DataGrid1.DataSource = ds;

This. Datagrid1.databind ();

}







Code generated #region the Web forms Designer

Override protected void OnInit (EventArgs e)

{

InitializeComponent ();

Base. OnInit (e);

}



<summary>

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

</summary>

private void InitializeComponent ()

{

This. datagrid1.itemcreated + = new System.Web.UI.WebControls.DataGridItemEventHandler (this. datagrid1_itemcreated);

This. Datagrid1.pageindexchanged + = new System.Web.UI.WebControls.DataGridPageChangedEventHandler (this. datagrid1_pageindexchanged);

This. Datagrid1.itemdatabound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this. Datagrid1_itemdatabound);

This. Load + = new System.EventHandler (this. Page_Load);



}

#endregion



Page

private void Datagrid1_pageindexchanged (object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

Datagrid1.currentpageindex = E.newpageindex;

Datagriddatabind ();

}

Get current item

protected int Getrecordindex (int itemindex)

{

Return (Datagrid1.currentpageindex * datagrid1.pagesize + itemindex + 1);

}

private void Datagrid1_itemcreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

DataGrid dg = (DataGrid) sender;

if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)

{

E.item.cells[0]. Text = (DG. CurrentPageIndex * DG. PageSize + E.item.itemindex + 1). ToString ();

E.ITEM.CELLS[1]. Text = (DG. CurrentPageIndex * DG. PageSize + E.item.itemindex + 1). ToString ();

}

}



private void Datagrid1_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

DataGrid dg = (DataGrid) sender;

if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)

{

((Label) E.item.findcontrol ("ItemIndex")). Text = (DG. CurrentPageIndex * DG. PageSize + E.item.itemindex + 1). ToString ();

}

}

}

}



The data layer code is as follows:

Using System;

Using System.Data;

Using System.Data.SqlClient;

Using System.Configuration;



Namespace Aspnetpaging

{

public class DataAccess

{

private static string connstring = configurationsettings.appsettings["ConnString"];

Private DataAccess ()

{



}



public static DataSet Getcustomersdata ()

{

SqlConnection conn = new SqlConnection (connstring);

SqlCommand comm = new SqlCommand ("GetCustomers", conn);

Comm.commandtype = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter (comm);

DataSet ds = new DataSet ();

DataAdapter.Fill (DS);

return DS;

}

}

}



Summing up, the above four methods in fact, the first two kinds of processing is the same, but the processing time is different; for the third I think the simplest, directly in the foreground page binding, does not require additional assistance; For the fourth method bound to the foreground I think the most flexible, It is to be noted that the Getrecordindex method requires protected or public so that its inheriting classes can be accessed.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.