如何将Gridview转换为Datatable

如何将Gridview转换为Datatable,gridview,visual-studio-2005,datatable,Gridview,Visual Studio 2005,Datatable,我在应用程序中使用GridView填充数据 有没有简单的方法将gridview复制到datatable 实际上,在我的GridView中,有一个控件是textbox。 所以我可以随时编辑该控件。。。我需要的是在按钮上单击我在GridView中所做的任何更改都必须复制到一个数据表中 我是用密码做的 dt = CType(Session("tempTable"), DataTable) i = 0 For Each rows As GridViewRow In Grid1.Rows Dim

我在应用程序中使用
GridView
填充数据

有没有简单的方法将gridview复制到datatable

实际上,在我的
GridView
中,有一个控件是textbox。
所以我可以随时编辑该控件。。。我需要的是在按钮上单击我在
GridView
中所做的任何更改都必须复制到一个数据表中

我是用密码做的

dt = CType(Session("tempTable"), DataTable) 
i = 0 For Each rows As GridViewRow In Grid1.Rows 
   Dim txt As TextBox 
   txt = CType(rows.FindControl("txt"), TextBox) 
   dt.Rows(i)(1) = txt.Text
   i = i + 1 
Next
在这里,我借助“for each”循环遍历网格。
我担心它是否会影响性能?

您能告诉我将
GridView
复制到datatable

的其他简单方法吗?更好的方法是使用数据绑定。如果您设法使双向数据绑定工作,那么您的数据表将自动更新

就性能而言,您可能会从动态生成的表中获得最佳速度,其中文本框具有一个Id,您可以在回发时轻松解释并保存更改,而无需GridView使用ViewState或还原其状态并触发所有事件。

html页面看起来

                            <asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="False" GridLines="None">
                                <Columns>
                                    <asp:TemplateField HeaderText="ID">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl1" runat="server" Text='<%#Bind("ID") %>' CssClass="rowHeader"></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txt1" runat="server" Text='<%#Bind("ID") %>'></asp:TextBox>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Description">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txt" runat="server" Text='<%#Bind("Description") %>'></asp:TextBox>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txt2" runat="server" Text='<%#Bind("Description") %>'></asp:TextBox>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Comments">
                                        <ItemTemplate>
                                            <asp:Label ID="Comments" runat="server" Text='<%#Bind("Comments") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="Drop1" runat="server">
                                                <asp:ListItem>v1</asp:ListItem>
                                                <asp:ListItem>v2</asp:ListItem>
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

                            <asp:Button ID="btnAdd" runat="server" Text="Add" />
                            <asp:Button ID="btnSave" runat="server" Text="Save" />
单击“添加”按钮

如果Session(“blnFlag”)=False,则 会话(“blnFlag”)=真 其他的 getFooter() 如果结束

    Grid1.FooterRow.Visible = True
单击“保存”按钮

Dim intOldCount为整数 Dim intNewCount为整数 Dim dt作为新数据表 作为字符串的Dim strQuery intOldCount=CType(会话(“intOldCount”),整数) 如果Session(“blnFlag”)=True,则

        getFooter()
        dt = CType(Session("tempTable"), DataTable)
        intNewCount = dt.Rows.Count

        If intOldCount = intNewCount Then
            Dim tx1 As TextBox
            Dim tx2 As TextBox
            Dim drp As DropDownList
            tx1 = CType(Grid1.FooterRow.FindControl("txt1"), TextBox)
            tx2 = CType(Grid1.FooterRow.FindControl("txt2"), TextBox)
            drp = CType(Grid1.FooterRow.FindControl("Drop1"), DropDownList)

            strQuery = "INSERT INTO Emp (ID,Description,Comments) values ('" + tx1.Text + "','" + tx2.Text + "','" + drp.SelectedValue + "')"
            Dim Cmd As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd.ExecuteNonQuery()
            conn.Close()

        Else
            For i = intOldCount To intNewCount - 1
                Dim strId As String
                Dim strDesc As String
                Dim strComm As String
                strId = dt.Rows(i)(0).ToString()
                strDesc = dt.Rows(i)(1).ToString()
                strComm = dt.Rows(i)(2).ToString()

                strQuery = "INSERT INTO Emp (ID,Description,Comments) values ('" + strId + "','" + strDesc + "','" + strComm + "')"
                Dim Cmd As New OleDb.OleDbCommand(strQuery, conn)
                conn.Open()
                Cmd.ExecuteNonQuery()
                conn.Close()
            Next
        End If

        For i = 0 To intOldCount - 1
            Dim strId As String
            Dim strDesc As String
            strId = dt.Rows(i)(0).ToString()
            strDesc = dt.Rows(i)(1).ToString()
            strQuery = "update Emp set Description = '" + strDesc + "' where ID = '" + strId + "'"

            Dim Cmd1 As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd1.ExecuteNonQuery()
            conn.Close()
        Next

        ds = New DataSet()
        Dim CmdData As New OleDb.OleDbDataAdapter("Select * from Emp", conn)
        CmdData.Fill(ds, "Employee")
        Grid1.DataSource = ds.Tables("Employee").DefaultView
        Grid1.DataBind()

        Session("blnFlag") = False
    Else
        dt = CType(Session("tempTable"), DataTable)
        i = 0
        For Each rows As GridViewRow In Grid1.Rows
            Dim txt As TextBox
            txt = CType(rows.FindControl("txt"), TextBox)
            dt.Rows(i)(1) = txt.Text
            i = i + 1
        Next
        Session("tempTable") = dt

        For i = 0 To intOldCount - 1
            Dim strId As String
            Dim strDesc As String
            strId = dt.Rows(i)(0).ToString()
            strDesc = dt.Rows(i)(1).ToString()
            strQuery = "update Emp set Description = '" + strDesc + "' where ID = '" + strId + "'"

            Dim Cmd1 As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd1.ExecuteNonQuery()
            conn.Close()
        Next

        Grid1.DataSource = dt.DefaultView
        Grid1.DataBind()

    End If
我使用一个函数,比如

公共函数getFooter() 将tx1设置为文本框 将tx2设置为文本框 作为下拉列表的Dim drp tx1=CType(Grid1.FooterRow.FindControl(“txt1”),文本框) tx2=CType(Grid1.FooterRow.FindControl(“txt2”),文本框) drp=CType(Grid1.FooterRow.FindControl(“Drop1”),DropDownList)


如何在没有数据源的情况下使用dataset和datatable在gridview上编辑数据

嗨,Sergiu Damian,我理解你说的…但我不知道如何以双向方式获取数据。。。你能举个例子告诉我吗?你可以将数据绑定到GridView,你可以一次编辑一条记录。基本上,在编辑每一行之后,必须先保存该行,然后才能编辑另一行。一个好的起点:嗨。。。实际上,在我的表格中,我有3行。第一列中有一个文本框,所以我可以在单击“保存”按钮之前编辑所有三行。。。您发送的链接一次仅用于编辑一行。。。谢谢你的时间。。。我通过使用两个会话解决了这个问题…使用两个会话是什么意思?在页面加载时,我已将原始网格值保存到会话中。然后单击save按钮,我再次读取了网格并保存在另一个会话中,之后我将新会话与旧会话进行了比较。若发生任何修改,那个么只有那个值会保存在数据库中。我已经给出了上面的程序。。。检查前面的答案。。。我们可以在会话的帮助下编辑gridview
        getFooter()
        dt = CType(Session("tempTable"), DataTable)
        intNewCount = dt.Rows.Count

        If intOldCount = intNewCount Then
            Dim tx1 As TextBox
            Dim tx2 As TextBox
            Dim drp As DropDownList
            tx1 = CType(Grid1.FooterRow.FindControl("txt1"), TextBox)
            tx2 = CType(Grid1.FooterRow.FindControl("txt2"), TextBox)
            drp = CType(Grid1.FooterRow.FindControl("Drop1"), DropDownList)

            strQuery = "INSERT INTO Emp (ID,Description,Comments) values ('" + tx1.Text + "','" + tx2.Text + "','" + drp.SelectedValue + "')"
            Dim Cmd As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd.ExecuteNonQuery()
            conn.Close()

        Else
            For i = intOldCount To intNewCount - 1
                Dim strId As String
                Dim strDesc As String
                Dim strComm As String
                strId = dt.Rows(i)(0).ToString()
                strDesc = dt.Rows(i)(1).ToString()
                strComm = dt.Rows(i)(2).ToString()

                strQuery = "INSERT INTO Emp (ID,Description,Comments) values ('" + strId + "','" + strDesc + "','" + strComm + "')"
                Dim Cmd As New OleDb.OleDbCommand(strQuery, conn)
                conn.Open()
                Cmd.ExecuteNonQuery()
                conn.Close()
            Next
        End If

        For i = 0 To intOldCount - 1
            Dim strId As String
            Dim strDesc As String
            strId = dt.Rows(i)(0).ToString()
            strDesc = dt.Rows(i)(1).ToString()
            strQuery = "update Emp set Description = '" + strDesc + "' where ID = '" + strId + "'"

            Dim Cmd1 As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd1.ExecuteNonQuery()
            conn.Close()
        Next

        ds = New DataSet()
        Dim CmdData As New OleDb.OleDbDataAdapter("Select * from Emp", conn)
        CmdData.Fill(ds, "Employee")
        Grid1.DataSource = ds.Tables("Employee").DefaultView
        Grid1.DataBind()

        Session("blnFlag") = False
    Else
        dt = CType(Session("tempTable"), DataTable)
        i = 0
        For Each rows As GridViewRow In Grid1.Rows
            Dim txt As TextBox
            txt = CType(rows.FindControl("txt"), TextBox)
            dt.Rows(i)(1) = txt.Text
            i = i + 1
        Next
        Session("tempTable") = dt

        For i = 0 To intOldCount - 1
            Dim strId As String
            Dim strDesc As String
            strId = dt.Rows(i)(0).ToString()
            strDesc = dt.Rows(i)(1).ToString()
            strQuery = "update Emp set Description = '" + strDesc + "' where ID = '" + strId + "'"

            Dim Cmd1 As New OleDb.OleDbCommand(strQuery, conn)
            conn.Open()
            Cmd1.ExecuteNonQuery()
            conn.Close()
        Next

        Grid1.DataSource = dt.DefaultView
        Grid1.DataBind()

    End If
    Dim dr As DataRow
    Dim dt As DataTable
    dt = CType(Session("tempTable"), DataTable)

    dr = dt.NewRow()
    dr("ID") = tx1.Text
    dr("Description") = tx2.Text
    dr("Comments") = drp.SelectedValue
    dt.Rows.Add(dr)

    i = 0
    For Each rows As GridViewRow In Grid1.Rows
        Dim txt As TextBox
        txt = CType(rows.FindControl("txt"), TextBox)
        dt.Rows(i)(1) = txt.Text
        i = i + 1
    Next

    Grid1.DataSource = dt.DefaultView
    Grid1.DataBind()

    Session("tempTable") = dt
End Function