Several methods of displaying a DataGrid control column number in ASP.NET

xiaoxiao2021-03-06  38

Several methods of displaying a DataGrid control column number in ASP.NET

Source: Unknown Author: Unknown Recommended rating: Visits: 18

DataGrid control that displays the serial number of the column of asp.net in several ways: 2004-9-10 in aps.net Zheng Zuo multi-control data binding lot, on the function, it should belong to the most complete DataGrid, but it did not provide The function of displaying record serial numbers, but we can get the serial number through some of the parameters it belong, let's take a look at how to get and display the serial number calculation method as follows: (1) in the background DataGrid.currentPageIndex * DataGrid.pageSize e.Item.ItemIndex 1 (2) in the foreground DataGrid1.CurrentPageIndex * DataGrid1.PageSize Container.ItemIndex 1 Description: e System.Web.UI.WebControls.DataGridItemEventArgs shows an example of the parameter class; the DataGrid1 shows an example where the front DataGrid.currentPageIndex: Get or set the index of the current display page; DataGrid.pageSize: Get or set the number of items to display on the single page of the DataGrid control. Here I used four ways to display the serial number at the front desk, but it was expanded around the above calculation. (1) Use DataGrid's itemcreated settings, and the cell's cell can be a binding column or template column (including empty template); (2) using the DataGrid's itemDatabase value, and the front desk can be a binding column or Template column (including empty template); (3) Direct binding calculation expressions in front desk; (4) Calculate the expression by inheritance of the front page class in the background class. Remarks: Setting the extra number of serial numbers when you get data in the database, you don't discuss it, I think this is the worst implementation method.

Below to get the data of the Customers table of the Northwind database, the following: Sequence number 1 serial number 2 serial number 3 serial number 4 serial 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 56 Norts 57 57 57 57 57 Ocean 58 58 58 58 59 59 Ottik 60 60 60 60 60 Paris 1 2 3 4 5 6 7 8 9 10 below is WebFormPaging .aspx file code, <% @ page language = "c #" codebehind = "WebFormPaging.aspx.cs" autoeventwireup = "false" inherits = "aspnetpaging.webform1"%> > 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 = "400" align = "center" border = "1"> < Tr> <TD> <asp: DataGrid ID = "DataGrid1" Runat = "SE rver "AutoGenerateColumns =" ​​False "Width =" 100% "AllowPaging =" True "> <Columns> <asp: BoundColumn HeaderText =" No. 1 "> </ asp: BoundColumn> <asp: TemplateColumn HeaderText =" No. 2 "> </ ask: templateColumn> <ask: templatecolumn headertext = "Sequence number 3"> <</p> <p>ItemTemplate> <ask = "ItemIndex" runat = "server"> </ asp: label> </ itemtemplate> </ asp: templateColumn> <ask: templateColumn headertext = "serial 4"> <itemtemplate> <% # (DataGrid1.CurrentPageIndex * DataGrid1.PageSize Container.ItemIndex 1)%> </ ItemTemplate> </ asp: TemplateColumn> <asp: TemplateColumn HeaderText = "No. 5"> <ItemTemplate> <% # GetRecordIndex (Container.ItemIndex) %> </ ItemTemplate> </ asp: TemplateColumn> <asp: BoundColumn DataField = "CustomerID" HeaderText = "CustomerID"> </ asp: BoundColumn> </ Columns> <PagerStyle Mode = "NumericPages"> </ PagerStyle> < / ASP: DATAGRID> </ TD> </ TD> <Tr> <TD> <TD> </ TD> <TD> <TD> </ TD> <Tr> </ Table> </ form> < / body> </ HTML> 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; use system.web.ui.webcontrols; use system.web.ui.htmlcontrols; namespace aspnetpaging {public class webform1: system.web.ui.pag e {private int recordCount = 0; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load (object sender, System.EventArgs e) {if {DataGridDataBind () (Page.IsPostBack!);}} // tie given data private void DataGridDataBind () {DataSet ds = DataAccess.GetCustomersData (); this.DataGrid1.DataSource = ds; this.DataGrid1.DataBind ();} #region Web form Designer generated code override protected void OnInit (EventArgs e) {initializationComponent ();</p> <p>Base.onInit (E);} /// <summary> /// Designer supports the required method - Do not use the code editor to modify the // / this method.</p> <p>/// </ 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 the 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 == List ItemType.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: use system;</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-70359.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="70359" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.041</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'V4Ld3TR8_2FpAXOpws6o_2Bh4tMFzkgJlSMQZU5yzz7YSnaepoSOxrRfnbS1oM6hd3c41P6E_2BD53z0SFHStIvFMi_2FA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>