如何在窗口窗体C#上的组合框中显示外键 [英] How Can I Show Foreign Key In Combo Box On Window Form C#

查看:79
本文介绍了如何在窗口窗体C#上的组合框中显示外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在我的c#项目中帮助我,我有两个表,客户和容器,客户主键是容器中的外键,两者都有单独的表单但没有任何类,现在我以客户形式添加/ det,但是我想在容器形式中做同样的事情,但是有客户外键的组合框,它确实显示了任何数据,但是,我尝试了很多次,但没有取得任何成功,有人可以帮我解决这个问题吗???我将非常感谢

这是我的鳕鱼。 。 。





can anybody help me in my c# project, i have two table, customer and container, customer primary key is foreign key in container,both have separate form but no any class, now i add/det,in customer form,but i want to do same in container form, but there is combo box for customer foreign key, and it did show any data, althoug, i tried many time, but did not get any success, can anybody help me to solve this ??? i will be thanks full
this is my cod . . .


private void Container_Load(object sender, EventArgs e)
        {

            SqlConnection mycon = new SqlConnection();
            mycon.ConnectionString = "Data Source=abdarkhanyousaf\\sqlexpress;Initial Catalog=REHMAN_BROTHERS;Integrated Security=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = mycon;
            cmd.CommandText = "select * from container ";
            SqlDataAdapter dta = new SqlDataAdapter();
            dta.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable tb = new DataTable();
            dta.Fill(tb);

            customer cu = new customer();
            comboBox1.DataSource = ds.Tables["customer"];
            comboBox1.DisplayMember = ("customer_id");
            comboBox1.ValueMember = ("customer_id");
            
        }

推荐答案

将你的sql语句更改为如下所示

change your sql statement to like below
"select customer_id,customer_name from customer"



然后它将从客户表中加载客户。您可以设置值并显示如下字段


then it will load the customers from customer table. you can set value and display fields as below

comboBox1.DisplayMember = "customer_name";
comboBox1.ValueMember = "customer_id";



现在您的组合框将显示客户名称,但是当用户从组合框中选择项目时,您可以将所选值作为客户ID。


Now your combobox will display customers names but when user select item from combobox you can get the selected value as customer id.


首先,不要在屏幕上显示键。显示客户名称。为此,您需要在容器和客户表之间使用 join 。您可以根据您的要求在左连接内连接之间进行选择。然后,您的查询将返回容器的所有详细信息以及客户ID和名称。在C#代码中,您可以将名称设置为客户下拉列表的显示成员,并将密钥保留为值成员以进行跟踪和更新等。



您的查询将可能看起来像这样:



First of all, do not show keys on screen. Show customer names instead. To do this, you will need to use a join between container and customer table. You can choose between left join or inner join based on your requirements. Your query when then return all the details for a container along with customer Id and name. In the C# code, you can then set the name as display member for customer drop down and keep the key as value member for tracking and updates etc.

Your query would probably look something like this:

select * from containers inner join customers on container.customerId = customers.customerId





理想情况下,您应该给列名称仅返回数据屏幕需要。



You should ideally give column names to return only the data that the screen would need.


这篇关于如何在窗口窗体C#上的组合框中显示外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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