DataGridView.Rows.Count为0 [英] DataGridView.Rows.Count is 0
本文介绍了DataGridView.Rows.Count为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读取一个excel文件的内容(我必须处理数据不显示),但是行数是0.我在UI中显示了数据,行没有数字。
I'm trying to read the content of an excel file (I must process the data not show it) but the number of rows is 0. I did show the data in the UI and the rows didn't have a number.
string src = "the path;";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + src +
"Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [RENNES4_wk13$]", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataGridView dgv = new DataGridView();
dgv.DataSource = ds.Tables[0];
int rows = dgv.Rows.Count;
int cells = dgv.Rows[0].Cells.Count;//ArgumentOutOfRange why?
如何解决?
推荐答案
尝试这个
string src = @"C:\SampleData\us-500.xls";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + src +
";Extended Properties=Excel 8.0;");
//con.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [MySheet$]", con);
DataSet ds = new DataSet();
da.Fill(ds);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = ds.Tables[0];
var dataGridView1 = new DataGridView();
// Add data grid view as child control on form, flow Layout Panel is placed on windows form
flowLayoutPanel1.Controls.Add(dataGridView1);
dataGridView1.DataSource = bindingSource;
int rows = dataGridView1.Rows.Count;
int cells = dataGridView1.Rows[0].Cells.Count;
这篇关于DataGridView.Rows.Count为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文