繁体   English   中英

从一个数据表中筛选值以传输到另一个数据表

[英]Filtering values from a Datatable to be transferred to another Datatable

我有一个具有复选框和值的数据表,我想要的是,当我单击按钮时,它将获得所有复选框及其对应的值,并将它们添加到空白数据表中。 我不打算保存从1个列表移动到另一个列表的数据,因为将提供另一种功能来确认是否要保存更改(此问题中未询问)。

这是我的html代码:

  <asp:ListView runat="server" ID="uoListViewAirportSaved" EmptyDataText="No Data Found">
                <LayoutTemplate>
                <table border="0" cellpadding="0" cellspacing="0" width="500px">
                    <tr>
                       <th runat="server">&nbsp;</th>
                    <th runat="server">Airport</th>
                    </tr>
                    <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td class="leftAligned">
                             <asp:CheckBox ID="uoCheckBoxSelect" runat="server" />
                    </td>
                    <td class="leftAligned">
                                <asp:HiddenField ID="uoHiddenFieldAirport" runat="server" Value='<%# Eval("ColAirportCodeVarchar") %>' />
                                <asp:Label ID="uoLabelAirport" runat="server" Text='<%# Eval("colAirportFullName")%>' />                         
                    </td>
                </tr>
            </ItemTemplate>
            <EmptyDataTemplate>
                <table border="0" cellpadding="0" cellspacing="0"  width="500px">
                    <tr>
                        <td>Airport</td>
                    </tr>
                    <tr>
                        <td colspan="3" class="leftAligned">No Record</td>
                    </tr>
                </table>
            </EmptyDataTemplate>                
            </asp:ListView>

和单击添加按钮时的代码:

    private void AddAirport() {
        CheckBox uoCheckBoxSelect;
        HiddenField uoHiddenFieldAirport;
        Label uoLabelAirport;

        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();

        DataTable dt3 = new DataTable();

        string serviceProvider = GlobalCode.Field2String(Request.QueryString["pid"]);
        ///datatable with all list of airports
        dt = VendorMaintenanceBLL.GetServiceProviderAirportbyBrand(serviceProvider);
        ///datatable with provider's current airports
        dt2 = VendorMaintenanceBLL.GetServiceProviderAirportbyVendor(serviceProvider);


        foreach(ListViewItem item in uoListViewAirport.Items)
        {
            uoCheckBoxSelect = (CheckBox)item.FindControl("uoCheckBoxSelect");
            if (uoCheckBoxSelect.Checked == true)
            {
                uoHiddenFieldAirport = (HiddenField)item.FindControl("uoHiddenFieldAirport");
                uoLabelAirport = (Label)item.FindControl("uoLabelAirport");

                /// my new list should contain  the values from the checked values of the checkboxes
                        ???

            }
        }

    }

我的数据表包含AirportID和AirportStringName列。

我找到了我要复制的代码,逻辑上。 但是,它是为列表而设计的:

            List<AirportDTO> listToBeAdded = new List<AirportDTO>();
    List<AirportDTO> listAdded = new List<AirportDTO>();

    listToBeAdded = GetAirportNotInUser(false, false);
    listAdded = GetAirportInUser(false);

    foreach (ListViewItem item in uoListViewAirport.Items)
    {
        uoCheckBoxSelect = (CheckBox)item.FindControl("uoCheckBoxSelect");
        if (uoCheckBoxSelect.Checked == true)
        {
            uoHiddenFieldAirport = (HiddenField)item.FindControl("uoHiddenFieldAirport");
            uoLabelAirport = (Label)item.FindControl("uoLabelAirport");

            var listToAdd = (from a in listToBeAdded
                             where a.AirportIDString == GlobalCode.Field2String(uoHiddenFieldAirport.Value)
                             select new
                             {
                                 AirportID = a.AirportIDString,
                                 AirportName = a.AirportNameString,
                             }).ToList();
        }
    }

我不知道如何从数据表中获取必要的数据,因为它与列表不同。 有没有一种方法可以对DataTables进行处理列表的方法。

由于昨天确实需要解决此问题,因此我决定执行以下操作。

我所做的只是基本的for循环,然后在内部比较每一行,然后仅使用Datatable.ImportRowDataTable.RemoveAt 我还为更改后的列表添加了一个临时数据表。

        foreach(ListViewItem item in uoListViewAirport.Items)
        {

            uoCheckBoxSelect = (CheckBox)item.FindControl("uoCheckBoxSelect");
            if (uoCheckBoxSelect.Checked == true)
            {
                uoHiddenFieldAirport = (HiddenField)item.FindControl("uoHiddenFieldAirport");
                uoLabelAirport = (Label)item.FindControl("uoLabelAirport");

               string  HiddenFieldAirport = GlobalCode.Field2String(uoHiddenFieldAirport.Value);

               for (var f = 0; f < dt.Rows.Count; f++ )
               {

                   if (dt.Rows[f]["colAirportIDInt"].ToString() == HiddenFieldAirport)
                   {   
                       dt3.ImportRow(dt.Rows[f]);
                       dt.Rows.RemoveAt(f);
                   }
               }

            }

        }

代码很脏,加上我也必须对其进行排序,但是由于我确实需要它,所以我可以解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM