Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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# 附加信息:复杂数据绑定接受IList或IListSource作为数据源_C#_List - Fatal编程技术网

C# 附加信息:复杂数据绑定接受IList或IListSource作为数据源

C# 附加信息:复杂数据绑定接受IList或IListSource作为数据源,c#,list,C#,List,我试图在excel表格中选择合计,并将其添加到一个列表框中,每一个都在一个新行中 private void btn_update_Click(object sender, EventArgs e) { for (int i = list_sheetnames.Items.Count -1; i >= 0; i--) { string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tex

我试图在excel表格中选择合计,并将其添加到一个列表框中,每一个都在一个新行中

private void btn_update_Click(object sender, EventArgs e) {
    for (int i = list_sheetnames.Items.Count -1; i >= 0; i--) {
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox_filename.Text + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
        string selectCmd = "Select SUM(Total) As Total_SUM From [" + list_sheetnames.Items[i] + "$]";

        using(OleDbConnection excelConn = new OleDbConnection(connString)) {
          excelConn.Open(); 
          OleDbCommand command = new OleDbCommand(selectCmd, excelConn);
          OleDbDataAdapter da = new OleDbDataAdapter(command);
          DataTable sheetInfo = new DataTable();
          da.Fill(sheetInfo);

          //Do something with the data.
          //list_total.Items.Add(sheetInfo);
          list_total.DataSource = da.ToString();
        }
    }
}

我遇到一个错误

看起来您正在尝试将数据表绑定到列表。我猜列表的总数是一个
列表
?如果要添加datatable中的值,则需要遍历行和项以获得所需的内容

foreach (var row in da.Rows)
{
    foreach (var item in row.ItemArray)
    {
        //do your checks on the data here and add it to your list if necessary.
        list_total.Add(item.ToString());
    }
}
您也可以尝试传统的for循环,如下所示:

for (int i = 0; i < da.Rows.Count; i++)
{
    list_total.Add(da.Rows[i]["Total_SUM"].ToString());
}
for(int i=0;i
更新
因此,您的代码现在是:

List<string> lst = new List<string>(); 

foreach (DataRow r in sheetInfo.Rows) 
{ 
    string sheettotal = (string)r["Total_SUM"].ToString();
    lst.Add(sheettotal); 
}

list_total.DataSource = lst;
List lst=new List();
foreach(sheetInfo.Rows中的数据行r)
{ 
string sheettotal=(string)r[“Total_SUM”].ToString();
第一次添加(总计);
}
list_total.DataSource=lst;

您遇到了哪一个错误?我重新编写了您的问题,请包括我没有遇到的错误,请您明确我的意思是我可以在哪里添加此代码以及如何配置it@AbdullazizHappy-在执行“list_total.DataSource=da.ToString();”操作时。不能将datatable作为数据源添加到列表中。相反,您需要通过查看datatable.row.ItemArray将各个条目添加到列表中。此处不可用的是已编辑的代码datatable sheetInfo=new datatable();da.填写(表格信息);字符串总数=sheetInfo.ToString();foreach(sheetInfo.Rows中的var row){foreach(row.ToString()中的var item){list_total.Items.Add(item.ToString();}}}@AbdullazizHappy-row.ToString()不起作用。您需要迭代该行中的项目。@AbdullazizHappy-使用替代解决方案更新了我的答案。