2012-03-06 102 views
0

IM类的?因为并非所有的列标题都是我想列在其中的。 由于从下面的代码我显示在Excel文件dataGridView中的数据。如果有人能帮助我,我将不胜感激。谢谢。从dataGridView的一系列列标题中填充winforms(C#)中的Combobox?停留在如何填充ComboBox与一系列在DataGridView头

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]; 
    } 
} 

}

回答

2

这个怎么样: 编辑:如果你想从COL3列出来结束例如:

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

但为什么补基础上,DGV你的组合框时,你可以基于其他数据源填充你的Combobox(例如excel)

+0

谢谢你,但是这列出了所有的头文件,我如何得到它列出所有的列头文件m Column3到那时结束?此外,你的评论让我感到害怕,如果我能够从实际的Excel文件本身展示它们会更好,我将如何去做这件事?再次感谢你.... – Rg786 2012-03-06 19:44:31

+0

我编辑了这篇文章。如果你想从Excel中填写它,我真的不知道,从来没有使用Excel自动化(.Net方式来访问Excel文件)。我想你将不得不在Excel文件中寻找标题。列标题可以通过它们的属性或其位置来标识 – squelos 2012-03-06 20:02:41

+0

谢谢,我现在决定从excel文件中获取组合框的列,这要感谢您指引我朝着正确的方向。 – Rg786 2012-03-06 20:25:36