Search code examples
c#winformsdatagridviewopenfiledialog

C# Update column in Excel from file loaded in DataGridView via OpenFileDialog with a value selected from a ComboBox


I have a small form which has 2 Buttons (Browse & updateExcel), a ComboBox (comboBox1) and a DataGridView (dataGridView1)

The first button lets you select an Excel file and then loads in the file to the DataGridView:

private void Browse_Click(object sender, EventArgs e)
    {

        OpenFileDialog op = new OpenFileDialog();
        op.InitialDirectory = @"C:\";
        op.Title = "Browse Excel Files";
        op.CheckFileExists = true;
        op.CheckPathExists = true;
        op.DefaultExt = "xls";
        op.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
        op.FilterIndex = 2;
        op.RestoreDirectory = true;
        op.ReadOnlyChecked = true;
        op.ShowReadOnly = true;

        if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (File.Exists(op.FileName))
            {
                string[] Arr = null;
                Arr = op.FileName.Split('.');
                if (Arr.Length > 0)
                {
                    if (Arr[Arr.Length - 1] == "xls")
                        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                        op.FileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
                }
                else if (Arr[Arr.Length - 1] == "xlsx")
                {
                    sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + op.FileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
                }
            }
            FillData();
        }
    }

This also uses the following code:

    public string sConnectionString;
    private void FillData()
    {
        if (sConnectionString.Length > 0)
        {
            OleDbConnection cn = new OleDbConnection(sConnectionString);
            {
                cn.Open();
                DataTable dt = new DataTable();
                OleDbDataAdapter Adpt = new OleDbDataAdapter("select * from [sheet1$]", cn);
                Adpt.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            try { }
            catch (Exception ex)
            {
            }
        }
    }

Once the file is displayed I have a ComboBox (comboBox1) which has set static values that can be selected.

What I am trying to to do is on another button (updateExcel) this takes the value you have selected in the ComboBox and then replaces all values in column C.

Current Usings:

using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System;
using System.ComponentModel;
using System.Data;

For example:

If my Excel file is:

a   b   c   d   e   f
aaa bbb ccc ddd eee fff
ggg hhh iii jjj kkk lll
mmm nnn ooo ppp qqq rrr

And I choose XXX from the ComboBox I would want the output to be:

a   b   c   d   e   f
aaa bbb XXX ddd eee fff
ggg hhh XXX jjj kkk lll
mmm nnn XXX ppp qqq rrr

Solution

  • I solved this by using the following code:

        private void updateExcel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                dataGridView1[2, i].Value = ConsigneeCombo.Text;
            }
        }