المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : importing RowName and ColumnName from an excelfile to a datagridview ...



C# Programming
04-09-2012, 03:03 AM
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 .