2

编码问题:

将具有控件的 Asp.Net GridView 转换回数据表

现实世界的问题:

谷歌没有我能找到并实施的任何真正有用的答案

Asp.Net 网格视图:

<asp:GridView ID="gvCompanies" runat="server" AutoGenerateColumns="False" AllowSorting="True" OnSorting="gridView_Sorting">
            <Columns>
            <asp:TemplateField HeaderText = "Comapny Id"><ItemTemplate><%# Eval("Company Id")%></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "New Company Id"><ItemTemplate><asp:TextBox ID="TextBox5" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Company Name"><ItemTemplate><asp:TextBox ID="TextBox6" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Branch Id"><ItemTemplate><asp:TextBox ID="TextBox7" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Location Id"><ItemTemplate><asp:TextBox ID="TextBox8" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Location Name"><ItemTemplate><asp:TextBox ID="TextBox9" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Prefix"><ItemTemplate><asp:TextBox ID="TextBox10" runat ="server" ></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Sort"><ItemTemplate><asp:TextBox ID="TextBox11" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Cash In Transit"><ItemTemplate><asp:TextBox ID="TextBox12" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Cash Sub In Transit"><ItemTemplate><asp:TextBox ID="TextBox13" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Cash Account"><ItemTemplate><asp:TextBox ID="TextBox14" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Cash Sub Account"><ItemTemplate><asp:TextBox ID="TextBox15" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "Payment Method"><ItemTemplate><asp:TextBox ID="TextBox16" runat ="server"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText = "AppDB"><ItemTemplate><%# Eval("AppDB")%></ItemTemplate></asp:TemplateField>
            </Columns>
            </asp:GridView
4

2 回答 2

4

解决方案:

  1. gvCompanies 是 GridView
  2. dtCompanies 是原始的 DataTable
  3. 可以为不同的控件类型添加更多 if else

    protected DataTable ConvertToDataTable()
    {
        DataTable TempTable = new DataTable();
        TempTable = dtCompanies.Clone();
    
        foreach (GridViewRow row in gvCompanies.Rows)
        {
             DataRow TempRow = TempTable.NewRow();
    
             for (int i = 0; i < row.Cells.Count; i++)
             {
                 if (row.Cells[i].Controls[0].GetType().Equals(typeof(DataBoundLiteralControl)))
                 {
                     TempRow[i] = ((DataBoundLiteralControl)row.Cells[i].Controls[0] as DataBoundLiteralControl).Text;
                 }
                 else if (row.Cells[i].Controls[0].GetType().Equals(typeof(TextBox)))
                 {
                     TempRow[i] = ((TextBox)row.Cells[i].Controls[0]).Text;
                 }
             }
             TempTable.Rows.Add(TempRow);
        }
        return TempTable;
    }
    
于 2012-11-30T20:17:26.780 回答
0

Dim Rectab As New DataTable '取一个新的数据表

       '------Add Columns into your DataTable----------------
        For k As Integer = 0 To CalibSchdNameAry.Length - 1 
            Rectab.Columns.Add(CalibSchdNameAry(k))
        Next
       '-----------------------------------------------------
       '------Add Rows Data Into Your Datatable From Html Table-------
        For i As Integer = 0 To ClbTab.Rows.Count - 1
            Rectab.Rows.Add()
            For j As Integer = 0 To CalibSchdNameAry.Length - 1
                If i + 1 < ClbTab.Rows.Count - 1 Then
                    Rectab.Rows(i).Item(CalibSchdNameAry(j)) = ClbTab.Rows(i + 1).Cells(j).Text
                End If
            Next


        Next

'--------现在您的数据表可以使用了----------------Suresh.A------

于 2017-04-06T10:37:48.173 回答