十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
ASP.NET中的gridview如何增加一行呢,下面介绍一种最简单的方式。
只使用后台的数据,在后台的datatable或者list增加一项,然后重新绑定gridview。
直接看代码
效果:
实体类
public class PersonModel { private int personIndex; public int PersonIndex { get { return personIndex; } set { personIndex = value; } } private string personID; public string PersonID { get { return personID; } set { personID = value; } } private string personName; public string PersonName { get { return personName; } set { personName = value; } } private string personSex; public string PersonSex { get { return personSex; } set { personSex = value; } } private int personAge; public int PersonAge { get { return personAge; } set { personAge = value; } } private bool personSelected = false; public bool PersonSelected { get { return personSelected; } set { personSelected = value; } } }
public class MainManager { private int sIndex = 0; private ListpersonCollect = new List (); private static MainManager instance = null; public List PersonCollect { get { return personCollect; } set { personCollect = value; } } public static MainManager DoGetInstance() { if (instance == null) { instance = new MainManager(); } return instance; } public void DoAddSinglePersons() { sIndex = this.PersonCollect.Count; PersonModel model = new PersonModel(); model.PersonIndex = sIndex; model.PersonID = System.Guid.NewGuid().ToString(); model.PersonName = "测试" + sIndex; model.PersonAge = 17 + sIndex; model.PersonSex = sIndex % 2 == 0 ? "男" : "女"; model.PersonSelected = false; this.PersonCollect.Add(model); } }
public partial class _Default : System.Web.UI.Page { private MainManager dManager = null; protected PersonModel selectItem = null; protected void Page_Load(object sender, EventArgs e) { dManager = MainManager.DoGetInstance(); if (!IsPostBack) { dManager.DoAddSinglePersons(); this.dgPersons.DataSource = dManager.PersonCollect; this.dgPersons.DataBind(); } } protected void AddItem_Click(object sender, EventArgs e) { dManager.DoAddSinglePersons(); this.dgPersons.DataSource = dManager.PersonCollect; this.dgPersons.DataBind(); } }如果一行中有其他list控件比如 下拉列表asp:DropDownList,那么在实体中增加一个list,对应下拉列表的绑定。而且对于每一行的点击,选择等操作都要写相应的事件修改数据,下一次绑定时要将list中的数据修改成操作过的值或状态,再进行绑定。
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。