GridView動(dòng)態(tài)綁定列_第1頁(yè)
GridView動(dòng)態(tài)綁定列_第2頁(yè)
GridView動(dòng)態(tài)綁定列_第3頁(yè)
GridView動(dòng)態(tài)綁定列_第4頁(yè)
GridView動(dòng)態(tài)綁定列_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、GridView動(dòng)態(tài)綁定列在VS2005我們利用GridView的頻率比較的大,但我們的數(shù)據(jù)列一般是不發(fā)生變化的,但在實(shí)際的工作中,我們需要對(duì)GridView的數(shù)據(jù)列進(jìn)行修改,進(jìn)行重新綁定,以下以進(jìn)行動(dòng)態(tài)綁定的代碼,希望對(duì)您有所幫助,以下是HTML代碼:<%. Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default" Theme="Default" CodeFile="Dictionary.aspx.cs" In

2、herits="Admin_Dictionary" %><!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Transitional/EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head runat="server"> <title>數(shù)據(jù)字典配置&

3、lt;/title></head><body> <form id="form1" runat="server"> <fieldset> <legend>菜單</legend> <div id="tabsF"> <ul> <li><a href="OfficeType.aspx" title="辦公用品類型"><span>辦公用品</span><

4、;/a></li> <li><a href="AddTax.aspx" title="增值稅稅率數(shù)據(jù)字典"><span>增值稅稅率</span></a></li> <li><a href="MachineName.aspx" title="整機(jī)機(jī)種,收款計(jì)劃數(shù)據(jù)字典"><span>整機(jī)機(jī)種|收款計(jì)劃</span></a></li> <li>&l

5、t;a href="Default.aspx" title="返回"><span>返回</span></a></li> </ul> </div> </fieldset> <div> <fieldset> <legend>數(shù)據(jù)字典信息</legend>&nbsp; <table width="100%"> <tr> <td rowspan="2"

6、; valign="top" style="width: 174px"> <fieldset style="text-align: left"> <legend>數(shù)據(jù)字典項(xiàng)</legend> <asp:BulletedList ID="blDataDict" runat="server" DisplayMode="LinkButton" OnClick="blDataDict_Click" BulletSty

7、le="Square"> <asp:ListItem Selected="True" Value="Dict_PayType">付款類型</asp:ListItem> <asp:ListItem Value="Dict_PayModel">付款方式</asp:ListItem> <asp:ListItem Value="Dict_IncType">收款類型</asp:ListItem> <asp:ListItem

8、 Value="Dict_DeliveryModel">交貨方式</asp:ListItem> <asp:ListItem Value="Customer_Type">客戶類型</asp:ListItem> <asp:ListItem Value="Dict_TransModel">運(yùn)輸方式</asp:ListItem> <asp:ListItem Value="Dict_MachineName">整機(jī)名稱</asp:ListIte

9、m> <asp:ListItem Value="Provider_Type">供應(yīng)商類型</asp:ListItem> <asp:ListItem Value="Dict_IncPlan_Kind">收款性質(zhì)類型</asp:ListItem> </asp:BulletedList> </fieldset> </td> <td> <fieldset> <legend>相關(guān)信息</legend> <asp:GridV

10、iew ID="gvDictionary" SkinID="Default_GridView" runat="server" CaptionAlign="Left" OnRowCancelingEdit="gvDictionary_RowCancelingEdit" OnRowDataBound="gvDictionary_RowDataBound" OnRowEditing="gvDictionary_RowEditing" OnRowUpdating=

11、"gvDictionary_RowUpdating"> <Columns> <asp:BoundField DataField="DictName" HeaderText="字典名稱"></asp:BoundField> <asp:BoundField DataField="DictCode" HeaderText="字典編號(hào)" ReadOnly="True"></asp:BoundField> <asp

12、:CheckBoxField DataField="IfUsing" HeaderText="是否啟用" Text="啟用"></asp:CheckBoxField> <asp:BoundField DataField="Note" HeaderText="備注" /> <asp:CommandField ShowEditButton="True" CausesValidation="False"></asp

13、:CommandField> </Columns> </asp:GridView> </fieldset> </td> </tr> <tr> <td> <fieldset> <legend>添加數(shù)據(jù)字典信息</legend> <br /> <table cellpadding="4" id="tableInfo" runat="server"> <tr> <td ali

14、gn="center" colspan="2"> <asp:Label ID="lblTitle" runat="server" Font-Bold="True"></asp:Label></td> </tr> <tr> <td class="tdbg"> <span style="color: #ff0000">*</span>數(shù)據(jù)字典編號(hào):</t

15、d> <td align="left"> <asp:TextBox ID="txtDictCode" runat="server" Width="241px"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtDictCode" ErrorMe

16、ssage="數(shù)據(jù)字典編號(hào)不能為空">*</asp:RequiredFieldValidator></td> </tr> <tr> <td class="tdbg"> <span style="color: #ff0000">*</span>數(shù)據(jù)字典名稱:</td> <td align="left"> <asp:TextBox ID="txtDictName" runat=&q

17、uot;server" Width="241px"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtDictName" ErrorMessage="數(shù)據(jù)字典名稱不能為空">*</asp:RequiredFieldValidator></td> </tr>

18、 <tr> <td class="tdbg"> 是否啟用:</td> <td align="left"> <asp:CheckBox ID="chkUsing" runat="server" Text="啟用" Checked="True" /> <asp:Label ID="lblTable" runat="server" Visible="False&quo

19、t;></asp:Label></td> </tr> <tr> <td class="tdbg"> 備注:<br /> </td> <td align="left"> <asp:TextBox ID="txtNote" runat="server" Height="60px" TextMode="MultiLine" Width="241px">

20、;</asp:TextBox></td> </tr> <tr> <td align="center" colspan="2"> <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" /> <asp:Button ID="btnCancel" runat="server"

21、; Text="取消" CausesValidation="False" OnClick="btnCancel_Click" /> <br /> <asp:Label ID="lblInfo" runat="server"></asp:Label> </td> </tr> </table> </fieldset> </td> </tr> </table> </fie

22、ldset> <fieldset> <legend>業(yè)務(wù)邏輯</legend> <ul> <li>帶<span style="color: red">*</span>項(xiàng)不能為空</li> </ul> </fieldset> </div> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox=&q

23、uot;True" ShowSummary="False" /> </form></body></html>以上HTML,代碼和普通的頁(yè)面代碼很相似,本頁(yè)面完成的功能是,通過(guò)點(diǎn)擊左邊的鏈接,動(dòng)態(tài)的調(diào)用,不同的表中的數(shù)據(jù),但是,表中的字段名稱,有的是不同的,因此需要我根據(jù)實(shí)際情況,進(jìn)行動(dòng)態(tài)綁定,以下頁(yè)面代碼:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using Syst

24、em.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Admin_Dictionary : System.Web.UI.Page /define class WebUtility.DictionaryBase dict=new WebUtility.DictionaryBase(); protected void Page_L

25、oad(object sender, EventArgs e) if (!Page.IsPostBack) /instantiation class dict.DictionaryTableName = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString(); this.lblTable.Text = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString(); /set datasource this.gvDictionary.Caption = "付款類型&qu

26、ot;+" 數(shù)據(jù)字典列表" this.lblTitle.Text = "添加付款類型 數(shù)據(jù)字典" this.gvDictionary.DataSource = dict.DataSetDictionary(); this.gvDictionary.DataBind(); protected void blDataDict_Click(object sender, BulletedListEventArgs e) /set caption and title this.gvDictionary.Caption = this.GetCaption(this.

27、blDataDict.Itemse.Index.Text); this.lblTitle.Text = this.GetTitle(this.blDataDict.Itemse.Index.Text); /clear add item information this.ClearInfo(); /set tablename to label this.lblTable.Text = this.blDataDict.Itemse.Index.Value; this.gvBind(); /modify add info area private string GetCaption(string s

28、trItem) return strItem+" 數(shù)據(jù)字典列表" private string GetTitle(string strItem) return " 添加" + strItem + " 數(shù)據(jù)字典" private void ClearInfo() this.txtDictCode.Text = "" this.txtDictName.Text = "" this.txtNote.Text = "" this.lblInfo.Text = ""

29、 private int GetValue(bool bol) if (bol) return 1; else return 0; protected void btnCancel_Click(object sender, EventArgs e) . this.ClearInfo(); protected void btnSave_Click(object sender, EventArgs e) . dict.DictionaryTableName = this.lblTable.Text; if (dict.IsDictionary(this.txtDictCode.Text) . th

30、is.lblInfo.ForeColor = System.Drawing.Color.Red; this.lblInfo.Text = this.txtDictCode.Text + " 編號(hào)已經(jīng)存在,請(qǐng)重新填寫" return; /set value to member dict.DictionCode = this.txtDictCode.Text; dict.DictionName = this.txtDictName.Text; dict.Note = this.txtNote.Text; dict.IfUsing = this.GetValue(this.chk

31、Using.Checked); /transfer member to add operate int i = this.dict.AddDictionary(); if (i > 0) . this.ClearInfo(); this.lblInfo.ForeColor = System.Drawing.Color.Green; this.lblInfo.Text = "數(shù)據(jù)字典添加操作成功" this.gvBind(); else . this.lblInfo.ForeColor = System.Drawing.Color.Red; this.lblInfo.T

32、ext = "數(shù)據(jù)字典添加操作失?。?quot; private void gvBind() . dict.DictionaryTableName = this.lblTable.Text; / when table is dict_machineName,clear all gridview columns / and add new column if (this.lblTable.Text = WebUtility.DictionaryBase.DictType.Dict_MachineName.ToString() . this.gvDictionary.Columns.Cl

33、ear(); /define class System.Web.UI.WebControls.BoundField field; /instantiation class field = new BoundField(); field.DataField = "TypeName" field.HeaderText = "整機(jī)類型" this.gvDictionary.Columns.Add(field); field = new BoundField(); field.DataField = "Type" field.HeaderTe

34、xt = "整機(jī)編號(hào)" field.ReadOnly = true; this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField(); chkField.DataField = "IfUsing" chkField.Text = "啟用" chkField.HeaderText = "是否啟用" this.gvDictionary.Columns.Add(chkF

35、ield); field = new BoundField(); field.DataField = "Note" field.HeaderText = "備注" this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CommandField cmdField = new CommandField(); cmdField.ShowEditButton = true; cmdField.CausesValidation = false; this.gvDictionary.Colum

36、ns.Add(cmdField); /if table is Customer_Type or Provider_Type else if (this.lblTable.Text = WebUtility.DictionaryBase.DictType.Customer_Type.ToString() | this.lblTable.Text = WebUtility.DictionaryBase.DictType.Provider_Type.ToString() . this.gvDictionary.Columns.Clear(); /define class System.Web.UI.

37、WebControls.BoundField field; /instantiation class field = new BoundField(); field.DataField = "TypeName" field.HeaderText = "類型名稱" this.gvDictionary.Columns.Add(field); field = new BoundField(); field.DataField = "TypeCode" field.HeaderText = "類型編號(hào)" field.Rea

38、dOnly = true; this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField(); chkField.DataField = "IfUsing" chkField.Text = "啟用" chkField.HeaderText = "是否啟用" this.gvDictionary.Columns.Add(chkField); field = new BoundField()

39、; field.DataField = "Note" field.HeaderText = "備注" this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CommandField cmdField = new CommandField(); cmdField.ShowEditButton = true; cmdField.CausesValidation = false; this.gvDictionary.Columns.Add(cmdField); else . this.g

40、vDictionary.Columns.Clear(); /define class System.Web.UI.WebControls.BoundField field; /instantiation class field = new BoundField(); field.DataField = "DictName" field.HeaderText = "字典名稱" this.gvDictionary.Columns.Add(field); field = new BoundField(); field.DataField = "Dic

41、tCode" field.HeaderText = "字典編號(hào)" field.ReadOnly = true; this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CheckBoxField chkField = new CheckBoxField(); chkField.DataField = "IfUsing" chkField.Text = "啟用" chkField.HeaderText = "是否啟用" this.gvD

42、ictionary.Columns.Add(chkField); field = new BoundField(); field.DataField = "Note" field.HeaderText = "備注" this.gvDictionary.Columns.Add(field); System.Web.UI.WebControls.CommandField cmdField = new CommandField(); cmdField.ShowEditButton = true; cmdField.CausesValidation = fals

43、e; this.gvDictionary.Columns.Add(cmdField); this.gvDictionary.DataSource = dict.DataSetDictionary(); this.gvDictionary.DataBind(); protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e) . if (e.Row.RowType = DataControlRowType.DataRow) . e.Row.Attributes.Add("onmouseove

44、r", "e=this.style.backgroundColor; this.style.backgroundColor='linen'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e"); protected void gvDictionary_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) . this.gvDictionary.EditI

45、ndex = -1; this.gvBind(); protected void gvDictionary_RowEditing(object sender, GridViewEditEventArgs e) . this.gvDictionary.EditIndex = e.NewEditIndex; this.gvBind(); protected void gvDictionary_RowUpdating(object sender, GridViewUpdateEventArgs e) . TextBox txtName = (TextBox)this.gvDictionary.Row

46、se.RowIndex.Cells0.Controls0; TextBox txtNote = (TextBox)this.gvDictionary.Rowse.RowIndex.Cells3.Controls0; CheckBox chkIfUsing = (CheckBox)this.gvDictionary.Rowse.RowIndex.Cells2.Controls0; /get value to member; dict.DictionaryTableName = this.lblTable.Text; dict.DictionName = txtName.Text; dict.Di

47、ctionCode = this.gvDictionary.Rowse.RowIndex.Cells1.Text; dict.IfUsing = this.GetValue(chkIfUsing.Checked); dict.Note = txtNote.Text; /transfer method begin update dict.UpdateDictionary(); /exit edit mode; this.gvDictionary.EditIndex = -1; /begin databind this.gvBind(); 其中, 最重要的是,gvBind()函數(shù),首先將GridV

48、iew的所有列進(jìn)行清除,之后根據(jù)需要,進(jìn)行添加操作private void gvBind() dict.DictionaryTableName = this.lblTable.Text; / when table is dict_machineName,clear all gridview columns / and add new column if (this.lblTable.Text = WebUtility.DictionaryBase.DictType.Dict_MachineName.ToString() this.gvDictionary.Columns.Clear(); /define class System.Web.UI.WebControls.BoundField field; /instantiation class fi

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論