【问题标题】:C# Autocomplete textbox using excel as a database?使用 excel 作为数据库的 C# 自动完成文本框?
【发布时间】:2019-07-13 09:48:48
【问题描述】:

所以我正在创建一个具有自动完成文本框的 Windows 窗体应用程序。我希望文本框从 Excel 电子表格中提取数据。我该怎么做呢?我注意到您不能将项目从多任务传输到文本框中,但是,这是我知道如何从 excel 中提取数据的唯一方法。有什么建议?

【问题讨论】:

    标签: c# excel visual-studio autocomplete textbox


    【解决方案1】:

    我不确定您将如何使用 Excel 作为后端来执行此操作,或者您为什么甚至想要这样做,但这是您使用普通数据库的方式。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Collections;
    
    namespace WinAutoComplete
    {
        public partial class Form1 : Form
        {
            AutoCompleteStringCollection ProductList = new
            AutoCompleteStringCollection();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //declare connection string
                string cnString = @"Data Source=Your_Server_Name; Initial Catalog=Your_DB_Name;" +
                        "Trusted_Connection = True";
    
                /*use following if you use standard security
                string cnString = @"Data Source=(local);Initial
                Catalog=northwind; Integrated Security=SSPI"; */
                //declare Connection, command and other related objects
    
                SqlConnection conGetData = new SqlConnection(cnString);
                SqlCommand cmdGetData = new SqlCommand();
                SqlDataReader drGetData;
    
                try
                {
                    //open connection
                    conGetData.Open();
                    //prepare connection object to get the data through
                    //reader and populate into dataset
                    cmdGetData.CommandType = CommandType.Text;
                    cmdGetData.Connection = conGetData;
                    cmdGetData.CommandText = "Select ProductName From Products";
                    //read data from command object
    
                    drGetData = cmdGetData.ExecuteReader();
                    if (drGetData.HasRows == true)
                    {
                        while (drGetData.Read())
                            ProductList.Add(drGetData["ProductName"].ToString());
                    }
                    else
    
                        MessageBox.Show("No data found in Products tables");
    
                    //close reader and connection
                    drGetData.Close();
                    conGetData.Close();
                    //set the default pattern to SuggestAppend
                    //comboBoxPattern.SelectedIndex = 1;
                    txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    txtProductID.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtProductID.AutoCompleteCustomSource = ProductList;
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    //check if connection is still open then attempt to close it
                    if (conGetData.State == ConnectionState.Open)
                    {
                        conGetData.Close();
                    }
                }
            }
    
            private void comboBoxPattern_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                //switch (comboBoxPattern.Text)
                //{
                //    case "Suggest":
                //        txtProductID.AutoCompleteMode = AutoCompleteMode.Suggest;
                //        break;
                //    case "Append":
                //        txtProductID.AutoCompleteMode = AutoCompleteMode.Append;
                //        break;
                //    case "SuggestAppend":
                //        txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //        break;
                //}
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2016-05-16
      相关资源
      最近更新 更多