简体   繁体   中英

Populate Combobox in winforms (C#) from a range of column header in the dataGridView?

im sort of stuck on how to populate a combobox with a range of headers in the dataGridView? as not all the column headers are what i want to be listed in it. As from the code below i am displaying the data in dataGridView from an Excel file. I would appreciate if anyone could help me on this. Thank you.

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

}

What about this : Edit : if you want to list from col3 to end for example :

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

But why fill your combobox based on the DGV when you can fill your Combobox based on some other datasource (excel for example ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM