none
DataGrid Edit(Update)的问题 RRS feed

  • 问题

  •  

    1.获取绑定列中的数据
    1)处于非编辑状态、或者数据项处于编辑状态但该绑定列只读
      e.Item.Cells[列索引].Text

    2)处于编辑状态
    绑定列如果处于编辑状态,则会显示为TextBox控件形式
      ((TextBox)(e.Item.Cells[列索引].Controls[0])).Text

    关于第2点,写成vb.net
    CType(e.Item.Celss(i).Controls(0),TextBox).Text

     

    ---DataGrid 的信息

            <aspBig SmileataGrid ID="dgData" runat="server" AutoGenerateColumns ="False"
                Width="700px" OnEditCommand ="dg_Edit" OnCancelCommand ="dg_Cancel" OnDeleteCommand ="dg_Delete"
                OnUpdateCommand ="dg_Update" Font-Bold="False"
                Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                Font-Underline="False"
                HorizontalAlign="Center" ShowFooter ="true"
                DataKeyField="ID"  >
                <Columns >
                    <asp:TemplateColumn HeaderText ="ID">
                        <ItemTemplate >
                            <asp:Label runat="server" Text ='<%# container.dataitem("id") %>' />
                           
                        </ItemTemplate>
                    </asp:TemplateColumn>
               
               
                    <asp:boundColumn  HeaderText ="科目内容" datafield="mainSubject"  />
      
                   
                    <asp:EditCommandColumn EditText ="编辑" CancelText ="取消" UpdateText ="更新"
                        HeaderText ="编辑"  ItemStyle-Wrap ="false"  >
    <ItemStyle Wrap="False"></ItemStyle>
                    </asp:EditCommandColumn>
                </Columns>
           
           
            </aspBig SmileataGrid>

     

     

        Sub dg_Update(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
            Dim sql As String

            sql = "Update mainSubject set mainSubject='" & CType(e.Item.Cells(1).Controls(0), TextBox).Text & "' where id =" & dgData.DataKeys(e.Item.ItemIndex)
            Response.Write(CType(e.Item.Cells(1).Controls(0), TextBox).Text & "<br />")
            Response.Write(sql)
            Dim objCmd As OleDbCommand = New OleDbCommand(sql, objConn)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

            dgData.EditItemIndex = -1
            dgData.DataBind()


        End Sub

     

    为什么不能获取编辑后的文本内容。不能够更新。点击更新后还是原来的值!!不变!!!

    2008年12月25日 6:47

答案

  • 你好,

    你下面这段代码没有制定数据源。你应该指定更新后的数据源然后再绑定试试.

    Code Snippet

    Sub dg_Update(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)   

      Dim sql As String


            sql = "Update mainSubject set mainSubject='" &
    CType(e.Item.Cells(1).Controls(0), TextBox).Text & "' where id ="
    & dgData.DataKeys(e.Item.ItemIndex)



            Response.Write(CType(e.Item.Cells(1).Controls(0), Label).Text & "<br />")



            Response.Write(sql)



            Dim objCmd As OleDbCommand = New OleDbCommand(sql, objConn)


            objConn.Open()



            objCmd.ExecuteNonQuery()



            objConn.Close()


            dgData.EditItemIndex = -1


    //重新指定数据源再绑定

            dgData.DataBind()



    End Sub

         


    2008年12月29日 4:07