1. 程式人生 > >動態新增TemplateField列

動態新增TemplateField列

GridViewTextTemplate類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
///GridViewTextTemplate 的摘要說明
/// </summary>
public class GridViewTextTemplate : System.Web.UI.ITemplate
{
    private DataControlRowType templateType;

    private string columnName;

    private string cId;


    public GridViewTextTemplate(DataControlRowType type, string colname, string controlId)
    {

        templateType = type;

        columnName = colname;

        cId = controlId;

    }


    public void InstantiateIn(System.Web.UI.Control container)
    {

        // Create the content for the different row types.

        switch (templateType)
        {

            case DataControlRowType.Header:

                // Create the controls and set id properties to put in the header
                Literal myHeadLiteral = new Literal();

                myHeadLiteral.ID = cId;

                myHeadLiteral.Text = "<B>" + columnName + "</B>";


                container.Controls.Add(myHeadLiteral);
                break;

            case DataControlRowType.DataRow:

                // Create the controls and set id properties to put in a data row

                TextBox myTextBox = new TextBox();

                myTextBox.ID = cId;

                myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding);

                myTextBox.Width = 100;
                container.Controls.Add(myTextBox);

                break;

            default:

                // Insert code to handle unexpected values.

                break;

        }

    }


    private void TextBoxDataBinding(Object sender, EventArgs e)
    {

        TextBox myTextBox = (TextBox)sender;

        GridViewRow row = (GridViewRow)myTextBox.NamingContainer;


        myTextBox.Text = System.Web.UI.DataBinder.Eval(row.DataItem, columnName).ToString();

    }

}
在後臺GridView動態新增TemplateField:

	//parameter_description繫結的資料 textParameterDesc是Text的ID
        TemplateField tField;
        tField = new TemplateField();
        tField.HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "說明", "textParameterDescHd");
        tField.ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "parameter_description", "textParameterDesc");
        GridView1.Columns.Add(tField);
或者:
GridView1.Columns.Add(new TemplateField() { HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "序號", ""), ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "PlanSN", "") });

相關推薦

動態新增TemplateField

GridViewTextTemplate類: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI.WebContro

動態新增到DataTable的第一

 dt.Columns.Add(new DataColumn("序號", Type.GetType("System.String")));  dt.Columns["序號"].SetOrdinal(0);  for (int i = 0; i < dt.Rows.Count; i++)  {     

GridView動態新增模板並自動繫結資料

 protected void ForeachCheckBox()         {             gv.Columns.Clear();             BoundField bf = null;             TemplateField t

DataGrid動態新增模板的一個例子

namespace csdn {  /// <summary>  /// WebForm30 的摘要說明。  /// </summary>  public class WebForm30 : System.Web.UI.Page  {   protected System.Web.UI

GridView中動態新增模板和其子控制元件集合

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Security; 7 using Sy

c#動態新增band 新增 新增模板程式碼示例

private DevExpress.XtraGrid.Views.BandedGrid.GridBand band;         private DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gridCol

淺談js中如何動態新增表頭/表/表格內容

我想很多童鞋用js動態向表格中新增資料很熟悉,而且也覺得非常簡單!是的,對於寫頁面的童鞋來說,最喜歡寫查詢的頁面了,動態向表格繫結資料。用for迴圈就可以輕鬆搞定。 如果我們的業務需求有所變化,可能我們要的資料就不是這樣一條一條的中規中矩的。如果你還是新手,又

datagridview動態新增和行以及新增之後重新整理操作

有的時候需要動態的新增datagridview 的列名: System.Windows.Forms.DataGridViewTextBoxColumn[] Column = new DataGridViewTextBoxColumn[columnNum];

Extjs 實現動態新增gridPanel的和資料

最近幾天在我們的.net專案中有一個單據要實現根據使用者選擇條件動態新增gridPanel的對應列和資料。 以前的頁面及資料實現是在頁面上直接顯示gridPanel控制元件,並對應一個store。資料則在cs中賦值。這次則改在js中建立gridPanel物件,render到

datagridview 動態新增和行

       dataGridView1.ReadOnly = true ;      //禁用編輯功能 方法一:通過手動新增Datatable,再繫結dataGridView DataTable dt = new DataTable();//建立個數據表 dt.Colu

ASP.NET 2.0 中動態新增 GridView 模板的例子

<%...@ Page Language="C#"%><%...@ Import Namespace="System.Data"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www

Alert 新增,可重復執行的

where eight span blank code dynamic sco sys .com 向數據表新增一個列 https://stackoverflow.com/questions/12678208/altering-sql-table-to-add-column

給Grid動態添加和添加樣式

geb project idt 選中 center itl check actual plus BudgetProjectSearch: function () { $("#load").show(); var that = th

C# DataGridVie利用model特性動態加載

count() each typeof 動態加載 else 界面 text nco add 今天閑來無事看到ORm的特性映射sql語句。我就想到datagridview也可以用這個來動態添加列。這樣就不用每次都去界面上點開界面填列了。 代碼簡漏希望有人看到了能指點一二。 先

批量動態新增分區表

range分區 麻煩 creat 條件篩選 exc interval errors pro rom mysql分區表人為添加不可控性太大,如果出現人員變動交接不利,或者遺忘,會導致業務數據丟失或寫入失敗,同時如果使用分區表較多,逐個添加分區也是挺麻煩的 推薦使用存儲過程+e

oracle 存儲過程-動態行轉,解決。

pla gin sel rom con left join from blog creat 包頭 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure A

vue.set動態新增對象屬性,觸發dom渲染

bject tro 數據 his col def style this lec 當我們給一個props或者data中被觀測的對象添加一個新的屬性的時候,不能直接添加,必須使用Vue.set方法 /** * ==== 選擇產品 ==== * 因為vue實

Spotfire報表維護經驗——新增計算而不修改基本資料

客戶要求將一個view改回原來的樣子(即,不適用與新需求)。由於上次的修改採用了新增計算列的方法,所以,只需要將檢視中的統計指標改回原來的列即可,修改起來挺方便的。 試想,如果未採用新增計算列的方法,而是通過Information Link修改了基本資料,那這次改動就沒這麼容易了。 可見

runtime實戰(二)動態新增方法

runtime的實戰二就是動態新增方法 一:建立一個Person類 我們在Person類的標頭檔案宣告一個eat方法,並不去實現它,當我們呼叫時,會出現如下的崩潰棧 2017-05-01 20:43:06.483 SH_Runtime[2133:104550] -[Pers

Unity UGUI按鈕動態新增點選事件

使用程式碼的方式給按鈕新增點選事件需要引入兩個名稱空間: using UnityEngine.UI; using UnityEngine.Events; 先建立一個介面,並且在介面裡放置一個按鈕: 建立一個Test指令碼,程式碼如下: using