本文介绍了gridview需要一列来自动减少s.no.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在gridview中需要一个列来自动递减s.no ..其条件是它将基于年和月。假设gridview具有基于日期的列。 for 每个月s.no开始来自 1 。 输出将类似于此: S.no日期 2 29-01-2015 1 30-01-2015 2 01-02-2015 1 05-02-2015 2 02-03-2015 1 05-03-2015 3 06-04-2015 2 07-04-2015 1 08-04-2015 aspx页面: < asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False > < columns> < asp:TemplateField HeaderText = S.no > < itemtemplate> < asp:标签ID = lblSerial runat = server /> < / itemtemplate > < asp:BoundField DataField = 日期 HeaderText = 日期 /> < / 列 > < / asp:GridView > aspx.cs页面: public partial class test:System.Web.UI.Page { MySqlConnection con = new MySqlConnection(ConStr.ConnectStr()); int monthcount; DataTable dt = new DataTable(); 受保护 void Page_Load( object sender,EventArgs e) { if (!IsPostBack) { Binddata(); } } public void Binddata() { 尝试 { con.Open(); MySqlCommand cmd = new MySqlCommand( select *来自测试,con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(dt); monthcount = dt.Rows.Count + 1 ; for ( int i = 0 ; i < dt.Rows.Count; i ++) { string date = dt.Rows [i] [ Date]。ToString(); string [] sl = date.Split(' - '); string sl2 = sl [ 2 ]。ToString(); string sl1 = sl [ 1 ]。ToString(); string current_row_date = sl2 + sl1; if (i!= 0 ) { string ldate = dt.Rows [i - 1 ] [ 日期]。ToString(); string [] lsl = date.Split(' - '); string lsl2 = lsl [ 2 ]。ToString(); string lsl1 = lsl [ 1 ]。ToString(); string lcurrent_row_date = lsl2 + lsl1; 会话[ lcurrent_row_date] = lcurrent_row_date.ToString(); } else {会话[ lcurrent_row_date] = ; } string lastrow =会话[ lcurrent_row_date]的ToString(); if (current_row_date == lastrow) { Label lblSerial =(Label)GridView1.Rows [i] .FindControl( lblSerial); lblSerial.Text = monthcount.ToString(); } else { Label lblSerial =(Label)GridView1.Rows [i] .FindControl( lblSerial); lblSerial.Text = monthcount.ToString(); } } monthcount--; GridView1.DataSource = dt; GridView1.DataBind(); } catch (例外) { 抛; } } } } 解决方案 对于自动生成的S.No列,pLease将此代码写入gridview< column>在设计页面上。 < templetefield> < ItemTemplate > < asp:标签 ID = lblserial 文本 =' <% #Container.DataItemIndex + 1 %> ' runat = server > < / asp:Label > < / ItemTemplate > 提前致谢 添加到SELECT ROW_NUMBER()函数中,对日期进行分区,ORDER BY DESC 详细信息取决于您的数据库 - 可能是ROWNUM,ROW_NUMBER或其他一些功能。做研究,这个简单的查询应该很容易实现。 这是用于SQL Server SELECT *,ROW_NUMBER()OVER (按年份划分(your_date_field),月份(your_date_field)按照你的_date_field DESC订购)sno 来自your_table 注意你不应该'生产中有SELECT *,因为它会随着新的连接或视图的添加(环境字段)而破坏,或者你依赖于字段的顺序。明确说明你从查询中返回的内容。 这可能对mySQL有用(之前我没有使用过MySQL): MySQL RowNumber示例 [ ^ ] 如果这有帮助,请花些时间接受解决方案。谢谢。 参考 of Sinisa Hajnal ......我想通了..非常感谢Sinisa Hajnal ... 在 Mysql: SELECT @ row_num := IF (@ prev_value = date_format(str_to_date(paydate,' %d-%m-%Y') ,' %m%Y'),@ row_num + 1, 1 ) AS RowNumber ,@ Customer:= date_format(str_to_date(paydate,' %d-%m-%Y'),' %m%Y') as 日期 ,o .PayD ate , @ prev_value := date_format(str_to_date(paydate,' %d-%m-%Y'),' % m%Y') FROM test o,( SELECT @ row_num := 1 )x,( SELECT @ prev_value := ' ')y 订单 BY RowNumber DESC In a gridview need a column to auto decrement the s.no.. having a condition that it will be based on the year and month . assume that the gridview having a column based on date. for every month s.no starts from 1.Output will be like this:S.no Date 2 29-01-2015 1 30-01-2015 2 01-02-2015 1 05-02-2015 2 02-03-2015 1 05-03-2015 3 06-04-2015 2 07-04-2015 1 08-04-2015aspx page:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <columns> <asp:TemplateField HeaderText="S.no"> <itemtemplate> <asp:Label ID="lblSerial" runat="server" /> </itemtemplate> <asp:BoundField DataField="Date" HeaderText="Date" /> </columns> </asp:GridView >aspx.cs page: public partial class test : System.Web.UI.Page { MySqlConnection con = new MySqlConnection(ConStr.ConnectStr()); int monthcount; DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Binddata(); } } public void Binddata() { try { con.Open(); MySqlCommand cmd = new MySqlCommand("select * from test", con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(dt); monthcount = dt.Rows.Count + 1; for (int i = 0; i < dt.Rows.Count; i++) { string date = dt.Rows[i]["Date"].ToString(); string[] sl = date.Split('-'); string sl2 = sl[2].ToString(); string sl1 = sl[1].ToString(); string current_row_date = sl2 + sl1; if (i != 0) { string ldate = dt.Rows[i - 1]["Date"].ToString(); string[] lsl = date.Split('-'); string lsl2 = lsl[2].ToString(); string lsl1 = lsl[1].ToString(); string lcurrent_row_date = lsl2 + lsl1; Session["lcurrent_row_date"] = lcurrent_row_date.ToString(); } else { Session["lcurrent_row_date"] = ""; } string lastrow = Session["lcurrent_row_date"].ToString(); if (current_row_date == lastrow) { Label lblSerial = (Label)GridView1.Rows[i].FindControl("lblSerial"); lblSerial.Text = monthcount.ToString(); } else { Label lblSerial = (Label)GridView1.Rows[i].FindControl("lblSerial"); lblSerial.Text = monthcount.ToString(); } } monthcount--; GridView1.DataSource = dt; GridView1.DataBind(); } catch (Exception) { throw; } } }} 解决方案 For auto generated S.No column, pLease write this code inside gridview<column> on design page.<templetefield><ItemTemplate> <asp:Label ID = "lblserial" Text ='<%# Container.DataItemIndex + 1%>' runat="server"></asp:Label> </ItemTemplate>Thanks in advanceAdd into your SELECT ROW_NUMBER() function, partition over the date, ORDER BY DESCThe details depend on your database - it could be ROWNUM, ROW_NUMBER or maybe some other function. Do the research, this simple query should be very simple to implement.This is for SQL ServerSELECT * ,ROW_NUMBER() OVER (PARTITION BY YEAR(your_date_field), MONTH(your_date_field) ORDER BY your_date_field DESC) AS snoFROM your_tableNOTE that you shouldn't have SELECT * in production as it tends to break as new joins or views are added (ambigous fields) or you depend on the order of the fields for something. State explicitly what you're returning from the query.This might be useful for mySQL (I didn't work with MySQL before):MySQL RowNumber example[^]If this helps, please take time to accept the solution. Thank you.By the reference of Sinisa Hajnal ... i figured it out.. thank u so much Sinisa Hajnal...In Mysql:SELECT @row_num := IF(@prev_value= date_format(str_to_date(paydate, '%d-%m-%Y'), '%m%Y'),@row_num+1,1) AS RowNumber ,@Customer:=date_format(str_to_date(paydate, '%d-%m-%Y'), '%m%Y') as Date ,o.PayDate ,@prev_value := date_format(str_to_date(paydate, '%d-%m-%Y'), '%m%Y') FROM test o, (SELECT @row_num := 1) x, (SELECT @prev_value := '') y order BY RowNumber DESC 这篇关于gridview需要一列来自动减少s.no.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 03:48