if(e.Item.ItemType==ListItemType.EditItem)
{
((TextBox)e.Item.Cells[3].Controls[0]).Width=Unit.Pixel(320);//文本框宽度
((TextBox)e.Item.Cells[3].Controls[0]).CssClass="textface";//文本框样式表
((TextBox)e.Item.Cells[3].Controls[0]).ForeColor=Color.Red;//文字颜色
}

解决方案 »

  1.   

    在DataGrid右击,选编辑模版列,看到TextBox了吗!改他的属性就OK了!
      

  2.   

    在DataGrid右击,选编辑模版列,看到TextBox了吗!改他的属性就OK了!
    我找不到唉!
    还有我vb做的
    ((TextBox)e.Item.Cells[3].Controls[0]).Width=Unit.Pixel(320);
    这里的(TextBox)vb怎么转啊
      

  3.   

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <%@ Page Debug=True %>
    <script language="VB" runat="server">
        Dim i as Integer
    Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath(".")+"/db/kk.mdb"
        Sub Page_Load(Src As Object, E As EventArgs)
            If Not (IsPostBack)
                BindGrid()
            End If
        End Sub    Sub BindGrid()
            Dim myConnection as New OleDbConnection (strConn)
            Dim DS As DataSet
            Dim MyCommand As OleDbDataAdapter
            MyCommand = new OleDbDataAdapter("SELECT boardid,boardtype,class FROM [board] ORDER BY boardid ASC", MyConnection)
            DS = new DataSet()
            MyCommand.Fill(DS, "board")
            MyDataGrid.DataSource=DS.Tables("board").DefaultView
            MyDataGrid.DataBind()
        End Sub
     
        Public Sub MyDataGrid_Cancel(sender As Object, e As DataGridCommandEventArgs)
            MyDataGrid.EditItemIndex = -1
            BindGrid()      
        End Sub    Sub MyDataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
    MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
            BindGrid()   
        End Sub    Public Sub MyDataGrid_Update(sender As Object, e As DataGridCommandEventArgs)
           Dim txtCount As TextBox = CType(e.Item.Cells(1).Controls(0), TextBox)
           Response.Write("<FONT COLOR=#CC0000>" & txtCount.Text & "</FONT>")
        End Sub    Sub MyDataGrid_Delete(sender As Object, e As DataGridCommandEventArgs)
           dim strID as string =  MyDataGrid.DataKeys(e.Item.ItemIndex)
           Response.write(strID)
        End Sub Sub myItemDataBound(sender as Object , e as DataGridItemEventArgs)
    If e.Item.ItemType=ListItemType.EditItem then
       For i = 0 to e.Item.Cells.Count - 3
        Dim txt As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
        txt.BorderColor = System.Drawing.Color.red
        txt.BorderWidth = Unit.Pixel(1)
        txt.BackColor = System.Drawing.Color.FromName("#D4D4D4")
    If i mod 2 <> 0 then 
       txt.Width=Unit.Percentage(100)
    Else 
       txt.Width=Unit.Percentage(70)
                    End If
                Next i
        End If
        End Sub Sub myItemCreated(Sender As Object, e As DataGridItemEventArgs)
         If e.Item.ItemType=ListItemType.Item or e.Item.ItemType=ListItemType.AlternatingItem or e.Item.ItemType=ListItemType.EditItem then
        Dim myTableCell As TableCell
        myTableCell = e.Item.Cells(4)
            Dim myDeleteButton As LinkButton
            myDeleteButton = myTableCell.Controls(0)
              myDeleteButton.Attributes.Add("onclick", "return confirm('Are you Sure you want to delete this recoders?');")
     End If
         End Sub
    </script>
    <Form runat="server" ID="editDataGrid">
        <ASP:DataGrid id="MyDataGrid" runat="server"
          Width="300"
          ShowFooter="false"
          OnCancelCommand="MyDataGrid_Cancel" 
          OnUpdateCommand="MyDataGrid_Update" 
          OnEditCommand="MyDataGrid_Edit" 
      OnDeleteCommand="MyDataGrid_Delete"
          AutoGenerateColumns="false"
      DataKeyField="boardid"
      OnItemDataBound="myItemDataBound"
      OnItemCreated="myItemCreated">
           <Columns>
           <asp:BoundColumn DataField="boardid" HeaderText="ID"/>
               <asp:BoundColumn DataField="boardtype" HeaderText="Type"/>
       <asp:BoundColumn DataField="class" HeaderText="Class"/>
               <asp:editcommandcolumn edittext="Edit" canceltext="Cancel" updatetext="Update" itemstyle-wrap="false"/>
               <asp:buttoncolumn text="Delete" commandname="Delete" />
           </Columns>
          </ASP:DataGrid>
    </Form>
      

  4.   

    好像不是textbox,textbox在里面,样式还好。
    就是外面的框撑得很大的
      

  5.   

    ItemDataBound中写
    if(e.Item.ItemType == ListItemType.EditItem)
    {
    TextBox tb = (TextBox)e.Item.Cells[5].Controls[0];
    tb.TextMode=TextBoxMode.MultiLine ;
                                      根据你的需要设置吧

    }
      

  6.   

    谢谢各位。textbox现在清楚了
    就是列一多,比如7列,编辑的时候,里面的textbox和外面的边框当中有很大的一块
    列数少的时候,textbox和外面的边框只有很小的一丝,挺不错的。
    不知道这个能不能修改
      

  7.   

    在页面中定义TEXTBOX的CSS样式
    <style>
    input{width:96%;}
    </style>
    这样Textbox就与单元格大小差不多呢!
      

  8.   

    好的,看到效果了。但是为什么我的button变得很长很长了:(
      

  9.   

    应该是就datagrid使用这个样式吧,该怎么放?
      

  10.   

    将datagird中的编辑、更新、取消的按纽类型改为linkButton而非Pushbutton
      

  11.   

    好的,看到效果了。但是为什么我的button变得很长很长了:(将datagird中的编辑、更新、取消的按纽类型改为linkButton而非Pushbutton
      

  12.   

    另外一种解决方案是在DATAGRID中用模板,然后定义编辑项的TEXTBOX的CSSCLASS属性
      

  13.   

    不是这个按钮,是放在网页中的其他按钮
    我把样式放在<head></head>之间对巴
    这样对整个页面都有效了