Click here to Skip to main content
14,981,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using this code for import data and image from MS access to datagridview.
now can i use excel file insteted of access file and how?
C#
private void MainForm_Load(object sender, EventArgs e)
    {
        // Declare dataBase variables
        string cnStr, cmdText;
        cnStr =  "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=Persons.mdb";
        OleDbConnection cn = new OleDbConnection(cnStr);
        OleDbCommand cmd;
        OleDbDataReader dr;
        DataTable dt = new DataTable("Persons");
        try
        {
            cn.Open();

            // Load Data into DataGridView
            cmdText = "SELECT * FROM Persons";
            cmd = new OleDbCommand(cmdText, cn);
            dr = cmd.ExecuteReader();
            if (dr.HasRows) dt.Load(dr);
            dr.Close();

            dgv.DataSource = dt;

            // Initialize DataGridView Columns
            dgv.RowHeadersVisible = false;
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                col.ReadOnly = true;
                if (col.GetType().Name == "DataGridViewImageColumn")
                {
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        if (row.IsNewRow) continue;
                        row.Height = row.Cells["image"].ContentBounds.Height + 6;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cn.Close();
            cn = null;
        }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        // Calling DataGridView Printing
        PrintDGV.Print_DataGridView(dgv);
    }
}
Posted
Updated 25-Jul-15 16:50pm
v2
Comments
RosettaH 26-Jan-16 4:45am
   
I'm afraid this cannot be done with OLEDB, the reason is that with it you can only read the cell collection's values. But note that images in excel files are actually in a separate collection, they do not belong to any cell (even though it may seem they do because they are placed, located, over a single cell).
So I would suggest you to try a different approach, for example you can use this .NET library for excel files. It can read your workbooks (XLS, XLSX, etc.) from C# and also it can directly import that excel's content into your DataGridView (note that this approach imports cells data and pictures as well and even more like hyperlinks, etc.).

1 solution

You just need to change the code for retrieving data and rest of the code need not be changed.
C#
cnStr =  "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=Persons.xls;Extended Properties=Excel 8.0;";
            OleDbConnection cn = new OleDbConnection(cnStr);
            OleDbCommand cmd;
            DataTable dt = new DataTable("Persons");
            try
            {
                cn.Open();
 
                // Load Data into DataGridView
                cmd = System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]",
            , cn);
System.Data.DataSet ds = new System.Data.DataSet();
            cmd.Fill(ds);
            dt=ds.Tables[0];
                
            dgv.DataSource = dt

I haven't executed this code but something like this should work.

Hope, it helps :)
   
v2
Comments
Member 10273293 27-Jul-15 3:12am
   
this code is not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900