附加信息:Complex DataBinding接受IList或IListSource作为数据源 [英] Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource
问题描述
嗨我试图在Excel工作表中选择总数之和并将其添加到一个列表框中,每个人都在一个新行中
这里是我的代码
私人 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 + < span class =code-string> ;扩展属性='Excel 12.0 XML; HDR = YES;';;
string selectCmd = 从[ + list_sheetnames.Items [i] + $]中选择SUM(总计)为Total_SUM 跨度>;
使用(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);
// 对数据执行某些操作。
// list_total.Items.Add(sheetInfo);
list_total.DataSource = da.ToString() ;
}
}
我收到此错误
你能帮忙吗?我找到解决方案
i将我的代码改成这个
列表与LT;串GT; lst = new List< string>();
foreach (DataRow r in sheetInfo.Rows)
{
string sheettotal =( string )r [ Total_SUM]。ToString();
lst.Add(sheettotal);
}
list_total.DataSource = lst;
及其工作但只添加最后一个列表框中的总数
;
使用(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);
// 对数据执行某些操作。
// list_total.Items.Add(sheetInfo);
list_total.DataSource = da.ToString() ;
}
}
我收到此错误
你能帮忙吗?我找到解决方案
i将我的代码改成这个
列表与LT;串GT; lst = new List< string>();
foreach (DataRow r in sheetInfo.Rows)
{
string sheettotal =( string )r [ Total_SUM]。ToString();
lst.Add(sheettotal);
}
list_total.DataSource = lst;
及其工作但只添加最后一个列表框中的总数
Quote:
list_total.DataSource = da.ToString();
您正在设置数据源列表到包含System.Data.OleDb.OleDbDataAdapter
的字符串。
您需要将其设置为查询返回的数据:
list_total.DataSource = sheetInfo;
hi iam trying to select sum of total in excel sheets and add it into a list box each one of them in a new line
here is my code
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();
}
}
and i got this error
could you please help me to find the solution
i change my code into this
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;
and its working but only add the last total in the list box
"; 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(); } }
and i got this error
could you please help me to find the solution
i change my code into this
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;
and its working but only add the last total in the list box
Quote:
list_total.DataSource = da.ToString();
You're setting the data source of the list to a string containing"System.Data.OleDb.OleDbDataAdapter"
.
You need to set it to the data returned by the query instead:
list_total.DataSource = sheetInfo;
这篇关于附加信息:Complex DataBinding接受IList或IListSource作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!