GridView动态绑定列

时间:2020-12-14 00:57:15
      在VS2005我们利用GridView的频率比较的大,但我们的数据列一般是不发生变化的,但在实际的工作中,我们需要对GridView的数据列进行修改,进行重新绑定,以下以进行动态绑定的代码,希望对您有所帮助,以下是HTML代码:
GridView动态绑定列GridView动态绑定列<%@ Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default" Theme="Default"
GridView动态绑定列    CodeFile
="Dictionary.aspx.cs" Inherits="Admin_Dictionary" 
%>
GridView动态绑定列
GridView动态绑定列
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
GridView动态绑定列
<html xmlns="http://www.w3.org/1999/xhtml">
GridView动态绑定列
<head runat="server">
GridView动态绑定列    
<title>数据字典配置</title>
GridView动态绑定列
</head>
GridView动态绑定列
<body>
GridView动态绑定列    
<form id="form1" runat="server">
GridView动态绑定列        
<fieldset>
GridView动态绑定列            
<legend>菜单</legend>
GridView动态绑定列            
<div id="tabsF">
GridView动态绑定列                
<ul>
GridView动态绑定列                    
<li><href="OfficeType.aspx" title="办公用品类型"><span>办公用品</span></a></li>
GridView动态绑定列                    
<li><href="AddTax.aspx" title="增值税税率数据字典"><span>增值税税率</span></a></li>
GridView动态绑定列                    
<li><href="MachineName.aspx" title="整机机种,收款计划数据字典"><span>整机机种|收款计划</span></a></li>
GridView动态绑定列                    
<li><href="Default.aspx" title="返回"><span>返回</span></a></li>
GridView动态绑定列                
</ul>
GridView动态绑定列            
</div>
GridView动态绑定列        
</fieldset>
GridView动态绑定列        
<div>
GridView动态绑定列            
<fieldset>
GridView动态绑定列                
<legend>数据字典信息</legend>&nbsp;
GridView动态绑定列                
<table width="100%">
GridView动态绑定列                    
<tr>
GridView动态绑定列                        
<td rowspan="2" valign="top" style="width: 174px">
GridView动态绑定列                            
<fieldset style="text-align: left">
GridView动态绑定列                                
<legend>数据字典项</legend>
GridView动态绑定列                                
<asp:BulletedList ID="blDataDict" runat="server" DisplayMode="LinkButton" OnClick="blDataDict_Click"
GridView动态绑定列                                    BulletStyle
="Square">
GridView动态绑定列                                    
<asp:ListItem Selected="True" Value="Dict_PayType">付款类型</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_PayModel">付款方式</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_IncType">收款类型</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_DeliveryModel">交货方式</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Customer_Type">客户类型</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_TransModel">运输方式</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_MachineName">整机名称</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Provider_Type">供应商类型</asp:ListItem>
GridView动态绑定列                                    
<asp:ListItem Value="Dict_IncPlan_Kind">收款性质类型</asp:ListItem>
GridView动态绑定列                                
</asp:BulletedList>
GridView动态绑定列                            
</fieldset>
GridView动态绑定列                        
</td>
GridView动态绑定列                        
<td>
GridView动态绑定列                            
<fieldset>
GridView动态绑定列                                
<legend>相关信息</legend>
GridView动态绑定列                                
<asp:GridView ID="gvDictionary" SkinID="Default_GridView" runat="server" CaptionAlign="Left"
GridView动态绑定列                                    OnRowCancelingEdit
="gvDictionary_RowCancelingEdit" OnRowDataBound="gvDictionary_RowDataBound"
GridView动态绑定列                                    OnRowEditing
="gvDictionary_RowEditing" OnRowUpdating="gvDictionary_RowUpdating">
GridView动态绑定列                                    
<Columns>
GridView动态绑定列                                        
<asp:BoundField DataField="DictName" HeaderText="字典名称"></asp:BoundField>
GridView动态绑定列                                        
<asp:BoundField DataField="DictCode" HeaderText="字典编号" ReadOnly="True"></asp:BoundField>
GridView动态绑定列                                        
<asp:CheckBoxField DataField="IfUsing" HeaderText="是否启用" Text="启用"></asp:CheckBoxField>
GridView动态绑定列                                        
<asp:BoundField DataField="Note" HeaderText="备注" />
GridView动态绑定列                                        
<asp:CommandField ShowEditButton="True" CausesValidation="False"></asp:CommandField>
GridView动态绑定列                                    
</Columns>
GridView动态绑定列                                
</asp:GridView>
GridView动态绑定列                            
</fieldset>
GridView动态绑定列                        
</td>
GridView动态绑定列                    
</tr>
GridView动态绑定列                    
<tr>
GridView动态绑定列                        
<td>
GridView动态绑定列                            
<fieldset>
GridView动态绑定列                                
<legend>添加数据字典信息</legend>
GridView动态绑定列                                
<br />
GridView动态绑定列                                
<table cellpadding="4" id="tableInfo" runat="server">
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td align="center" colspan="2">
GridView动态绑定列                                            
<asp:Label ID="lblTitle" runat="server" Font-Bold="True"></asp:Label></td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td class="tdbg">
GridView动态绑定列                                            
<span style="color: #ff0000">*</span>数据字典编号:</td>
GridView动态绑定列                                        
<td align="left">
GridView动态绑定列                                            
<asp:TextBox ID="txtDictCode" runat="server" Width="241px"></asp:TextBox>
GridView动态绑定列                                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtDictCode"
GridView动态绑定列                                                ErrorMessage
="数据字典编号不能为空">*</asp:RequiredFieldValidator></td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td class="tdbg">
GridView动态绑定列                                            
<span style="color: #ff0000">*</span>数据字典名称:</td>
GridView动态绑定列                                        
<td align="left">
GridView动态绑定列                                            
<asp:TextBox ID="txtDictName" runat="server" Width="241px"></asp:TextBox>
GridView动态绑定列                                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtDictName"
GridView动态绑定列                                                ErrorMessage
="数据字典名称不能为空">*</asp:RequiredFieldValidator></td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td class="tdbg">
GridView动态绑定列                                            是否启用:
</td>
GridView动态绑定列                                        
<td align="left">
GridView动态绑定列                                            
<asp:CheckBox ID="chkUsing" runat="server" Text="启用" Checked="True" />
GridView动态绑定列                                            
<asp:Label ID="lblTable" runat="server" Visible="False"></asp:Label></td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td class="tdbg">
GridView动态绑定列                                            备注:
<br />
GridView动态绑定列                                        
</td>
GridView动态绑定列                                        
<td align="left">
GridView动态绑定列                                            
<asp:TextBox ID="txtNote" runat="server" Height="60px" TextMode="MultiLine" Width="241px"></asp:TextBox></td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                    
<tr>
GridView动态绑定列                                        
<td align="center" colspan="2">
GridView动态绑定列                                            
<asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />
GridView动态绑定列                                            
<asp:Button ID="btnCancel" runat="server" Text="取消" CausesValidation="False" OnClick="btnCancel_Click" />
GridView动态绑定列                                            
<br />
GridView动态绑定列                                            
<asp:Label ID="lblInfo" runat="server"></asp:Label>
GridView动态绑定列                                        
</td>
GridView动态绑定列                                    
</tr>
GridView动态绑定列                                
</table>
GridView动态绑定列                            
</fieldset>
GridView动态绑定列                        
</td>
GridView动态绑定列                    
</tr>
GridView动态绑定列                
</table>
GridView动态绑定列            
</fieldset>
GridView动态绑定列            
<fieldset>
GridView动态绑定列                
<legend>业务逻辑</legend>
GridView动态绑定列                
<ul>
GridView动态绑定列                    
<li><span style="color: red">*</span>项不能为空</li>
GridView动态绑定列                
</ul>
GridView动态绑定列            
</fieldset>
GridView动态绑定列        
</div>
GridView动态绑定列        
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
GridView动态绑定列            ShowSummary
="False" />
GridView动态绑定列    
</form>
GridView动态绑定列
</body>
GridView动态绑定列
</html>
GridView动态绑定列
以上HTML,代码和普通的页面代码很相似,本页面完成的功能是,通过点击左边的链接,动态的调用,不同的表中的数据,但是,表中的字段名称,有的是不同的,因此需要我根据实际情况,进行动态绑定,以下页面代码:
GridView动态绑定列using System;
GridView动态绑定列
using System.Data;
GridView动态绑定列
using System.Configuration;
GridView动态绑定列
using System.Collections;
GridView动态绑定列
using System.Web;
GridView动态绑定列
using System.Web.Security;
GridView动态绑定列
using System.Web.UI;
GridView动态绑定列
using System.Web.UI.WebControls;
GridView动态绑定列
using System.Web.UI.WebControls.WebParts;
GridView动态绑定列
using System.Web.UI.HtmlControls;
GridView动态绑定列
GridView动态绑定列
public partial class Admin_Dictionary : System.Web.UI.Page
GridView动态绑定列GridView动态绑定列
{
GridView动态绑定列    
//define class
GridView动态绑定列
    WebUtility.DictionaryBase dict=new WebUtility.DictionaryBase();
GridView动态绑定列    
protected void Page_Load(object sender, EventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
if (!Page.IsPostBack)
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
//instantiation class
GridView动态绑定列
            dict.DictionaryTableName = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString();
GridView动态绑定列            
this.lblTable.Text = WebUtility.DictionaryBase.DictType.Dict_PayType.ToString();
GridView动态绑定列            
//set datasource
GridView动态绑定列
            this.gvDictionary.Caption = "付款类型"+" 数据字典列表";
GridView动态绑定列            
this.lblTitle.Text = "添加付款类型  数据字典";
GridView动态绑定列            
this.gvDictionary.DataSource = dict.DataSetDictionary();
GridView动态绑定列            
this.gvDictionary.DataBind();
GridView动态绑定列        }

GridView动态绑定列    }

GridView动态绑定列    
protected void blDataDict_Click(object sender, BulletedListEventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
GridView动态绑定列        
//set caption and title
GridView动态绑定列
        this.gvDictionary.Caption = this.GetCaption(this.blDataDict.Items[e.Index].Text);
GridView动态绑定列        
this.lblTitle.Text = this.GetTitle(this.blDataDict.Items[e.Index].Text);
GridView动态绑定列        
//clear add item information
GridView动态绑定列
        this.ClearInfo();
GridView动态绑定列        
//set tablename to label
GridView动态绑定列
        this.lblTable.Text = this.blDataDict.Items[e.Index].Value;
GridView动态绑定列        
this.gvBind();
GridView动态绑定列        
//modify add info area
GridView动态绑定列

GridView动态绑定列    }

GridView动态绑定列    
private string GetCaption(string strItem)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
return strItem+" 数据字典列表";
GridView动态绑定列    }

GridView动态绑定列    
private string GetTitle(string strItem)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
return " 添加" + strItem + " 数据字典";
GridView动态绑定列    }

GridView动态绑定列    
private void ClearInfo()
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
this.txtDictCode.Text = "";
GridView动态绑定列        
this.txtDictName.Text = "";
GridView动态绑定列        
this.txtNote.Text = "";
GridView动态绑定列        
this.lblInfo.Text = "";
GridView动态绑定列    }

GridView动态绑定列    
private int GetValue(bool bol)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
if (bol)
GridView动态绑定列            
return 1;
GridView动态绑定列        
else
GridView动态绑定列            
return 0;
GridView动态绑定列    }

GridView动态绑定列    
protected void btnCancel_Click(object sender, EventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
this.ClearInfo();
GridView动态绑定列    }

GridView动态绑定列    
protected void btnSave_Click(object sender, EventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        dict.DictionaryTableName 
= this.lblTable.Text;
GridView动态绑定列        
if (dict.IsDictionary(this.txtDictCode.Text))
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
this.lblInfo.ForeColor = System.Drawing.Color.Red;
GridView动态绑定列            
this.lblInfo.Text = this.txtDictCode.Text + " 编号已经存在,请重新填写";
GridView动态绑定列            
return;
GridView动态绑定列        }

GridView动态绑定列        
//set value to member
GridView动态绑定列
        dict.DictionCode = this.txtDictCode.Text;
GridView动态绑定列        dict.DictionName 
= this.txtDictName.Text;
GridView动态绑定列        dict.Note 
= this.txtNote.Text;
GridView动态绑定列        dict.IfUsing 
= this.GetValue(this.chkUsing.Checked);
GridView动态绑定列        
//transfer member to add operate
GridView动态绑定列
        int i = this.dict.AddDictionary();
GridView动态绑定列        
if (i > 0)
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
this.ClearInfo();
GridView动态绑定列            
this.lblInfo.ForeColor = System.Drawing.Color.Green;
GridView动态绑定列            
this.lblInfo.Text = "数据字典添加操作成功";
GridView动态绑定列            
this.gvBind();
GridView动态绑定列        }

GridView动态绑定列        
else
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
this.lblInfo.ForeColor = System.Drawing.Color.Red;
GridView动态绑定列            
this.lblInfo.Text = "数据字典添加操作失败!";
GridView动态绑定列        }

GridView动态绑定列    }

GridView动态绑定列    
private void gvBind()
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        dict.DictionaryTableName 
= this.lblTable.Text;
GridView动态绑定列        
// when table is dict_machineName,clear all gridview columns
GridView动态绑定列        
// and add new column
GridView动态绑定列
        if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Dict_MachineName.ToString())
GridView动态绑定列GridView动态绑定列        

GridView动态绑定列            
this.gvDictionary.Columns.Clear();
GridView动态绑定列            
//define class
GridView动态绑定列
            System.Web.UI.WebControls.BoundField field;
GridView动态绑定列            
//instantiation class
GridView动态绑定列
            field = new BoundField();
GridView动态绑定列            field.DataField 
= "TypeName";
GridView动态绑定列            field.HeaderText 
= "整机类型";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "Type";
GridView动态绑定列            field.HeaderText 
= "整机编号";
GridView动态绑定列            field.ReadOnly 
= true;
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CheckBoxField chkField 
= new CheckBoxField();
GridView动态绑定列            chkField.DataField 
= "IfUsing";
GridView动态绑定列            chkField.Text 
= "启用";
GridView动态绑定列            chkField.HeaderText 
= "是否启用";
GridView动态绑定列            
this.gvDictionary.Columns.Add(chkField);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "Note";
GridView动态绑定列            field.HeaderText 
= "备注";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CommandField cmdField 
= new CommandField();
GridView动态绑定列            cmdField.ShowEditButton 
= true;
GridView动态绑定列            cmdField.CausesValidation 
= false;
GridView动态绑定列            
this.gvDictionary.Columns.Add(cmdField);
GridView动态绑定列        }

GridView动态绑定列        
//if table is Customer_Type or Provider_Type
GridView动态绑定列
        else if (this.lblTable.Text == WebUtility.DictionaryBase.DictType.Customer_Type.ToString() ||
GridView动态绑定列            
this.lblTable.Text == WebUtility.DictionaryBase.DictType.Provider_Type.ToString())
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
this.gvDictionary.Columns.Clear();
GridView动态绑定列            
//define class
GridView动态绑定列
            System.Web.UI.WebControls.BoundField field;
GridView动态绑定列            
//instantiation class
GridView动态绑定列
            field = new BoundField();
GridView动态绑定列            field.DataField 
= "TypeName";
GridView动态绑定列            field.HeaderText 
= "类型名称";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "TypeCode";
GridView动态绑定列            field.HeaderText 
= "类型编号";
GridView动态绑定列            field.ReadOnly 
= true;
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CheckBoxField chkField 
= new CheckBoxField();
GridView动态绑定列            chkField.DataField 
= "IfUsing";
GridView动态绑定列            chkField.Text 
= "启用";
GridView动态绑定列            chkField.HeaderText 
= "是否启用";
GridView动态绑定列            
this.gvDictionary.Columns.Add(chkField);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "Note";
GridView动态绑定列            field.HeaderText 
= "备注";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CommandField cmdField 
= new CommandField();
GridView动态绑定列            cmdField.ShowEditButton 
= true;
GridView动态绑定列            cmdField.CausesValidation 
= false;
GridView动态绑定列            
this.gvDictionary.Columns.Add(cmdField);
GridView动态绑定列        }

GridView动态绑定列        
else
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            
this.gvDictionary.Columns.Clear();
GridView动态绑定列            
//define class
GridView动态绑定列
            System.Web.UI.WebControls.BoundField field;
GridView动态绑定列            
//instantiation class
GridView动态绑定列
            field = new BoundField();
GridView动态绑定列            field.DataField 
= "DictName";
GridView动态绑定列            field.HeaderText 
= "字典名称";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "DictCode";
GridView动态绑定列            field.HeaderText 
= "字典编号";
GridView动态绑定列            field.ReadOnly 
= true;
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CheckBoxField chkField 
= new CheckBoxField();
GridView动态绑定列            chkField.DataField 
= "IfUsing";
GridView动态绑定列            chkField.Text 
= "启用";
GridView动态绑定列            chkField.HeaderText 
= "是否启用";
GridView动态绑定列            
this.gvDictionary.Columns.Add(chkField);
GridView动态绑定列
GridView动态绑定列            field 
= new BoundField();
GridView动态绑定列            field.DataField 
= "Note";
GridView动态绑定列            field.HeaderText 
= "备注";
GridView动态绑定列            
this.gvDictionary.Columns.Add(field);
GridView动态绑定列
GridView动态绑定列            System.Web.UI.WebControls.CommandField cmdField 
= new CommandField();
GridView动态绑定列            cmdField.ShowEditButton 
= true;
GridView动态绑定列            cmdField.CausesValidation 
= false;
GridView动态绑定列            
this.gvDictionary.Columns.Add(cmdField);
GridView动态绑定列        }

GridView动态绑定列        
this.gvDictionary.DataSource = dict.DataSetDictionary();
GridView动态绑定列        
this.gvDictionary.DataBind();
GridView动态绑定列    }

GridView动态绑定列
GridView动态绑定列    
protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
if (e.Row.RowType == DataControlRowType.DataRow)
GridView动态绑定列GridView动态绑定列        
{
GridView动态绑定列            e.Row.Attributes.Add(
"onmouseover""e=this.style.backgroundColor; this.style.backgroundColor='linen'");
GridView动态绑定列            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=e");
GridView动态绑定列        }

GridView动态绑定列
GridView动态绑定列    }

GridView动态绑定列    
protected void gvDictionary_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
this.gvDictionary.EditIndex = -1;
GridView动态绑定列        
this.gvBind();
GridView动态绑定列    }

GridView动态绑定列    
protected void gvDictionary_RowEditing(object sender, GridViewEditEventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        
this.gvDictionary.EditIndex = e.NewEditIndex;
GridView动态绑定列        
this.gvBind();
GridView动态绑定列    }

GridView动态绑定列    
protected void gvDictionary_RowUpdating(object sender, GridViewUpdateEventArgs e)
GridView动态绑定列GridView动态绑定列    
{
GridView动态绑定列        TextBox txtName 
= (TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[0].Controls[0];
GridView动态绑定列        TextBox txtNote 
= (TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[3].Controls[0];
GridView动态绑定列        CheckBox chkIfUsing 
= (CheckBox)this.gvDictionary.Rows[e.RowIndex].Cells[2].Controls[0];
GridView动态绑定列        
//get value to member;
GridView动态绑定列
        dict.DictionaryTableName = this.lblTable.Text;
GridView动态绑定列        dict.DictionName 
= txtName.Text;
GridView动态绑定列        dict.DictionCode 
= this.gvDictionary.Rows[e.RowIndex].Cells[1].Text;
GridView动态绑定列        dict.IfUsing 
= this.GetValue(chkIfUsing.Checked);
GridView动态绑定列        dict.Note 
= txtNote.Text;
GridView动态绑定列        
//transfer method begin update
GridView动态绑定列
        dict.UpdateDictionary();
GridView动态绑定列        
//exit edit mode;
GridView动态绑定列
        this.gvDictionary.EditIndex = -1;
GridView动态绑定列        
//begin databind
GridView动态绑定列
        this.gvBind();
GridView动态绑定列    }
 
GridView动态绑定列}

GridView动态绑定列

其中, 最重要的是,gvBind()函数,首先将GridView的所有列进行清除,之后根据需要,进行添加操作
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
            field = new BoundField();
            field.DataField = "TypeName";
            field.HeaderText = "整机类型";
            this.gvDictionary.Columns.Add(field);

            field = new BoundField();
            field.DataField = "Type";
            field.HeaderText = "整机编号";
            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(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 = false;
            this.gvDictionary.Columns.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.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 = "类型编号";
            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(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 = false;
            this.gvDictionary.Columns.Add(cmdField);
        }
        else
        {
            this.gvDictionary.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 = "DictCode";
            field.HeaderText = "字典编号";
            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(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 = false;
            this.gvDictionary.Columns.Add(cmdField);
        }
        this.gvDictionary.DataSource = dict.DataSetDictionary();
        this.gvDictionary.DataBind();
    }