本文介绍了使用c#动态更改asp.net中gridview的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个gridview控件。我根据三种不同的条件绑定它。对于每个条件,gridview的列数都会改变,也有些列是新的。 for ex。 - 在第一种情况下,我想显示Fname,Lname,地区,城市,州,国家。 - 第二种情况Fname,Lname,area,Pincode - 在第三种情况下Fname,MName,Lname,area,Pincode。 我使用模板字段和数据绑定表达式(如Eval)在gridview中显示数据。 /> 如何完成这项任务? 从下面的评论中复制的其他信息 但是我使用rowdata bound和rowcreated事件来执行一些计算,并在中间添加一个新行来显示小计和总计值,如下所示 rowdatabound: -I have a gridview control.I m binding it based on three different conditions.For every condition the number of columns change for gridview,also some columns are new.for ex.- In 1st case i want to display Fname,Lname,area,city,state,country.- In 2nd case Fname,Lname,area,Pincode- In 3rd case Fname,MName,Lname,area,Pincode.I m using template fields and databinding expressions like Eval to display data in gridview.How can i do this task?additional information copied from comment belowBut i using rowdata bound and rowcreated events to perform some calculations and adding a new row in the middle to display subtotal and grand total values as followsrowdatabound:-if (e.Row.RowType == DataControlRowType.DataRow){ strPreviousRowID = DataBinder.Eval(e.Row.DataItem, "VrDate").ToString(); double rowAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TrAmt").ToString()); dblSubTotalAmount += Math.Abs(rowAmount); dblGrandTotalAmount += Math.Abs(rowAmount);} 我可以通过简单的绑定网格来做到这一点吗?can i do this by simply binding grid?推荐答案<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"> <Columns> <asp:TemplateField HeaderText="Fname"> <ItemTemplate> <asp:TextBox ID="txtbox" Text='<%# Eval("Name") %>' runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField='Name' HeaderText="Lname" /> <asp:BoundField DataField='Name' HeaderText="area" /> <asp:BoundField DataField='Name' HeaderText="city" /> <asp:BoundField DataField='Name' HeaderText="state" /> <asp:BoundField DataField='Name' HeaderText="country" /> <asp:BoundField DataField='Name' HeaderText="Pincode" /> </Columns> </asp:GridView>public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { } else { DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add(" data 1"); dt.Rows.Add(" data 2 "); string[] case1 = { "Fname", "Lname", "area", "city", "state", "country" }; // u can add your columns names based on the conditions to hide string[] case2 = { "Fname", "Lname", "area", "Pincode" }; string[] case3 = { "Fname", "Lname", "Pincode" }; string casevalue = "case2"; for (int i = 0; i < GridView1.Columns.Count; i++) { GridView1.Columns[i].Visible = false; string header = GridView1.Columns[i].HeaderText; if( casevalue =="case1") if (case1.Contains(header)) GridView1.Columns[i].Visible = true; if (casevalue == "case2") if (case2.Contains(header)) GridView1.Columns[i].Visible = true; if (casevalue == "case3") if (case3.Contains(header)) GridView1.Columns[i].Visible = true; } GridView1.DataSource = dt; GridView1.DataBind(); } } } 这篇关于使用c#动态更改asp.net中gridview的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 23:38