我在我的应用程序中使用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”循环的帮助下遍历网格
我担心它是否影响性能?
你能否告诉我任何其他简单方法将GridView
复制到数据表
答案 0 :(得分:1)
最好的方法是使用数据绑定。如果您设法使双向数据绑定工作,您的DataTable会自动更新。
性能方面,您可能会从动态生成的表中获得最佳速度,其中您的文本框具有可以在回发上轻松解释的ID并保存更改,而无需GridView使用ViewState或恢复其状态和触发器所有活动。
答案 1 :(得分:1)
如何使用没有数据源的数据集和数据表编辑gridview上的数据
答案 2 :(得分: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; Data Source = 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”)=真 其他 getFooter() 结束如果
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) 如果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() 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