首先设置你的dataGrid存放textBox的列的宽度,然后才能控制textbox的宽度
在dataGrid_itemDataBand事件中初始化textBox的宽度
假设你的textbox放在第5列
dataGrid每一列的宽度可以在前端直接设置.
if (E.item.itemIndex > 0)
{
  TextBox txtBox = new TextBox();
  txtBox = (TextBox)E.item.cell(5).controls(0);
  txtBox.attributes.add("width","20px");
  //可能是:txtBox.width = "20";
}

解决方案 »

  1.   

    Specified argument was out of the range of valid values. Parameter name: index 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: indexSource Error: 
    Line 198:{
    Line 199:TextBox txtBox = new TextBox();
    Line 200:txtBox = (TextBox)e.Item.Cells[2].Controls[0];
    Line 201:txtBox.Attributes.Add("Width","20px");
    Line 202://可能是:txtBox.width = "20";
     
    出错了
      

  2.   

    in ItemDataBound event:if (e.Item.ItemType == ListItemType.EditItem)
    {
      TextBox txt = (TextBox)e.Item.Cells[0].Controls[0];
      txt.Width = Unit.Pixel(100);
    }
      

  3.   

    两种办法:
    1.使用模板列,下面是我使用的一个例子
    asp:TemplateColumn HeaderText="Fe(ppb)">
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
    <ItemTemplate>
    <asp:Label Width="82px" Text='<%# DataBinder.Eval(Container.DataItem, "FE") %>' runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="tbFE" Width="82px" Text='<%# DataBinder.Eval(Container.DataItem, "FE") %>' Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    2.如果不是模板列,可以在编辑按钮的事件处理程序中代码改变TextBox的属性
    ((TextBox)DataGrid.Items[i].FindControl("textbox")).Width=Unit.Pixel(82);
      

  4.   

    习惯使用模版列,在.aspx中可以利用Width属性控制