3 Replies - 642 Views - Last Post: 07 December 2012 - 12:52 PM Rate Topic: -----

#1 ebrug  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 06-December 12

Pulling Excel Values Into TextBox

Posted 06 December 2012 - 09:39 AM

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.

Kindly note, this is not for a school project.

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

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();
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Pulling Excel Values Into TextBox

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Pulling Excel Values Into TextBox

Posted 07 December 2012 - 05:52 AM

have you debugged to ensure that agentComboBox.SelectedIndex is actually a value?
Is that handler bound to the combo box?
Was This Post Helpful? 1
  • +
  • -

#3 ebrug  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 06-December 12

Re: Pulling Excel Values Into TextBox

Posted 07 December 2012 - 08:47 AM

View PostBigR1983, on 07 December 2012 - 05:52 AM, said:

have you debugged to ensure that agentComboBox.SelectedIndex is actually a value?
Is that handler bound to the combo box?


Hi BigR1983,

The end value of agentComboBox is -1 after pulling the values from my excel spreedsheet. I have tried using SelectedItem, SelectedIndex, and SelectedValue in an attempt to pull from the adjacent column/cell with no luck.

As for debugging, it is not throwing any errors/warnings that would shed some light on the issue.
Was This Post Helpful? 0
  • +
  • -

#4 ebrug  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 06-December 12

Re: Pulling Excel Values Into TextBox

Posted 07 December 2012 - 12:52 PM

I managed to figure it out. Thanks for the help BigR1983!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1