ASP.NET中的gridview如何增加一行呢,下面介紹一種最簡(jiǎn)單的方式。
只使用后臺(tái)的數(shù)據(jù),在后臺(tái)的datatable或者list增加一項(xiàng),然后重新綁定gridview。
直接看代碼
效果:
實(shí)體類
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 List<PersonModel> personCollect = new List<PersonModel>(); private static MainManager instance = null; public List<PersonModel> 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 = "測(cè)試" + sIndex; model.PersonAge = 17 + sIndex; model.PersonSex = sIndex % 2 == 0 ? "男" : "女"; model.PersonSelected = false; this.PersonCollect.Add(model); } }
<head runat="server"> <title></title> </head> <body id="MyBody" runat="server" ms_positioning="GridLayout"> <form id="Form1" runat="server" method="post"> <div> <asp:Button ID="btnAdd" runat="server" OnClick="AddItem_Click" Text="增加" /> <asp:GridView ID="dgPersons" runat="server" AutoGenerateColumns="False" EnableViewState="false" CellPadding="4" ForeColor="#333333" DataKeyNames="PersonID"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="PersonIndex" HeaderText="序號(hào)"/> <asp:TemplateField> <ItemTemplate> <input id="radiobutton1" name="pselect" type="radio" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PersonName" HeaderText="姓名" /> <asp:BoundField DataField="PersonAge" HeaderText="年齡" /> <asp:BoundField DataField="PersonSex" HeaderText="性別" /> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </div> </form> </body>
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,那么在實(shí)體中增加一個(gè)list,對(duì)應(yīng)下拉列表的綁定。而且對(duì)于每一行的點(diǎn)擊,選擇等操作都要寫相應(yīng)的事件修改數(shù)據(jù),下一次綁定時(shí)要將list中的數(shù)據(jù)修改成操作過(guò)的值或狀態(tài),再進(jìn)行綁定。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
文章題目:ASP.NET—002:GridView手動(dòng)增加一行-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://jinyejixie.com/article8/dsidop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)、電子商務(wù)、外貿(mào)建站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容