importing RowName and ColumnName from an excelfile to a datagridview ...
-
Hi All ! I want to Import an Excel File to a DataGridView as FirstRow (Except A1 Cell that's null)will be Column HeaderCell in DataGridView and First Column in Excel file (Except Cell of A1=null) will be Row HeaderCell in DataGridView .Here is my code :
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
string query = String.Format("select * from [{0}$]", SheetName);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];int row = ds.Tables[0].Rows.Count;
int col = ds.Tables[0].Columns.Count;for (int i = 0; i < row - 1; i++)
{
dataGridView1.Rows[i].HeaderCell.Value = ds.Tables[0].Rows[i].ToString(); ;
}for (int j = 0; j < col - 1; j++)
{
dataGridView1.Columns[j].HeaderCell.Value = ds.Tables[0].Columns[j].ToString();
}But I have some Problem for Header Column And Header Row . Thanks For Any Help .
-
Hi All ! I want to Import an Excel File to a DataGridView as FirstRow (Except A1 Cell that's null)will be Column HeaderCell in DataGridView and First Column in Excel file (Except Cell of A1=null) will be Row HeaderCell in DataGridView .Here is my code :
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
string query = String.Format("select * from [{0}$]", SheetName);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];int row = ds.Tables[0].Rows.Count;
int col = ds.Tables[0].Columns.Count;for (int i = 0; i < row - 1; i++)
{
dataGridView1.Rows[i].HeaderCell.Value = ds.Tables[0].Rows[i].ToString(); ;
}for (int j = 0; j < col - 1; j++)
{
dataGridView1.Columns[j].HeaderCell.Value = ds.Tables[0].Columns[j].ToString();
}But I have some Problem for Header Column And Header Row . Thanks For Any Help .
Importing via an OLEDB connection only works on a well formed excel file/table. If you are having trouble then it is highly likely that the excel file is crap (as usual). Throw a break point after the fill and see if there is some data.
Never underestimate the power of human stupidity RAH
-
Importing via an OLEDB connection only works on a well formed excel file/table. If you are having trouble then it is highly likely that the excel file is crap (as usual). Throw a break point after the fill and see if there is some data.
Never underestimate the power of human stupidity RAH
Thanks for your reply , Here is my file , pl z check it . or : www.4shared.com/rar/6WxLLxtL/ReadFromExcelFile.html I've prob yet ! thanks!