【问题标题】:how to convert Gridview to Datatable如何将 Gridview 转换为 Datatable
【发布时间】:2008-11-27 06:37:29
【问题描述】:

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

有没有什么简单的方法可以将 gridview 复制到数据表中?

实际上,在我的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”循环的帮助下遍历网格。
我担心它是否会影响性能?
能否请您告诉我任何其他将GridView 复制到数据表的简单方法

【问题讨论】:

    标签: gridview visual-studio-2005 datatable


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,Sergiu Damian,我明白你的意思......但我不知道如何以双向方式获取数据......你能举个例子吗?
    • 您可以将数据绑定到GridView,您可以一次编辑一条记录。基本上,在编辑每一行之后,必须先保存它,然后才能编辑另一行。一个好的起点:msdn.microsoft.com/en-us/magazine/cc163933.aspx
    • 嗨...实际上在我的表中我有 3 行...第一列是一个文本框,所以我可以在保存按钮单击之前编辑所有三行...您发送的链接仅用于一次编辑一行...感谢您的时间...我通过使用两个会话解决了这个问题...
    • 使用两个会话是什么意思?
    • 在页面加载时,我已将原始网格值保存到会话中。然后单击保存按钮,我再次阅读了网格并保存在另一个会话中,之后我将新会话与旧会话进行了比较。如果发生任何修改,则只有该值将保存在数据库中。
    【解决方案2】:

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

    【讨论】:

    • 我已经给出了上面的程序...检查上一个答案...我们可以在会话的帮助下编辑gridview
    【解决方案3】:

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

    在添加按钮点击时,

    如果 Session("blnFlag") = False 那么 会话(“blnFlag”)=真 别的 获取页脚() 结束如果

        Grid1.FooterRow.Visible = True
    

    点击保存按钮,

    将 intOldCount 调暗为整数 将 intNewCount 调暗为整数 将 dt 调暗为新数据表 将 strQuery 变暗为字符串 intOldCount = CType(会话(“intOldCount”),整数) 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() 将 tx1 调暗为文本框 将 tx2 调暗为文本框 将 drp 调暗为 DropDownList tx1 = CType(Grid1.FooterRow.FindControl("txt1"), 文本框) tx2 = CType(Grid1.FooterRow.FindControl("txt2"), 文本框) 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
    

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2020-09-04
      • 2014-03-16
      • 1970-01-01
      相关资源
      最近更新 更多