1

GridView在我的应用程序中用于填充数据。

有什么简单的方法可以将 gridview 复制到 datatable 吗?

实际上,在我GridView的控件之一是文本框。
所以我可以随时编辑该控件...我需要的是在按钮上单击我所做的任何更改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”循环的帮助下遍历网格。
我担心它是否会影响性能?
你能告诉我任何其他将a复制GridView到数据表的简单方法吗

4

3 回答 3

1

最好的方法是使用数据绑定。如果您设法让双向数据绑定工作,您的 DataTable 会自动更新。

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

于 2008-11-27T12:33:26.460 回答
1

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

于 2009-04-03T07:50:26.903 回答
0

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" />

在页面加载时,

conn = New OleDb.OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; 数据源 = D:\GDD_Work\Employee.mdb")

    If Not Page.IsPostBack Then

        ViewState("intCount") = 0
        Session("blnFlag") = False

        Dim Cmd As New OleDb.OleDbDataAdapter("Select * from Emp", conn)
        Cmd.Fill(ds, "Employee")

        Grid1.DataSource = ds.Tables("Employee")
        Grid1.DataBind()

        Session("intOldCount") = ds.Tables("Employee").Rows.Count
        Session("tempTable") = ds.Tables("Employee")

在添加按钮单击时,

If Session("blnFlag") = False Then Session("blnFlag") = True Else getFooter() End If

    Grid1.FooterRow.Visible = True

点击保存按钮,

Dim intOldCount As Integer Dim intNewCount As Integer Dim dt As New DataTable Dim strQuery As String intOldCount = CType(Session("intOldCount"), Integer) If Session("blnFlag") = True Then

        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() 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)

    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
于 2008-12-15T08:54:20.903 回答