gpt4 book ai didi

c# - 编译错误 CS0029 无法将类型 'ComboBox.ObjectCollection' 隐式转换为 'CheckedListBox.ObjectCollection'

转载 作者:太空宇宙 更新时间:2023-11-03 19:38:08 24 4
gpt4 key购买 nike

我在这一行的代码中有这个错误

ObjectCollection items = comboBox1.Items;

错误是:

"Error CS0029   Cannot implicitly convert type
'System.Windows.Forms.ComboBox.ObjectCollection' to
'System.Windows.Forms.CheckedListBox.ObjectCollection'"

我需要帮助解决这个问题,有人可以帮助我吗?我应该在这里做什么?

private void Button3_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
ObjectCollection items = comboBox1.Items;
foreach (var item in items)
{
MyClass myClass = (MyClass)item;
string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source ="
+ myClass.Path + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
OleDbConnection con = new OleDbConnection(constr);
DataTable dt = new DataTable();
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "Select * From[" + myClass.TableName + "]";
comm.Connection = con;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
dataTable.Merge(dt);
}
}
}

dataGridView1.DataSource = dataTable;
}

最佳答案

您可能正在使用 另一个同样定义此类型的命名空间。明确声明类型:

System.Windows.Forms.ComboBox.ObjectCollection items = comboBox1.Items;

或使用变量:

var items = comboBox1.Items;

关于c# - 编译错误 CS0029 无法将类型 'ComboBox.ObjectCollection' 隐式转换为 'CheckedListBox.ObjectCollection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com