DataGrid列显示如下:  DataGrid的列是运行时自动创建的. 订单编号 款式 总数量 Size1 Size2 Size 3
1,我是想在DataGrid动态绑定后,动态添加textbox模板列到 Size1,Size2,Size3
2,DataGrid的所有行都显示出textbox,达到同时多行编辑,而不是一次编辑时才把textbox显示出来

解决方案 »

  1.   

    new你的column,类型自定义,你可以选BoundColumn,然后将你的column添加到DataGrid,你可以这样:DataGrid.Columns.add(你的column),好像是这么写的
    DataGrid的所有行都显示出textbox好像默认的就是可以的。
      

  2.   

    AutoGernalColumns = true的话好象是没有办法解决的
    我现在用AutoGernalColumns = false然后用lovecherry的http://www.cnblogs.com/lovecherry/archive/2005/03/26/126102.html方法实现了我的问题.现在有个问题就是,更新时没有办法一次多行更新,因为DataGrid是动态添加的,不能用((TextBox)e.Item.FindControl("idname")).Text来循环取数据.我如果要一次更新多行,该怎么办??
      

  3.   

    在DataGrid的ItemCreated事件里添加,这样在你的按钮事件里就可以获取该控件的数值了if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
     TableCell tc = new TableCell();
     e.Item.Cells.Add(tc); TextBox tb = new TextBox();
     tb.ID = "idname";
     tc.Controls.Add(tb);
    }
      

  4.   

    http://blog.csdn.net/cuike519/archive/2003/12/28/19331.aspx
      

  5.   

    谢谢思归!
    我把DataGrid的AutoGernalColumns = true,然后用你的方法,只会在最后一列生产模板列.还有数据要怎么绑定到textbox???
      

  6.   

    >>>只会在最后一列生产模板列instead of 
     TableCell tc = new TableCell();
     e.Item.Cells.Add(tc);try
     TableCell tc = new TableCell();
     e.Item.Cells.AddAt(n,tc); //change n
    >>>还有数据要怎么绑定到textbox???
    add an ItemDataBound event handler for your datagrid, then
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
     
     TextBox tb = e.Item.FindControl("idname") as TextBox;
     tb.Text = DataBinder.Eval(e.Item.DataItem,"SomeColumName").ToString();}
      

  7.   

    我在ItemCreated里Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            BandGrid()
        End Sub    Private Sub BandGrid()
            Dim myDataSet As New DataSet
            Dim Sql As String = "select * from orderSize"
            Dim i As Int16
            Dim FieldName As String
            myDataSet = DBAccess.SqlHelper.ExecuteDataset(getConnectionString, CommandType.Text, Sql)
            DataGrid1.DataSource = myDataSet.Tables(0)
            DataGrid1.DataBind()
        End Sub下面是ItemCreated的代码If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
         Dim i As Byte
         For i = 0 To e.Item.Cells.Count - 1
             Dim tc As New TableCell
             e.Item.Cells.AddAt(i, tc)
             Dim tb As New TextBox
             tb.ID = i
             tc.Controls.Add(tb)
          Next
       End If运行后DataGrid的列会多出一倍,是不是要设置AutoGernalColumns =false,
    请思归继续关注,谢谢!
      

  8.   

    奇怪,你为什么要重新再生成一遍所有的Cell呢?
    For i = 0 To e.Item.Cells.Count - 1
             Dim tc As New TableCell
             e.Item.Cells.AddAt(i, tc)
    ???不明白你要干什么, 如果你的目的是要把Size1 Size2 Size 3中的数据放在TextBox里,那么你应该设置这样的Template<asp:TemplateColumn HeaderText="Size1">
     <ItemTemplate>
     <asp:TextBox id="txtSize1" Text='<%# DataBinder.Eval(Container.DataItem, "Size1") %>' runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn><asp:TemplateColumn HeaderText="Size2">
     <ItemTemplate>
     <asp:TextBox id="txtSize2" Text='<%# DataBinder.Eval(Container.DataItem, "Size2") %>' runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Size3">
     <ItemTemplate>
     <asp:TextBox id="txtSize3" Text='<%# DataBinder.Eval(Container.DataItem, "Size3") %>' runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    然后在按钮事件里,这么做for each dgi as DataGridItem in YourDataGrid.Items
      dim txt1 as TextBox = DirectCast(dgi.FindControl("txtSize1"), TextBox)
      dim txt2 as TextBox = DirectCast(dgi.FindControl("txtSize2"), TextBox)
      dim txt3 as TextBox = DirectCast(dgi.FindControl("txtSize3"), TextBox)
      '....next还有,你应该这么做初始绑定Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If not Page.IsPostBack then
            BandGrid()
      end if
    End Sub
      

  9.   

    因为我的Size1,Size2,Size3也是不固定的,所以不能直接在前台设置.
    可能会出现只有Size1 , 也可能会出现 Size1,Size2,Size3,Size4,Size5.
    我是想在各Size列加入Textbox.
    也就是我之前所说的,在绑定数据源到DataGrid后,再在DataGrid的列里加TextBox
      

  10.   

    我是想在DataGrid的每一列都加入一个TextBox
      

  11.   

    For i = 0 To e.Items.Count - 1  //遍历行
        Dim tb As New TextBox 
        e.Item(i).cell(Size1在第几列).controls.add(tb)
    next
    每一个cell里面都有很多控件,如果你不想要其他的空间,你可以在add前clear:e.Item(i).cell(Size1在第几列).controls.clear大概是这么写的,具体情况你自己查一下帮助,调试一下。