2008-11-27 99 views
1

我在我的应用程序中使用GridView来填充数据。如何将Gridview转换为Datatable

有没有简单的方法将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 

在这里,我通过“每个”循环遍历网格。
我很担心它是否会影响性能?
您可以请给我电话任何其他简单的方法将GridView复制到数据表

回答

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

在页面加载,

康恩=新OleDb.OleDbConnection(“供应商= 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") 

添加按钮点击,

如果会议(” blnFlag“)= false,那么 会议( “blnFlag”)= TRUE 否则 getFooter() 结束如果

Grid1.FooterRow.Visible = True 

上保存按钮点击,

昏暗intOldCount作为整数 昏暗intNewCount作为整数 昏暗DT作为新的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() 昏暗TX1作为文本框 昏暗TX2作为文本框 昏暗DRP IM作为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 
1

最好的方法是使用数据绑定。如果您设法让双向数据绑定起作用,那么您的DataTable会自动更新。性能明智,你可能会获得动态生成表格的最佳速度,其中你的文本框有一个Id,你可以在回发中轻松地解释并保存更改,而不需要GridView使用ViewState或恢复它的状态和触发所有事件。

+0

喜的Sergiu达米安 我明白你说...但我不知道如何来以双向方式获取数据... 请问您可以举个例子吗? – sona 2008-12-01 10:11:01

+0

您可以将数据绑定到GridView,您可以一次编辑一条记录。基本上,在编辑每行之后,必须先保存才能编辑另一行。 一个很好的起点:http://msdn.microsoft.com/en-us/magazine/cc163933.aspx – 2008-12-02 13:54:39

1

如何在不修改数据源使用DataSet和DataTable的GridView的数据