0

我想在网格中添加一个文本框列。我做了下面的编码。但它不起作用

    var box = (TextBox)sender;
    int p = int.Parse(box.Text);
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("details", typeof(TextBox)));
    // dt.Columns.Add(new DataColumn("count", typeof(int)));
    for (int k = 1; k <= p; k++)
    {
        TextBox tt = new TextBox();
        tt.ID = "TT";
        dr = dt.NewRow();
        dr["details"] =(TextBox) tt;
        // dr["count"] = string.Empty;
        dt.Rows.Add(dr);
    }

    ViewState["CurrentTable"] = dt;
    GridView1.DataSource = dt;
    GridView1.DataBind();

任何人都可以帮我这样做吗?提前致谢。

4

2 回答 2

1

尝试这个

//---------------My c# Code Behind file--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_init(object sender, EventArgs e)
{
GenerateDataGrids();
if (!Page.IsPostBack)
{ }
}
public void GenerateDataGrids()
{
//-- Instantiate the data set and table
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add();
//-- Add columns to the data table
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Book", typeof(string));
dt.Columns.Add("Author", typeof(string));
//-- Add rows to the data table
dt.Rows.Add(1, "1984", "George Orwell");
dt.Rows.Add(2, "Notes from the Underground", "Fydor Dostoyevsky");
dt.Rows.Add(3, "The Outsider", "Albert Camus");
dt.Rows.Add(4, "Post Office", "Charles Buchowski");
dt.Rows.Add(5, "The Chant of Maldoror", "Comte De Lautremont");
DataGrid dg = new DataGrid();
dg.ID = ID;
dg.DataSource = ds;
dg.AutoGenerateColumns = false;
DataTable Workdt = new DataTable();
Workdt = ds.Tables[0];
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
// Creating Template Column
TemplateColumn tc = new TemplateColumn();
string columnName = dt.Columns[i].ColumnName;
tc.HeaderTemplate = new DataGridTemplate(ListItemType.Header, columnName);
for (int j = 1; j <= dt.Rows.Count - 1; j++)
{
string RowCallName = dt.Rows[j][i].ToString();
tc.ItemTemplate = new DataGridTemplate(ListItemType.EditItem, RowCallName);
}
dg.Columns.Add(tc);
}
dg.DataBind();
form1.Controls.Add(dg);
}
}
public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<B>" + columnName + "</B>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
lc.Text = "Item " + columnName;
container.Controls.Add(lc);
break;
case ListItemType.EditItem:
TextBox tb = new TextBox();
tb.Text = columnName;
container.Controls.Add(tb);
break;
case ListItemType.Footer:
lc.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lc);
break;
}
}
}

<%----------------The ASP.NET File-------------------------%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
于 2013-05-20T08:56:05.610 回答
0

它不起作用,因为您正在尝试添加TextBoxDataRow. 您可以在网格设计器中将文本框添加到网格TemplateField

<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">

    <EditItemTemplate>

       <asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("FirstName") %>'></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

        <asp:Label ID="Label1" runat="server" Text='<%#Bind("FirstName") %>'></asp:Label>

    </ItemTemplate>

</asp:TemplateField>

或动态(现在不在计算机后面,所以需要测试):

var colTxt = new TemplateField();
colTxt.ItemTemplate = new TextBoxColumn();
gridView1.Columns.Add(colTxt);

添加类TextBoxColumn:

public class TextBoxColumn : ITemplate 
{
      public void InstantiateIn(Control container)
      {
          var txt = new TextBox();
          txt.Id = "TT";
          container.Controls.Add(txt);
      }
}
于 2013-05-20T08:03:42.327 回答