ASP.NET中的gridview如何增加一行呢,下面介紹一種最簡單的方式。
只使用后臺的數(shù)據(jù),在后臺的datatable或者list增加一項,然后重新綁定gridview。
直接看代碼
效果:
實體類
我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、硯山ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的硯山網(wǎng)站制作公司
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,對應(yīng)下拉列表的綁定。而且對于每一行的點擊,選擇等操作都要寫相應(yīng)的事件修改數(shù)據(jù),下一次綁定時要將list中的數(shù)據(jù)修改成操作過的值或狀態(tài),再進行綁定。