从dataGridView中的一系列列标题填充winforms(C#)中的Combobox?

从dataGridView中的一系列列标题填充winforms(C#)中的Combobox?,c#,visual-studio-2010,excel,datagridview,C#,Visual Studio 2010,Excel,Datagridview,在dataGridView中如何用一系列标题填充一个组合框,这让我有些困惑?因为并不是所有的列标题都是我想在其中列出的。 根据下面的代码,我在dataGridView中显示Excel文件中的数据。如果有人能在这方面帮助我,我将不胜感激。多谢各位 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Sys

在dataGridView中如何用一系列标题填充一个组合框,这让我有些困惑?因为并不是所有的列标题都是我想在其中列出的。 根据下面的代码,我在dataGridView中显示Excel文件中的数据。如果有人能在这方面帮助我,我将不胜感激。多谢各位

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GBstock
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // populate the dataGridView with the Excel File //
        string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", @"FilePath");
        string query = String.Format("select * from [{0}$]", "Sheet1");
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        dataGridView1.DataSource = dataSet.Tables[0];
    }
}
}

这个怎么样: 编辑:如果要从col3到end列出,例如:

for(i = 2; i < dgv1.Columns.Count) 
{
    cb.Items.Add(dgv1.Columns(i).HeaderText);
}
for(i=2;i

但是,当您可以基于其他数据源(例如excel)填充组合框时,为什么要基于DGV填充组合框呢?

谢谢您,但是这列出了所有标题,我如何让它列出从第3列到最后的所有列标题?你的评论也让我想,如果我能从实际的excel文件本身显示它们,也许会更好,我该怎么做呢?再次感谢你……我编辑了这篇文章。如果你想从excel填写,我真的不知道,从来没有使用过excel自动化(访问excel文件的.Net方式)。我想您必须在Excel文件中查找标题。列标题可以通过其属性或位置来识别谢谢,我现在决定从excel文件中获取组合框的列,谢谢你为我指明了正确的方向。