Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用asp.net中的多个字段对DDL进行排序_C#_Asp.net - Fatal编程技术网

C# 无法使用asp.net中的多个字段对DDL进行排序

C# 无法使用asp.net中的多个字段对DDL进行排序,c#,asp.net,C#,Asp.net,我正在尝试对动态创建的DDL进行排序。此DDL包含位置1、位置2和位置3字段的组合 用于创建DDL的代码如下所示: ddlLocation.Items.Clear(); dtLocation = dataSource.GetFilteredRiskInfo("location"); ddlLocation.Items.Insert(0, new ListItem("All", "-1")); foreach (DataRow row

我正在尝试对动态创建的DDL进行排序。此DDL包含位置1、位置2和位置3字段的组合

用于创建DDL的代码如下所示:

        ddlLocation.Items.Clear();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in dtLocation.Rows)
        {
            if (this.ddlLocation.Items.FindByText(row["Location1"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location1"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location2"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location2"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location3"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location3"].ToString()));
        }
            ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        sortedDt.Columns.Add("Location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now sort these now that they're all in the same column
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt.DefaultView.ToTable(); // should be the new sorted table

        // now your original code, but modified to populate the ddl with the new sorted data


        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }
           //Location
        ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        sortedDt.Columns.Add("Location");
        dtLocation = dataSource.GetFilteredRiskInfo("location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now make them distinct & sort them now that they're all in the same column
        sortedDt = sortedDt.DefaultView.ToTable(/*distinct*/ true);
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt = sortedDt.DefaultView.ToTable();

        // now your original code, but modified to populate the ddl with the new sorted data
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }
以下代码适用于位置1(我认为)

我一直在研究如何添加多个字段,即:location2和location3,它不起作用。代码本身显示Location1和Location2的错误(“名称“Location1”在当前上下文中不存在”)

所有的帮助都将是非常感激的

位置列表需要如下所示。假设这些位置具有以下数据

地点1: 悉尼,卧龙岗珀斯

地点2: 阿德莱德,北领地

地点3: 布里斯班、堪培拉、霍巴特

我需要组合列表如下所示: 阿德莱德 布里斯班 堪培拉 霍巴特 北领地 珀斯 悉尼 卧龙岗

我的代码现在如下所示:

        ddlLocation.Items.Clear();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in dtLocation.Rows)
        {
            if (this.ddlLocation.Items.FindByText(row["Location1"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location1"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location2"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location2"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location3"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location3"].ToString()));
        }
            ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        sortedDt.Columns.Add("Location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now sort these now that they're all in the same column
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt.DefaultView.ToTable(); // should be the new sorted table

        // now your original code, but modified to populate the ddl with the new sorted data


        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }
           //Location
        ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        sortedDt.Columns.Add("Location");
        dtLocation = dataSource.GetFilteredRiskInfo("location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now make them distinct & sort them now that they're all in the same column
        sortedDt = sortedDt.DefaultView.ToTable(/*distinct*/ true);
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt = sortedDt.DefaultView.ToTable();

        // now your original code, but modified to populate the ddl with the new sorted data
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }

现在列表按字母顺序排列,但有重复条目。我需要能够删除重复项/使条目不同。

不仅应该按照FrankO的建议在GetFilteredRiskInfo中对其进行预排序,而且听起来好像必须将3个位置列合并为一个。如果您可以在GetFilteredRiskInfo中这样做,它可能会更干净,如果不能,您可以尝试将列和代码排序与以下内容结合起来:

    // new table to combine the 3 columns of dtLocation into a one column datatable
    DataTable sortedDt = new DataTable();
    sortedDt.Columns.Add("Location");

    // combining columns
    foreach (DataRow row in dtLocation.Rows)
    {
        sortedDt.Rows.Add(row["Location1"]);
        sortedDt.Rows.Add(row["Location2"]);
        sortedDt.Rows.Add(row["Location3"]);
    }

    // now sort these now that they're all in the same column
    sortedDt.DefaultView.Sort = "Location";
    sortedDt = sortedDt.DefaultView.ToTable(); // should be the new sorted table

    // now your original code, but modified to populate the ddl with the new sorted data
    ddlLocation.Items.Clear();
    dtLocation = dataSource.GetFilteredRiskInfo("location");
    ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
    foreach (DataRow row in sortedDt.Rows)
    {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
    }
感谢FronkO(我现在在所有下拉列表中都有排序列表)和madamission(在位置排序方面的额外帮助)。在你们两个和一些来自互联网的帮助下,我现在让它工作了。工作守则如下:

        ddlLocation.Items.Clear();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in dtLocation.Rows)
        {
            if (this.ddlLocation.Items.FindByText(row["Location1"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location1"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location2"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location2"].ToString()));
            if (this.ddlLocation.Items.FindByText(row["Location3"].ToString()) == null)
                this.ddlLocation.Items.Add(new ListItem(row["Location3"].ToString()));
        }
            ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        dtLocation = dataSource.GetFilteredRiskInfo("location");
        sortedDt.Columns.Add("Location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now sort these now that they're all in the same column
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt.DefaultView.ToTable(); // should be the new sorted table

        // now your original code, but modified to populate the ddl with the new sorted data


        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }
           //Location
        ddlLocation.Items.Clear();
        // new table to combine the 3 columns of dtLocation into a one column datatable
        DataTable sortedDt = new DataTable();
        sortedDt.Columns.Add("Location");
        dtLocation = dataSource.GetFilteredRiskInfo("location");

        // combining columns
        foreach (DataRow row in dtLocation.Rows)
        {
            sortedDt.Rows.Add(row["Location1"]);
            sortedDt.Rows.Add(row["Location2"]);
            sortedDt.Rows.Add(row["Location3"]);
        }

        // now make them distinct & sort them now that they're all in the same column
        sortedDt = sortedDt.DefaultView.ToTable(/*distinct*/ true);
        sortedDt.DefaultView.Sort = "Location";
        sortedDt = sortedDt = sortedDt.DefaultView.ToTable();

        // now your original code, but modified to populate the ddl with the new sorted data
        ddlLocation.Items.Insert(0, new ListItem("All", "-1"));
        foreach (DataRow row in sortedDt.Rows)
        {
            this.ddlLocation.Items.Add(new ListItem(row["Location"].ToString()));
        }

为什么不合并所有三个位置的字符串,然后将该字符串添加到ddl中?您希望列表项显示
Location1+Location2
?为什么在调用GetFilteredRiskInfo()时没有排序?然后,当您在数据行中循环时,它将被预排序。