简体   繁体   English

如何从ArrayList或GridView中删除列C#

[英]How to remove columns from ArrayList or GridView c#

Is there any way to remove "columns" from an ArrayList? 有什么方法可以从ArrayList中删除“列”吗?

I got this site up and running before attempting populating my DropDownLists from txt files so I hard-typed each value in. Now I've made an ArrayList from each DropDownList so I can display those lists in DataGridView on the site. 在尝试从txt文件填充我的DropDownLists之前,我已经启动了此站点并开始运行,因此我在其中键入了每个值。现在,我已经从每个DropDownList中创建了一个ArrayList,以便可以在站点的DataGridView中显示这些列表。 The only issue is that "Enabled" and "Selected" show up as columns and I cannot seem to remove the column in the ArrayList, or specify which columns to bring in when creating the ArrayList, or GridView using GridView.Columns.Remove(); 唯一的问题是“启用”和“选定”显示为列,我似乎无法删除ArrayList中的列,也无法指定创建ArrayList或使用GridView.Columns.Remove()的GridView时要引入的列; because the integers 0 or 1 or 2 don't seem to correspond with anything and the site doesn't run and I can't specify a string as the column title for what to remove. 因为整数0或1或2似乎与任何东西都不对应,并且站点无法运行,并且我无法指定字符串作为要删除内容的列标题。

The DataGrids show up with columns as | DataGrid的列显示为| Enabled | 已启用 | Selected | 已选 | Text | 文字 | Value | 价值 |

Here's the code for this piece as it stands (You can see what I've tried out and that didn't work that I've commented away): 这是这段代码的样子(您可以看到我尝试过的内容,但我已经注释掉了,但没有用):

        // Create ListArrays from DropDownLists
        ArrayList BuildingList = new ArrayList(Building.Items);
        ArrayList DepartmentList = new ArrayList(Department.Items);
        //Building.Items.Remove("Enabled");
        //Building.Items.Remove("Selected");

        // Populate Building GridView
        BuildingGrid.DataSource = BuildingList;
        BuildingGrid.DataBind();
        //BuildingGrid.Columns.Remove;
        //BuildingGrid.Columns[0].Visible = false;

        // Populate Department GridView
        DepartmentGrid.DataSource = DepartmentList;
        DepartmentGrid.DataBind();
        //DepartmentGrid.Columns[0].Visible = false;
        //DepartmentGrid.Columns[1].Visible = false;

I would just go ahead and create a simple 2d array in a txt file with fields for "Value" and "Text" so the DropDownList will pull it in properly, but I can't figure that out either without being terribly inefficient and confusing. 我只是继续在txt文件中创建一个简单的2d数组,并在其中包含“值”和“文本”字段,因此DropDownList可以正确地将其拉入,但我无法弄清楚这一点,除非效率极低且令人困惑。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

So, here's the solution I ended up at. 所以,这就是我最终得到的解决方案。 I finally figured out how to extract everything from a txt file, and place it into the grid the way I wanted to. 我终于想出了如何从txt文件中提取所有内容,并按照我想要的方式将其放置到网格中的方法。

        // Populate Department GridView
        // get all lines of csv file
        string[] BuildingString = File.ReadAllLines(Server.MapPath("Content/BuildingsCSV.csv"));
        string[] DepartmentString = File.ReadAllLines(Server.MapPath("Content/DepartmentsCSV.csv"));
        // create new datatable
        DataTable BuildingTable = new DataTable();
        DataTable DepartmentTable = new DataTable();

        // Building Table
        // get the column header means first line
        string[] tempbuild = BuildingString[0].Split(',');
        // creates columns of gridview as per the header name
        foreach (string t in tempbuild)
        {
            BuildingTable.Columns.Add(t, typeof(string));
        }
        // now retrive the record from second line and add it to datatable
        for (int i = 1; i < BuildingString.Length; i++)
        {
            string[] t = BuildingString[i].Split(',');
            BuildingTable.Rows.Add(t);
        }
        // Department Table
        // get the column header means first line
        string[] tempdept = DepartmentString[0].Split(',');
        // creates columns of gridview as per the header name
        foreach (string t in tempdept)
        {
            DepartmentTable.Columns.Add(t, typeof(string));
        }
        // now retrive the record from second line and add it to datatable
        for (int i = 1; i < DepartmentString.Length; i++)
        {
            string[] t = DepartmentString[i].Split(',');
            DepartmentTable.Rows.Add(t);
        }
        // assign gridview datasource property by datatable
        BuildingGrid.DataSource = BuildingTable;
        BuildingGrid.DataBind();
        BuildingGrid.Rows[0].Visible = false;
        DepartmentGrid.DataSource = DepartmentTable;
        DepartmentGrid.DataBind();
        DepartmentGrid.Rows[0].Visible = false;

        foreach (DataRow drb in BuildingTable.Rows)
        {
            BuildingDrop.Items.Add(new ListItem(drb[0].ToString(), drb[1].ToString()));
        }
        foreach (DataRow drd in DepartmentTable.Rows)
        {
            DepartmentDrop.Items.Add(new ListItem(drd[0].ToString(), drd[1].ToString()));
        }

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

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