|
-
April 9th, 2012, 02:29 PM
#1
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 SheetName = "SAW";
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
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 .
in this importing , I must have only Col1,Co2 as a Header Column and row1,row2 must be as a HeaderCell Row In DataGridView .
You Can find my sample in :
http://www.4shared.com/rar/6WxLLxtL/...ExcelFile.html
Thx for any help !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|