~~~~如何在DataGrid模板中动态添加一个文本框~~~~请高手赐教~~~~~谢谢~~~~~

解决方案 »

  1.   

    /// </summary>
    private void BindData()
    {
    try
    {
    this.tr_blink.Visible = true;
    this.tr_detail.Visible = false; DataTable detailInfo = null; // Not Found in Sessoin 
    // We research the result
    if( this.Session["dsp_EnqTransFares_Head"] == null || this.Session["dsp_EnqTransFares_Detail"] == null )
    {
    // First intialize the page
    if( this.hid_SelectValue.Value == string.Empty )
    {
    return;
    }
    else
    {
    EnqTransFaresDB  fareDB = new EnqTransFaresDB( this.UserInfo.LangCode );
    detailInfo = fareDB.GetFareInfo( this.hid_SelectValue.Value , this.UserInfo.CompCode , DateTime.Parse(hid_EffiveDate.Value) );

    this.Session["dsp_EnqTransFares_Detail"] = detailInfo;
    }
    }
    else
    {
    detailInfo = (DataTable)this.Session["dsp_EnqTransFares_Detail"];
    } detailInfo.DefaultView.Sort = this.hid_SortField.Value + this.hid_SortType.Value;
    DataView detailView = detailInfo.DefaultView; this.dg_FareHead.DataSource = detailView;
    this.dg_FareHead.DataBind(); // Build DataGrid Dynamically
    // Remove All existed Columns
    while( this.dg_FareDetail.Columns.Count != 0 )
    {
    this.dg_FareDetail.Columns.RemoveAt(0);
    } // Add columns according to the dateSource
    // start from NO.1 , skip the first column (Destination)
    for( int i = 1 ; i < detailInfo.Columns.Count ; i ++ )
    {
    BoundColumn column = new BoundColumn(); column.DataField = detailInfo.Columns[i].ColumnName;
    column.HeaderText = detailInfo.Columns[i].ColumnName;
    column.SortExpression = detailInfo.Columns[i].ColumnName;
    column.DataFormatString = "{0:n2}"; column.HeaderStyle.Wrap = false;
    column.HeaderStyle.Font.Underline = true;
    column.ItemStyle.HorizontalAlign = HorizontalAlign.Right; dg_FareDetail.Columns.Add(column);
    } dg_FareDetail.DataSource = detailInfo;
    dg_FareDetail.DataBind(); // Reset DataGrid Color
    foreach( DataGridItem dgi in dg_FareDetail.Items )
    {
    for( int i = 0 ; i < dgi.Cells.Count ; i ++ )
    {
    // start from NO.1 , skip the first column (Destination)
    if( detailView[dgi.ItemIndex][i+1].ToString() == string.Empty )
    {
    // Using Grey Color
    dgi.Cells[i].BackColor = Color.FromArgb(221,221,221);
    }
    }
    } this.tr_blink.Visible = false;
    this.tr_detail.Visible = true;
    }
    catch( ApplicationException ex )
    {
    ComLogOutput.WriteLogFile(ex);
    this.lbl_Err.Text = ex.Message;
    }
    catch( Exception ex )
    {
    ComLogOutput.WriteLogFile(ex);
    }
    }
      

  2.   

    <ASP:DATAGRID id="dg_FareDetail" RUNAT="server" CSSCLASS="DATAGRID_DEFAULT" CELLPADDING="1" ALLOWPAGING="false" AUTOGENERATECOLUMNS="False" CellSpacing="0" AllowSorting="True">
    <EDITITEMSTYLE HORIZONTALALIGN="Center" CSSCLASS="Result_DG_EditItemStyle"></EDITITEMSTYLE>
    <ALTERNATINGITEMSTYLE HORIZONTALALIGN="Center" CSSCLASS="Result_DG_AlternatingItemStyle"></ALTERNATINGITEMSTYLE>
    <ITEMSTYLE HORIZONTALALIGN="Center" CSSCLASS="Result_DG_ItemStyle" Wrap="False"></ITEMSTYLE>
    <HEADERSTYLE HORIZONTALALIGN="Center" CSSCLASS="Result_DG_HeaderStyle" Wrap="False"></HEADERSTYLE>
    <Columns>
    </Columns>
    </ASP:DATAGRID>
      

  3.   

    TextBox txtbox = new TextBox();
    TemplateColumn tem = new TemplateColumn();
    tem.ItemTemplate.InstantiateIn(txtbox );