有没有办法从ArrayList中删除“列”?
我在尝试从txt文件填充DropDownLists之前启动并运行了这个站点,因此我硬编了每个值。现在我从每个DropDownList创建了一个ArrayList,这样我就可以在站点上的DataGridView中显示这些列表。唯一的问题是“Enabled”和“Selected”显示为列,我似乎无法删除ArrayList中的列,或者指定在创建ArrayList时引入哪些列,或使用GridView.Columns.Remove()指定GridView ;因为整数0或1或2似乎与任何东西都不对应,并且网站没有运行,我也无法指定字符串作为要删除的内容的列标题。
DataGrids显示列为| 已启用 | 已选择 | 文字 | 值 |
以下是这篇文章的代码(您可以看到我尝试过的内容,而且我已经评论过的内容不起作用):
// 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;
我会继续在txt文件中创建一个简单的2d数组,其中包含“Value”和“Text”的字段,因此DropDownList会正确地将其拉出来,但我无法解决这个问题,如果没有非常低效的话而且令人困惑。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
所以,这就是我最终得到的解决方案。我终于想出了如何从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()));
}