How do I export Excel Table into the correct Columns of Datagridview

Hi everybody,

I have to export an Excel table in a datagridview. The problem is, after adding Column Headers to the dgv, the Excel table starts in the first column after my defined column headers.

So please tell me, how do I get the Excel table in the first colum with the header "Bauteilart"(like in the picture).

Thx.
Kind regards



public void LoadList()
        {
            try
            {                
                string connection1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
                string connection3 = ";Extended Properties=\"Excel 8.0;HDR=No;\"";
                string connectionString = connection1 + "C:\\List.xls" + connection3;

                OleDbConnection cnn = new OleDbConnection(connectionString);
                OleDbDataAdapter da;                
                DataSet ds;

                da = new OleDbDataAdapter("Select * from [List$]", cnn);
                ds = new DataSet("Bl");                
                da.Fill(ds, "Bl");
                dgv_Order.AutoGenerateColumns =false;
                dgv_Order.DataSource = ds.Tables[0];
                cnn.Close();
                da.Dispose();
                ds.Dispose();
                Initialize_dgv_Order();            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());                
            }
        }

Open in new window

dgv-ee.bmp
padi89Asked:
Who is Participating?
 
MathiyazhaganConnect With a Mentor Commented:
Hi, The problem is you have added columns in Datagridview with headertext and not not configured "DataPropertName" property. this property only maps the datatable column with gridview columns. so, it will consider HeaderText as datapropertyname and searches for them in datatable.hence the datatable does not them,it will displayed empty and normal table popualted.To avoid is, we  should Remove columns in designer.after populating datagridview,change the column name as:
 dgv_Order.Columns["F1"].HeaderText = "bauteilart";

hope this helps
0
 
padi89Author Commented:
Hi,

I already thought that it have to be a easy solution, but you've shown me the way!

Thanks a lot!
Padi
0
 
padi89Author Commented:
Hi,

I arlready thought that the solution sould be easy, but you've shown me the way.
Thanks for the solution.
0
All Courses

From novice to tech pro — start learning today.