Click here to Skip to main content
16,020,377 members
Home / Discussions / C#
   

C#

 
GeneralRe: www.dofactory.com way of doing things Pin
Brendan Vogt28-Jul-09 3:42
Brendan Vogt28-Jul-09 3:42 
QuestionSoftware Testing Pin
CrimeanTurtle200827-Jul-09 20:48
CrimeanTurtle200827-Jul-09 20:48 
AnswerRe: Software Testing Pin
Luc Pattyn27-Jul-09 22:40
sitebuilderLuc Pattyn27-Jul-09 22:40 
AnswerRe: Software Testing Pin
Nagy Vilmos27-Jul-09 22:54
professionalNagy Vilmos27-Jul-09 22:54 
Questiondata from excel file Pin
Member 59031027-Jul-09 20:36
Member 59031027-Jul-09 20:36 
AnswerRe: data from excel file Pin
Tamer Oz27-Jul-09 20:45
Tamer Oz27-Jul-09 20:45 
GeneralRe: data from excel file Pin
Member 59031027-Jul-09 20:48
Member 59031027-Jul-09 20:48 
GeneralRe: data from excel file Pin
Tamer Oz27-Jul-09 21:31
Tamer Oz27-Jul-09 21:31 
Here is the code hope helps.

public string GetExcelConnectionString(string fileName)
{
    if (fileName.EndsWith("xls"))
    {
        return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + @";Extended Properties=""Excel 8.0;IMEX=1""";
    }
    else
    {
        return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;""";
    }
}
OpenFileDialog ofd = new OpenFileDialog();
ComboBox cmbTables = new ComboBox();
ListBox lstColumns = new ListBox();
Button btnShowInGrid = new Button();
DataGridView dgv = new DataGridView();
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
    lstColumns.SelectionMode = SelectionMode.MultiSimple;
    dgv.AutoGenerateColumns = false;
    btnShowInGrid.Text = "Show In Grid";
    btnShowInGrid.Click += new EventHandler(btnShowInGrid_Click);
    this.Controls.Add(dgv);
    this.Controls.Add(btnShowInGrid);
    this.Controls.Add(lstColumns);
    this.Controls.Add(cmbTables);



    lstColumns.Dock = DockStyle.Top;
    cmbTables.Dock = DockStyle.Top;
    btnShowInGrid.Dock = DockStyle.Top;
    dgv.Dock = DockStyle.Fill;

    cmbTables.SelectedIndexChanged += new EventHandler(cmbTables_SelectedIndexChanged);

    ofd.ShowDialog();

    OleDbConnection conn = new OleDbConnection(GetExcelConnectionString(ofd.FileName));
    conn.Open();
    DataTable tables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    conn.Close();
    cmbTables.DataSource = tables;
    cmbTables.DisplayMember = "TABLE_NAME";
    cmbTables.ValueMember = "TABLE_NAME";
}

void btnShowInGrid_Click(object sender, EventArgs e)
{
    dgv.Columns.Clear();
    foreach (object o in lstColumns.SelectedItems)
    {
        DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();

        c.DataPropertyName = o.ToString();
        dgv.Columns.Add(c);
    }
    dgv.DataSource = dt;
}

void cmbTables_SelectedIndexChanged(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection(GetExcelConnectionString(ofd.FileName));

    conn.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + ((DataRowView)cmbTables.SelectedItem)["TABLE_NAME"].ToString() + "]", conn);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);

    da.Fill(dt);
    conn.Close();
    lstColumns.Items.Clear();
    foreach (DataColumn clmn in dt.Columns)
    {
        lstColumns.Items.Add(clmn.ColumnName);
    }


}

GeneralRe: data from excel file Pin
Member 59031027-Jul-09 23:25
Member 59031027-Jul-09 23:25 
Questionc# Serialisation Exception Pin
GauravKP27-Jul-09 20:32
professionalGauravKP27-Jul-09 20:32 
QuestionStore multiple items from listbox to database Pin
elci27-Jul-09 20:26
elci27-Jul-09 20:26 
AnswerRe: Store multiple items from listbox to database Pin
Blikkies27-Jul-09 22:47
professionalBlikkies27-Jul-09 22:47 
QuestionAccesing remote files Pin
Kushi Bobby27-Jul-09 20:16
Kushi Bobby27-Jul-09 20:16 
AnswerRe: Accesing remote files Pin
Tamer Oz27-Jul-09 20:49
Tamer Oz27-Jul-09 20:49 
GeneralRe: Accesing remote files Pin
Kushi Bobby27-Jul-09 21:16
Kushi Bobby27-Jul-09 21:16 
GeneralRe: Accesing remote files Pin
Tamer Oz27-Jul-09 21:34
Tamer Oz27-Jul-09 21:34 
GeneralRe: Accesing remote files Pin
Kushi Bobby27-Jul-09 21:44
Kushi Bobby27-Jul-09 21:44 
GeneralRe: Accesing remote files Pin
Tamer Oz28-Jul-09 1:01
Tamer Oz28-Jul-09 1:01 
GeneralRe: Accesing remote files Pin
Kushi Bobby28-Jul-09 15:25
Kushi Bobby28-Jul-09 15:25 
QuestionError.. Pin
mjawadkhatri27-Jul-09 20:15
mjawadkhatri27-Jul-09 20:15 
AnswerRe: Error.. Pin
Christian Graus27-Jul-09 20:26
protectorChristian Graus27-Jul-09 20:26 
GeneralRe: Error.. Pin
mjawadkhatri27-Jul-09 20:32
mjawadkhatri27-Jul-09 20:32 
GeneralRe: Error.. Pin
Mbah Dhaim27-Jul-09 20:52
Mbah Dhaim27-Jul-09 20:52 
GeneralRe: Error.. Pin
mjawadkhatri27-Jul-09 21:00
mjawadkhatri27-Jul-09 21:00 
GeneralRe: Error.. Pin
Mbah Dhaim27-Jul-09 21:09
Mbah Dhaim27-Jul-09 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.