C#中如何从ArrayList或GridView移除指定列?
咱们逐个拆解你的两个问题,给你实用的解决办法:
问题一:DataGridView绑定DropDownList.Items后出现多余列且无法移除的解决办法
首先得搞明白根源:你绑定的是ListItem对象的集合(ArrayList里存的都是ListItem),DataGridView默认会自动生成对应ListItem所有公共属性的列,所以那些Enabled、Selected都是ListItem自带的属性列。直接移除列无效,大概率是因为操作时机不对,或者AutoGenerateColumns还在自动生成列,导致你刚移除又重新生成了。
最稳妥的解决方式是不要直接绑定ListItem集合,先把ListItem转成只包含你需要的Text和Value的自定义集合,再绑定到DataGridView:
比如用匿名类型集合(推荐,代码更简洁):
var customList = dropDownList.Items.Cast<ListItem>() .Select(item => new { Text = item.Text, Value = item.Value }) .ToList(); dataGridView1.AutoGenerateColumns = true; // 因为只有两个属性,自动生成也只会出你要的列 dataGridView1.DataSource = customList;
如果一定要用ArrayList(毕竟你提到了这个类型),可以这样转换:
ArrayList customArrayList = new ArrayList(); foreach (ListItem item in dropDownList.Items) { customArrayList.Add(new { Text = item.Text, Value = item.Value }); } dataGridView1.DataSource = customArrayList;
要是你已经绑定了ListItem集合,想强行移除多余列,记得要在DataBindingComplete事件里操作,并且先关闭AutoGenerateColumns避免重复生成:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { dataGridView1.AutoGenerateColumns = false; // 按列名移除,先判断列是否存在避免报错 if (dataGridView1.Columns["Enabled"] != null) dataGridView1.Columns.Remove("Enabled"); if (dataGridView1.Columns["Selected"] != null) dataGridView1.Columns.Remove("Selected"); // 也可以设置列不可见,效果一样 // dataGridView1.Columns["Enabled"].Visible = false; }
别忘了把这个事件绑定到DataGridView的DataBindingComplete上哦。
问题二:用TXT文件的二维数组绑定DropDownList的实现步骤
假设你的TXT文件格式是每行一个选项,每行里的显示文本和值用分隔符(比如逗号、制表符)分开,比如:
选项1,value1 选项2,value2 选项3,value3
具体实现分两步:
1. 读取并解析TXT文件为二维数组
// 读取TXT所有行 string[] lines = File.ReadAllLines(@"C:\你的文件路径\items.txt"); // 初始化二维数组,行数是TXT的行数,列数固定为2(Text和Value) string[,] twoDArray = new string[lines.Length, 2]; for (int i = 0; i < lines.Length; i++) { // 按分隔符分割每行,这里用逗号,要和你TXT里的分隔符一致 string[] parts = lines[i].Split(','); if (parts.Length >= 2) // 确保每行有足够的内容 { twoDArray[i, 0] = parts[0].Trim(); // 显示文本 twoDArray[i, 1] = parts[1].Trim(); // 对应的值 } }
2. 把二维数组转成可绑定的集合,再绑定到DropDownList
DropDownList不能直接绑定二维数组,所以需要转成ListItem集合或者自定义类集合:
方式一:用ListItem集合
List<ListItem> itemList = new List<ListItem>(); for (int i = 0; i < twoDArray.GetLength(0); i++) { itemList.Add(new ListItem(twoDArray[i, 0], twoDArray[i, 1])); } // 绑定数据源 dropDownList.DataSource = itemList; dropDownList.DataTextField = "Text"; // 指定显示的属性 dropDownList.DataValueField = "Value"; // 指定值的属性 dropDownList.DataBind();
方式二:用自定义类(更规范,适合复杂场景)
先定义一个类:
public class DropDownOption { public string DisplayText { get; set; } public string OptionValue { get; set; } }
然后转换并绑定:
List<DropDownOption> optionList = new List<DropDownOption>(); for (int i = 0; i < twoDArray.GetLength(0); i++) { optionList.Add(new DropDownOption { DisplayText = twoDArray[i, 0], OptionValue = twoDArray[i, 1] }); } dropDownList.DataSource = optionList; dropDownList.DataTextField = nameof(DropDownOption.DisplayText); dropDownList.DataValueField = nameof(DropDownOption.OptionValue); dropDownList.DataBind();
这样就能实现从TXT二维数组绑定DropDownList了。
内容的提问来源于stack exchange,提问作者Lord Bobbymort
相关产品推荐
相关产品推荐

