需要有关我的数据绑定代码发生的事情的解释? [英] Need explaination about something Happened with my DataBinding Code ???

查看:52
本文介绍了需要有关我的数据绑定代码发生的事情的解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i获胜形式包括ComboBox和1 TextBoxs和ListBox绑定其值从OrderDetails到CombBox SelectedItem >
110拖拉机50 3000
2 10 30拖拉机3500


一切与我的工作很好,我想,但我注意到一些weired但在有帮助为什么我发送邮件是因为我不喜欢看结果而又不知道结果如何呢?

这是我为OrderDetails中的SELECT语句设置的代码

i Have win form include  ComboBox and 1 TextBoxs and ListBox Bind its Values from OrderDetails Depened on CombBox SelectedItem Table here is look like of my Table with this Values  

OrderDetailsID        OrderID     ProductName     Quantity         ProductNetPrice

       1                         10             Tractor              50                  3000
       2                         10             Tractor              30                  3500


everything with me is work fine and as i want but i noted something weired but helpfull at the same time that why i sent because i dont like see results without understand how that comes okay

here is my Code that setup for the SELECT Statement from OrderDetails

string connstring = ConfigurationManager.AppSettings.Get("GrandMotorsConnection");
                string query = @"SELECT ProductID,ProductName FROM Products WHERE ProductName=@ProductName ";

                SqlConnection conni = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand(query, conni);




                cmd.Parameters.Add("@ProductName", SqlDbType.NVarChar, 50);
                cmd.Parameters["@ProductName"].Value = ((DataRowView)ProductNameCMD.SelectedItem)["ProductName"].ToString();

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataSet ds = new DataSet();


                da.Fill(ds, "Products");


                ProductIDTextBox.DataBindings.Add("text", ds, "Products.ProductID");

                string connstring2 = ConfigurationManager.AppSettings.Get("GrandMotorsConnection");
                string query2 = @"SELECT Quantity,ProductNetPrice FROM OrderDetails WHERE ProductName=@ProductName ";

                SqlConnection conni2 = new SqlConnection(connstring2);
                SqlCommand cmd2 = new SqlCommand(query2, conni2);


                cmd2.Parameters.Add("@ProductName", SqlDbType.NVarChar, 50);
                cmd2.Parameters["@ProductName"].Value = ((DataRowView)ProductNameCMD.SelectedItem)["ProductName"].ToString();

                SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "OrderDetails");



                ProductQunatityTextBox.DataBindings.Add("text", ds2, "OrderDetails.Quantity");
                
                
                ProductNetPriceListBox.DataSource = ds2;
                ProductNetPriceListBox.DisplayMember = "OrderDetails.ProductNetPrice";
                



此代码绑定到文本框,我的想法是,也许每种产品都有不同的价格,而LisBox的想法是显示ProductNetPrice列中的所有值,但发生这种情况,但让我们假设这种情况


我从上表中的CMD Box Tractor中选择了i,此选择带来了与Tractor相对应的数量以及ProductNetPrice列的所有值

我想知道为什么当我点击任何价格时,我希望它自动为我带来该价格的相应数量

怎么发生的以及我的代码的其中一部分如何做到这一点





this code is bind the Textboxs and my idea is that maybe each product have different prices and the idea of LisBox to show up all the Values in ProductNetPrice column and that happened but lets assume that scenario


i choiced from CMD Box   Tractor as on my above table and this choice bring the Quantity of the  correspond to Tractor and also The all the values for the ProductNetPrice column 

what i wanna know that why when i click over any price i want its bring me automatically the correspond Quantity for that price

how that happened and wich part of my code do that



推荐答案

默认情况下,每个表单都维护一个BindingContext对象,该对象为每个DataSource维护一个CurrencyManager.您为文本框和列表框设置了相同的数据源(ds2),它们使用相同的CurrencyManager,这会将它们链接到该数据源内的同一当前行.

Each form maintains by default one BindingContext object, which maintains one CurrencyManager for each DataSource.
Since you set the same DataSource (ds2) for the textbox and the listbox, they use the same CurrencyManager and this links them to the same current line inside that datasource.


这篇关于需要有关我的数据绑定代码发生的事情的解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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