当前位置: 首页 > 编程语言 > C#

向gridviewasp.net添加新行数据c#分享

时间:2023-04-10 11:23:25 C#

向gridviewasp.netc#添加新行数据客户){ID=cust.ID;姓名=客户.姓名;FatherName=cust.FatherName;Email=cust.Email;}publicintID{得到;放;}公共字符串名称{得到;放;}publicstringFatherName{get;放;}公共字符串电子邮件{得到;放;}}并创建此函数以加载包含一些数据的列表:publicListGenerate_Data(){ListlstCustomer=newList();客户customer=newCustomer();客户.ID=1;customer.Name="约翰塞纳";customer.FatherName="约翰";customer.Email="cena@gmail.com";lstCustomer.Add(新客户(客户));客户.ID=2;customer.Name="莫克什";customer.FatherName="Rajnikant";customer.Email="mokesh@gmail.com";lstCustomer.Add(新客户(客户));客户.ID=3;customer.Name="BilalAhmad";customer.FatherName="卡西夫";customer.Email="bilal@gmail.com";lstCustomer.Add(新客户(客户));客户.ID=4;customer.Name="秦尚";customer.FatherName="商禾";customer.Email="chinshang@gmail.com";lstCustomer.Add(新客户(客户));返回lstCustomer;返回此列表以与网格绑定的代码是:ListlstCustomer=newList();lstCustomer=Generate_Data();GridView1.DataSource=lstCustomer;GridView1.DataBind();该页面添加了4个文本框和一个按钮,名称为:Id、Name、FatherName、Email当我单击按钮时,我想将文本框的新值添加到gridview1行。我想动态地向gridview添加一行。如果我定义一个空的gridview,我如何将我的文本框值添加到gridview行?问题1的方法不一样吗?ASPX:代码隐藏:publicclassCustomer{publicCustomer(){}publicCustomer(Customercust){ID=cust.ID;姓名=客户.姓名;FatherName=cust.FatherName;Email=cust.Email;}publicintID}publicstringName{get;放;}publicstringFatherName{get;放;}公共字符串电子邮件{得到;放;}}protectedvoidButton1_Click(objectsender,EventArgse){ListlstCustomer=newList();if(Session["dt"]!=null){lstCustomer=(List)Session["dt"];}客户customer=newCustomer();customer.ID=int.Parse(TextBox1.Text);customer.Name=TextBox2.Text;customer.FatherName=TextBox2.Text;customer.Email=TextBox2.Text;lstCustomer.Add(新客户(客户));GridView1.DataSource=lstCustomer;GridView1.DataBind();Session["dt"]=lstCustomer;}更新!protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){ListlstCustomer=newList();客户tomercustomer=newCustomer();客户.ID=1;customer.Name="约翰塞纳";customer.FatherName="约翰";customer.Email="cena@gmail.com";lstCustomer.Add(新客户(客户));客户.ID=2;customer.Name="莫克什";customer.FatherName="Rajnikant";customer.Email="mokesh@gmail.com";lstCustomer.Add(新客户(客户));.ID=3;customer.Name="BilalAhmad";customer.FatherName="卡西夫";customer.Email="bilal@gmail.com";lstCustomer.Add(新客户(客户));客户.ID=4;customer.Name="秦尚";customer.FatherName="商禾";customer.Email="chinshang@gmail.com";lstCustomer.Add(新客户(客户));Session["dt"]=lstCustomer;您需要在回发期间保留您的集合根据您的集合有多大,您可能会考虑将其保存在视图状态、会话等中。列表lstCustomer=newList();lstCustomer=Generate_Data();ViewState["客户"]=lstCustomer;GridView1.DataSource=lstCustomer;GridView1.DataBind();因此,当用户用数据填充文本框并启动回发时,您可以创建新的客户对象并将它们添加到现有集合中。然后将数据重新绑定到网格。Customercustomer=newCustomer(){ID=int.Parse(TextBox1.Text),Name=TextBox2.Text,FatherName=TextBox2.Text,Email=TextBox2.Text}varlstCustomer=ViewState["Customers"]asList;lstCustomer.Add(客户);请注意,您需要将[Serializable]属性添加到Customer类。您可以简单地将AutoGenerateInsertButton="True"添加到标记中。所以,我会做的。首先,我将扩展lstCustomer的范围,以便它通过回发持续存在。然后在按钮事件处理程序中,我会写这样的东西//HerewecreatetheobjectCustomercustomer=newCustomer();customer.foo=txtFoo.Text;customer.bar=txtBar.Text;//...//然后我们将它添加到列表中lstCustomer.Add(customer);//并再次对其进行数据绑定GridView1.DataSource=lstCustomer;GridView1.DataBind();如果您希望用户对数据执行某些操作(或者您希望持久保存到数据库或其他内容),那也会有所帮助。以上就是C#学习教程:Addingnewrowdatatogridviewasp.netc#分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文来自网络收集,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: