Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用组合框中相关选定值的ID列填充文本框_C#_Winforms_List_Combobox_Textbox - Fatal编程技术网

C# 用组合框中相关选定值的ID列填充文本框

C# 用组合框中相关选定值的ID列填充文本框,c#,winforms,list,combobox,textbox,C#,Winforms,List,Combobox,Textbox,嗨,我在win表单中显示数据时遇到问题。我有一个sql数据库,我将该表放在一个列表中: public List<ItemsForSale> GetStock() { var db = new Model1(); return db.Stock.ToList(); } 如何在其他文本框中显示行数据。例如,如果我选择了库存意大利面,如何在文本框中显示其价格 下面是一个示例,您正在查找combobox的s

嗨,我在win表单中显示数据时遇到问题。我有一个sql数据库,我将该表放在一个列表中:

  public List<ItemsForSale> GetStock()
        {
            var db = new Model1();
            return db.Stock.ToList();
        }

如何在其他文本框中显示行数据。例如,如果我选择了库存意大利面,如何在文本框中显示其价格

下面是一个示例,您正在查找combobox的
selectedIndex\u Changed
事件

我有一个
股票
实体

public class Stock {
       public Stock(int Id,string Name,decimal Price) {
           this.Id = Id;
           this.Name = Name;
           this.Price = Price;
       }
       public int Id { get; set; }
       public string Name { get; set; }
       public decimal Price { get; set; }
}


List<Stock> dataSource = new List<Stock>(); // set list of object to an variable not to query db again and again, just use this all the time
private void Form1_Load(object sender, EventArgs e) {
      dataSource = GetStocks(); 
      comboBox1.DisplayMember = "Name"; // set display member
      comboBox1.ValueMember = "Id"; // value member as Id to use at selectedIndex changed
      comboBox1.DataSource = dataSource;
}
public List<Stock> GetStocks() { // just creating on my own list, you won't change this
     return new List<Stock>() { new Stock(1,"test1",13) , new Stock(2, "test2", 17), new Stock(3, "test3", 113) };
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
     // query your List variable to find the stock by selected Item's Id, get first one and take Price property
      decimal value = dataSource.Where(x => x.Id == (int)comboBox1.SelectedValue).FirstOrDefault().Price; 
      textBox1.Text = value.ToString(); // set value to textbox
}
公共类股票{
公开股票(整数Id、字符串名称、十进制价格){
这个.Id=Id;
this.Name=Name;
这个。价格=价格;
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制价格{get;set;}
}
列表数据源=新列表();//将list of object设置为一个变量,不要一次又一次地查询db,只需一直使用它即可
私有void Form1\u加载(对象发送方、事件参数e){
dataSource=GetStocks();
comboBox1.DisplayMember=“Name”//设置显示成员
comboBox1.ValueMember=“Id”;//在所选索引中用作Id的值成员已更改
comboBox1.DataSource=数据源;
}
public List GetStocks(){//仅在我自己的列表上创建,您不会更改此设置
returnnewlist(){newstock(1,“test1”,13),newstock(2,“test2”,17),newstock(3,“test3”,113)};
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e){
//查询列表变量以按所选项目的Id查找库存,获取第一个和获取价格属性
decimal value=dataSource.Where(x=>x.Id==(int)comboBox1.SelectedValue).FirstOrDefault().Price;
textBox1.Text=value.ToString();//将值设置为textbox
}

希望有帮助,

下面是一个示例,您正在查找combobox的
selectedIndex\u Changed
事件

我有一个
股票
实体

public class Stock {
       public Stock(int Id,string Name,decimal Price) {
           this.Id = Id;
           this.Name = Name;
           this.Price = Price;
       }
       public int Id { get; set; }
       public string Name { get; set; }
       public decimal Price { get; set; }
}


List<Stock> dataSource = new List<Stock>(); // set list of object to an variable not to query db again and again, just use this all the time
private void Form1_Load(object sender, EventArgs e) {
      dataSource = GetStocks(); 
      comboBox1.DisplayMember = "Name"; // set display member
      comboBox1.ValueMember = "Id"; // value member as Id to use at selectedIndex changed
      comboBox1.DataSource = dataSource;
}
public List<Stock> GetStocks() { // just creating on my own list, you won't change this
     return new List<Stock>() { new Stock(1,"test1",13) , new Stock(2, "test2", 17), new Stock(3, "test3", 113) };
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
     // query your List variable to find the stock by selected Item's Id, get first one and take Price property
      decimal value = dataSource.Where(x => x.Id == (int)comboBox1.SelectedValue).FirstOrDefault().Price; 
      textBox1.Text = value.ToString(); // set value to textbox
}
公共类股票{
公开股票(整数Id、字符串名称、十进制价格){
这个.Id=Id;
this.Name=Name;
这个。价格=价格;
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制价格{get;set;}
}
列表数据源=新列表();//将list of object设置为一个变量,不要一次又一次地查询db,只需一直使用它即可
私有void Form1\u加载(对象发送方、事件参数e){
dataSource=GetStocks();
comboBox1.DisplayMember=“Name”//设置显示成员
comboBox1.ValueMember=“Id”;//在所选索引中用作Id的值成员已更改
comboBox1.DataSource=数据源;
}
public List GetStocks(){//仅在我自己的列表上创建,您不会更改此设置
returnnewlist(){newstock(1,“test1”,13),newstock(2,“test2”,17),newstock(3,“test3”,113)};
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e){
//查询列表变量以按所选项目的Id查找库存,获取第一个和获取价格属性
decimal value=dataSource.Where(x=>x.Id==(int)comboBox1.SelectedValue).FirstOrDefault().Price;
textBox1.Text=value.ToString();//将值设置为textbox
}

希望有帮助,

您知道控件的事件吗?你卡在哪里?@Sinatr我唯一使用的事件控件是在表单onload上填充组合框。是否有一个事件可以用来执行此操作?“是否有一个事件”-。您知道控件的事件吗?你卡在哪里?@Sinatr我唯一使用的事件控件是在表单onload上填充组合框。是否有一个事件可以用来执行此操作?“是否有一个事件”-。正在进行此操作时,我将如何用库存金额填充textbox2。我已尝试重复SelectedIndexChanged中的部分,但结果没有显示
Stock value=dataSource.Where(x=>x.Id==(int)comboBox1.SelectedValue).FirstOrDefault()
textBox1.Text=value.Price.ToString()
textBox2.Text=value.AnotherProperty.ToString()@phil3992正在进行这项工作,我将如何用股票金额填充textbox2。我已尝试重复SelectedIndexChanged中的部分,但结果没有显示
Stock value=dataSource.Where(x=>x.Id==(int)comboBox1.SelectedValue).FirstOrDefault()
textBox1.Text=value.Price.ToString()
textBox2.Text=value.AnotherProperty.ToString()@Phil3992