点击一个button 让datagrid 变为可以编辑的状态(加入文本框)如何做?可添加新的行或者 编辑选中的行

解决方案 »

  1.   

    in function of the button 
      datagrid.ReadOnly=false;
    and so it can be edit
      

  2.   

    编辑选中行:DataGrid1.EditItemIndex = DataGrid1.SelectedIndex;
                DataGrid1.DataSource = yourDataTable;
                DataGrid1.DataBind();编辑新行:
           yourDataTable.Rows.Add(yourDataTable.NewRow()); 
           DataGrid1.EditItemIndex = yourDataTable.Rows.Count-1;
           DataGrid1.DataSource = yourDataTable;
           DataGrid1.DataBind();
      

  3.   

    <asp:DataGrid id="ItemsGrid"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"
               AutoGenerateColumns="false"
               runat="server" Width="864px" Height="184px">         <HeaderStyle BackColor="#aaaadd">
             </HeaderStyle>
     
             <Columns>            <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit item">               <ItemStyle Wrap="False">
                   </ItemStyle>               <HeaderStyle Wrap="False">
                   </HeaderStyle>            </asp:EditCommandColumn>            <asp:ButtonColumn HeaderText="Delete item" ButtonType="LinkButton" Text="Delete" CommandName="Delete"/>  
                
                <asp:BoundColumn HeaderText="Item" ReadOnly="True" DataField="Item"/>
     
                <asp:BoundColumn HeaderText="Quantity" DataField="Qty"/>
     
                <asp:BoundColumn HeaderText="Price" DataField="Price" DataFormatString="{0:c}"/>
                
             </Columns>
     
          </asp:DataGrid>
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '在此处放置初始化页的用户代码
    GetSource()
    If Not IsPostBack Then
    BindGrid()
    End If
    End Sub Sub ItemsGrid_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles ItemsGrid.EditCommand
    ''''DataGridCommandEventArgs的Item属性的功能是:获取特定项,表现在此则为正在编辑的项
    ''''返回的是DataGridItem类的一个实例
    ItemsGrid.EditItemIndex = e.Item.ItemIndex
    BindGrid() End Sub Sub ItemsGrid_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles ItemsGrid.CancelCommand ItemsGrid.EditItemIndex = -1
    BindGrid() End Sub Sub ItemsGrid_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles ItemsGrid.UpdateCommand Dim qtyText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
    Dim priceText As TextBox = CType(e.Item.Cells(4).Controls(0), TextBox)
    Dim item As String = e.Item.Cells(2).Text
    Dim qty As String = qtyText.Text
    Dim price As String = priceText.Text
    Dim dr As DataRow
    CartView.RowFilter = "Item='" & item & "'"
    If CartView.Count > 0 Then
    CartView.Delete(0)
    End If
    CartView.RowFilter = ""
    dr = Cart.NewRow()
    dr(0) = Convert.ToInt32(qty)
    dr(1) = item
    If price.Chars(0) = "$" Then
    dr(2) = Convert.ToDouble(price.Substring(1))
    Else
    dr(2) = Convert.ToDouble(price.Substring(1))
    End If
    Cart.Rows.Add(dr)
    ItemsGrid.EditItemIndex = -1
    BindGrid() End Sub Sub BindGrid()
    ItemsGrid.DataSource = CartView
    ItemsGrid.DataBind()
    End Sub Sub GetSource()
    If Session("ShoppingCart") Is Nothing Then
    Dim dr As DataRow
    Cart.Columns.Add(New DataColumn("Qty", GetType(Int32)))
    Cart.Columns.Add(New DataColumn("Item", GetType(String)))
    Cart.Columns.Add(New DataColumn("Price", GetType(Double)))
    Session("ShoppingCart") = Cart
    Dim i As Integer
    For i = 1 To 9
    dr = Cart.NewRow()
    If (i Mod 2) <> 0 Then
    dr(0) = 2
    Else
    dr(0) = 1
    End If
    dr(1) = "Item " & i.ToString()
    dr(2) = (1.23 * (i + 1))
    Cart.Rows.Add(dr)
    Next i
    Else
    Cart = CType(Session("ShoppingCart"), DataTable)
    End If
    CartView = New DataView(Cart)
    CartView.Sort = "Item"
    Return
    End Sub Sub DeleteItem(ByVal e As DataGridCommandEventArgs)
    Dim itemCell As TableCell = e.Item.Cells(2)
    Dim item As String = itemCell.Text CartView.RowFilter = "Item='" & item + "'"
    If CartView.Count > 0 Then CartView.Delete(0) End If CartView.RowFilter = "" BindGrid() End Sub Sub ItemsGrid_Delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles ItemsGrid.DeleteCommand
    Response.Write("Commandsource:" & e.CommandSource.ToString)
    Response.Write("")
    Response.Write("CommandName:" & e.CommandName.ToString)
    '''If (CType(e.CommandSource, LinkButton)).CommandName = "Delete" Then
    DeleteItem(e)
    '''End If End SubEnd Class
      

  4.   

    模版列
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="&lt;div onclick=&quot;return confirm('确定要更新吗?')&quot;&gt;更新&lt;/div&gt;" HeaderText="更新" CancelText="取消" EditText="编辑">
    </asp:EditCommandColumn>
    cs:
    private void Db_G_news_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    Db_G_news.EditItemIndex = e.Item.ItemIndex;
        News_DataBind();
    // TextBox tb = (TextBox)e.Item.Controls[0];
    // tb.TextMode=TextBoxMode.MultiLine ;
    // tb.Rows = 5;

    }
    catch(Exception ex)
    {
    string test = ex.Message;
    Response.Write(test);
    Response.End(); }
      

  5.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=B12283DE-DB20-4322-ACCC-12724442808A
      

  6.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=B12283DE-DB20-4322-ACCC-12724442808A
      

  7.   

    yourDataTable.Rows.Add(yourDataTable.NewRow()); 
           DataGrid1.EditItemIndex = yourDataTable.Rows.Count-1;
           DataGrid1.DataSource = yourDataTable;
           DataGrid1.DataBind();  我这样操作以后  怎么才能、将DATAGRID中的编辑框 的内容取出来
      

  8.   

    我这样操作以后  怎么才能、将DATAGRID中的编辑框 的内容取出来
      

  9.   

    取DATAGRID中的编辑框 的内容:
    TextBox tmpTxt;
    DataRow dr = 你当前编辑的DataRow
    tmpTxt=(TextBox)datagrid.Items[行号].Cells[列号].Controls[0];
    dr[0] = tmpTxt.Text;
    tmpTxt=(TextBox)datagrid.Items[行号].Cells[列号].Controls[0];
    dr[1] = tmpTxt.Text;
    .....
    方法是可以的,具体的你自己调试,Controls[0]还是Controls[1]不是记得很清楚了
      

  10.   

    假设DataGrid的某一列声明如下
    <asp:TemplateColumn>
       <ItemTemplate>
          <asp:TextBox Runat="server" ID="txtID" Text='<%# DataBinder.Eval(Container.DataItem,"au_id") %>'>
          </asp:TextBox>
       </ItemTemplate>
    </asp:TemplateColumn>
    读取方法:
    TextBox txt = (TextBox)DataGrid1.Items[1].FindControl("txtID");
    Response.Write(txt.Text);
      

  11.   

    TextBox tmpTxt;
    DataRow dr = 你当前编辑的DataRow 哥哥 关键是这个怎么获得?