无法根据组合框1的选择将数据加载到combobox2中。 [英] Not able to load data into combobox2 based on the selectio of combobox1..

查看:92
本文介绍了无法根据组合框1的选择将数据加载到combobox2中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序表单,我有三个控件..



Combobox1 --- cmbNameS

Combobox1 ---- cmbVersion

TextBox1 ---- txtKeys



我有一个数据库表其中我有软件名称,版本和键作为列和一些值。



现在我希望在表单加载时Combobox应该显示不同的值(我已经完成了那部分)并根据我选择的应该加载的版本自动进入Combobox2并根据所选版本,键应显示在文本框中...



我的代码...

< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;

命名空间 CMb
{
public partial class Form1:Form
{
SqlConnection connect = new SqlConnection( Data Source = Admin; Initial Catalog = Practice_DatabaseSQL; User Id = sa;密码= p @ ssw0rd);
DataTable dt1;
public Form1()
{
InitializeComponent();
bmbBind();
}

void bmbBind()
{
connect.Open();
SqlCommand cmd = new SqlCommand( SELECT来自dbo.Keys的Distinct SoftwareName,connect);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0
{
cmbNameS.DataSource = dt;
cmbNameS.DisplayMember = SoftwareName;
cmbNameS.ValueMember = SoftwareName;
}
}

}
}





我想要一些帮助做同样的事情....

解决方案

将一些代码放入要查询的cmbNameS的 SelectedIndexChanged 事件中对于基于 cmbNameS.SelectedItem 的适当数据,然后将结果绑定到cmbVersion,方法与cmbNameS相同。



要在文本框中显示所选版本,您需要在cmbVersion的 SelectedIndexChanged 事件中只需指定SelectedItem.ToString()的代码







这是用于填充文本框的代码

 private void cmbVersion_SelectedIndexChanged(object sender,EventArgs e)
{
string query =从dbo.Keys中选择SKeys其中SoftwareName ='+ cmbVersion.S electValue +';
SqlCommand cmd = new SqlCommand(query,connect);
SqlDataReader data = cmd.ExecuteReader();

if(data.Read())
{
txtKeys.Text = data.GetSqlValue(0).ToString();
}

}



如果你要调试它,你会发现你的查询字符串不正确,或者,当我调试它时,没有显示错误但处理中止

 string query =  从dbo.Keys中选择SKeys,其中SoftwareName =' + cmbVersion.SelectedValue +  '; 



将行更改为

 string query =从dbo.Keys中选择SKeys,其中SoftwareName = '+ cmbVersion。 SelectedItem.ToString() +'; 

你会发现它有效。 (我在原始解决方案中确实说过SelectedItem!)



我建议您对简单的调试技术进行一些研究 - 我曾经花了几秒钟才发现问题IDE中的代码。此CodeProject文章在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]应该让你去


I have a windows application form where I have three controls..

Combobox1 --- cmbNameS
Combobox1 ---- cmbVersion
TextBox1 ---- txtKeys

I have a database table Keys where i have softwarename, version and keys as columns and some values.

Now I want that when the form is loaded Combobox should show the distinct values(I have done that part) and based on what I select the Versions should be loaded automatically into Combobox2 and based on the selected version the key should be displayed in the Textbox...

My code...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CMb
{
    public partial class Form1 : Form
    {
        SqlConnection connect = new SqlConnection("Data Source=Admin;Initial Catalog=Practice_DatabaseSQL;User Id=sa;password=p@ssw0rd");
        DataTable dt1;
        public Form1()
        {
            InitializeComponent();
            bmbBind();
        }
        
        void bmbBind()
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand("SELECT Distinct SoftwareName from dbo.Keys", connect);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {
                cmbNameS.DataSource = dt;
                cmbNameS.DisplayMember = "SoftwareName";
                cmbNameS.ValueMember = "SoftwareName";
            }
        }

    }
}



I want some help to do the same....

解决方案

Put some code into the SelectedIndexChanged event of cmbNameS to query for the appropriate data based on the cmbNameS.SelectedItem, then just bind the results to cmbVersion in the same way that you have done it for cmbNameS.

To display the selected version in the text box you will need code in the SelectedIndexChanged event of cmbVersion that just assigns the SelectedItem.ToString()

[EDIT - further information in response to OP Comment - "unable to get desired result in textbox"]

This is the code you are using to populate the text box

private void cmbVersion_SelectedIndexChanged(object sender, EventArgs e)
{
    string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedValue + "'";
    SqlCommand cmd = new SqlCommand(query, connect);
    SqlDataReader data = cmd.ExecuteReader();

    if (data.Read())
    {
        txtKeys.Text = data.GetSqlValue(0).ToString();
    }

}


If you were to debug this you would find that your query string is incorrect or, as when I debug this, no error is displayed but processing aborts on the line

string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedValue + "'";


Change the line to

string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedItem.ToString() + "'";

and you should find that it works. (I did say SelectedItem in my original solution!)

I recommend that you do some research on simple debugging techniques - it took me seconds to find the problem once I had the code in the IDE. This CodeProject article Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] should get you going


这篇关于无法根据组合框1的选择将数据加载到combobox2中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆