【问题标题】:Selected Text in combo box is not returning any value组合框中的选定文本未返回任何值
【发布时间】:2010-11-25 10:48:42
【问题描述】:

我有一个函数可以从下拉列表中填充所选数据库中主键的所有表的列表:

public void PrimaryKeyTable()
{

    //An instance of the connection string is created to manage the contents of the connection string.

    var sqlConnection = new SqlConnectionStringBuilder();
    sqlConnection.DataSource = "192.168.10.3";
    sqlConnection.UserID = "gp";
    sqlConnection.Password = "gp";
    sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue);
    string connectionString = sqlConnection.ConnectionString;

    SqlConnection sConnection = new SqlConnection(connectionString);

    //To Open the connection.
    sConnection.Open();

    string selectPrimaryKeys = @"SELECT 
                                       TABLE_NAME 
                                   FROM
                                       INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
                                  WHERE 
                                       CONSTRAINT_TYPE = 'PRIMARY KEY'
                               ORDER BY 
                                       TABLE_NAME";

    //Create the command object
    SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);

    try
        {
        //Create the dataset
        DataSet dsListOfPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

        //Create the dataadapter object
        SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeys, sConnection);

        //Provides the master mapping between the sourcr table and system.data.datatable
        sDataAdapter.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");

        //Fill the dataset
        sDataAdapter.Fill(dsListOfPrimaryKeys);

        //Bind the result combobox with primary key tables
        DataViewManager dvmListOfPrimaryKeys = dsListOfPrimaryKeys.DefaultViewManager;
        cmbResults.DataSource = dsListOfPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
        cmbResults.DisplayMember = "TABLE_NAME";
        cmbResults.ValueMember = "TABLE_NAME";
        }
    catch(Exception ex)
        {
        //All the exceptions are handled and written in the EventLog.
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
        }
    finally
        {
        //If connection is not closed then close the connection
        if(sConnection.State != ConnectionState.Closed)
            {
            sConnection.Close();
            }
        }
}

现在我希望将此函数称为按钮单击,如下所示:

private void btnStartAnalysis_Click(object sender, EventArgs e)
    {
        //This is the function call for the primary key checking in DB
        PrimaryKeyTable();
    }

它工作正常,但是当我想在按钮上调用它时,当从下拉列表中选择特定文本时,如下所示:

private void btnStartAnalysis_Click(object sender, EventArgs e)
    {
        //This is the function call for the primary key checking in DB
        if(cmbOperations.SelectedText == "PrimaryKeyTables")
            {
            PrimaryKeyTable();
            } 
    }

然后它没有给出任何结果......

谁能告诉我哪里出错了?

【问题讨论】:

    标签: c# .net winforms combobox


    【解决方案1】:

    使用 SelectedItem 代替 SelectedText。然后使用 ToString() 获取字符串值。

    【讨论】:

      【解决方案2】:

      我认为你应该更换

      if(cmbOperations.SelectedText == "PrimaryKeyTables")
      

      与

      if((string)cmbOperations.SelectedValue == "PrimaryKeyTables")
      

      【讨论】:

      • +1 这是正确的。 SelectedText 属性表示在组合框的可编辑部分中选择的文本,就像它在文本框上所做的一样。如果DropDownStyle 设置为DropDownList,则不会 有任何文本可供选择。 SelectedValue 会返回你想要的字符串。
      猜你喜欢
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多