CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2012
    Posts
    3

    Pulling Excel Values Into A TextBox Based On ComboBox

    Hello,

    The topic title may seem a bit vague, so allow me to extend my apologies in advance. I am attempting to pull values into my C# application from a local Excel spreedsheet into both a combox, and a textbox. The issue that I am running into is that I can pull into the combox perfectly fine, but the text box field does not auto complete based on the drop down selection of the combox.

    The Excel spreedsheet is simple enough, and I am currently only utilizing two columns. For the sake of example:

    The user selects the name Da from the combox drop down, which happens to be located in cell A1. Da's network login is dlogs, and can be found in cell B1. If Da is selected, I would like for my textbox to automatically pull cell B1 (dlogs) into its value for multiple purposes.

    Any constructive criticism is greatly appreciated, and thank you in advance for taking the time to look over this post!

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.Common;
    
    namespace WindowsFormsApplication1
    {
        public partial class Messenger : Form
        {
            DataTable table;
            public Messenger()
            {
                InitializeComponent();
                presetComboBox.Items.Add("Manual");
                string path = @"C:\SharpTestFiles\test.xls"; //Change your path name
                ReadExcelFile(path);
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                using (StreamReader reader = new StreamReader(@"C:\SharpTestFiles\test2.txt"))
                {
                    string data = "";
                    data = reader.ReadLine();
                    presetComboBox.Items.Add(data);
                    data = reader.ReadLine();
                    presetComboBox.Items.Add(data);
                    data = reader.ReadLine();
                    presetComboBox.Items.Add(data);
                    reader.Close();
    
                }
            }
    
            void presetComboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (presetComboBox.SelectedItem == "Manual")
                {
                    manualMessage.Enabled = true;
                    manualText.Enabled = true;
    
                }
                else
                {
                    manualMessage.Enabled = false;
                    manualText.Enabled = false;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void manualText_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (manualText.Enabled != false && manualText.Text == String.Empty)
                {
                    MessageBox.Show("You haven't entered a message yet! Either select a preset message, or write something.");
                }
            }
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
            private void ReadExcelFile(string filePath)
            {
                string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", filePath);
                table = new DataTable();
                using (OleDbConnection conn = new OleDbConnection(connString))
                {
                    using (OleDbDataAdapter da = new OleDbDataAdapter(@"SELECT * FROM [Sheet1$]", conn))
                        da.Fill(table);
                }
                foreach (DataRow row in table.Rows)
                    agentComboBox.Items.Add(row[0].ToString());
            }
            private void agentComboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (agentComboBox.SelectedIndex > -1)
                    autoName.Text = table.Rows[agentComboBox.SelectedIndex][0].ToString();
            }
        }
    }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Pulling Excel Values Into A TextBox Based On ComboBox

    Can't see an obvious reason why. Several possibilities:

    (1) Put a breakpoint in agentComboBox_SelectedIndexChanged. Does it get called when you change the combobox dropdown? If not, the event might not be correctly bound.

    (2) Check the data stored in the DataTable in column 0. Does it contain the expected data? Or is it an empty string?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Posts
    3

    Re: Pulling Excel Values Into A TextBox Based On ComboBox

    At the da.Fill(table), I am getting a SelectedIndex of -1 for the agentComboBox, but I'm not pulling a value for the autoName.Text. As for column 0, this is what I am seeing in my call stack window: > Agent Alerter.exe!WindowsFormsApplication1.Messenger.ReadLoginsTab(string filePath) Line 60 C#

  4. #4
    Join Date
    Dec 2012
    Posts
    3

    Re: Pulling Excel Values Into A TextBox Based On ComboBox

    I managed to figure it out. Thank you for the help BioPhysEngr!

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Pulling Excel Values Into A TextBox Based On ComboBox

    Glad you got it working!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured