更改ComboBox项选择时重新加载DataGridview

时间:2018-11-19 04:48:04

标签: c# combobox datagrid

我创建了一个具有DataGrid的表单,该表单是从Excel文件加载的。我想在组合框中显示excel文件工作表名称。我找到了在我的组合框中显示excel表名称的解决方案。但是我想当我更改组合框项目时,datagridview取决于我更改组合框的excel工作表。 [IMG] http://i67.tinypic.com/153l82v.jpg[/IMG] 我怎样才能做到这一点?  我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace excel2access
{
    public partial class Form2 : Form
    {
        string FilePath;
        string CB;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenFD = new OpenFileDialog();
            OpenFD.FileName = "";
            OpenFD.Title = "Choose Excel file to Upload Data ";
            OpenFD.DefaultExt = "xls";
            OpenFD.Filter = "Ms-Excel Files (*.xls)|*.xls|All Files|*.*";

            if (OpenFD.ShowDialog() == DialogResult.OK)
            {
                FilePath = OpenFD.InitialDirectory + OpenFD.FileName;//Code to get FullPath, Filename and extension
                textBox1.Text = FilePath;
                string excelConnStr = string.Empty;
                OleDbCommand excelCommand = new OleDbCommand();
                if (FilePath.EndsWith(".xlsx"))
                {
                    //2007 Format
                    excelConnStr =string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};Extended Properties='Excel 8.0;HDR=No'", FilePath);
                }
                else
                {
                    //2003 Format
                   excelConnStr= string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties='Excel 8.0;HDR=No'", FilePath);
                }
                //Get the Sheets in Excel Workbook                
                OleDbConnection excelConn = new OleDbConnection(excelConnStr);
                OleDbCommand cmdExcel = new OleDbCommand();
                OleDbDataAdapter oda = new OleDbDataAdapter();
                cmdExcel.Connection = excelConn;
                excelConn.Open();
                comboBox1.DataSource = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                comboBox1.DisplayMember = "TABLE_NAME";
                comboBox1.ValueMember = "TABLE_NAME";
                CB = comboBox1.DisplayMember;
                DataTable dtsheet = new DataTable();
                dtsheet = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + comboBox1.Text + "]", excelConnStr);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

    }
}

3 个答案:

答案 0 :(得分:0)

您必须为comboBox1定义SelectionChanged事件,然后从该事件重新绑定网格。该代码将是这样的:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     string excelConnStr = string.Empty;
     OleDbCommand excelCommand = new OleDbCommand();
     if (FilePath.EndsWith(".xlsx"))
     {
         excelConnStr =string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};Extended Properties='Excel 8.0;HDR=No'", FilePath);
     }
     else
     {
         excelConnStr= string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties='Excel 8.0;HDR=No'", FilePath);
     }
     string selectedSheet= (sender as ComboBox).SelectedItem as string;
     OleDbConnection excelConn = new OleDbConnection(excelConnStr);
     OleDbCommand cmdExcel = new OleDbCommand();
     OleDbDataAdapter oda = new OleDbDataAdapter();
     cmdExcel.Connection = excelConn;
     excelConn.Open();
     DataTable dtsheet = new DataTable();
     dtsheet = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                
     OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + selectedSheet + "]", excelConnStr);
     DataTable dt = new DataTable();
     da.Fill(dt);
     dataGridView1.DataSource = dt;
}

在上面的代码(sender as ComboBox).SelectedItem as string;中,您可以从选择更改事件中的组合框中获取所选项目

答案 1 :(得分:0)

您必须为comboBox1定义SelectionIndexChanged事件,然后从该事件重新绑定网格。

我不得不建议您在第一次加载网格时,然后在一个具有多个表的数据集中存储多个工作表数据。

当用户更改下拉值然后从该数据集中获取数据时,可以防止多个数据库命中。

答案 2 :(得分:0)

尝试

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.PerformClick();
        }