添加2个或更多字段到C#编码的值成员 [英] Add 2 or more field to value member of C# coding

查看:156
本文介绍了添加2个或更多字段到C#编码的值成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个主键字段的表。我加载到我的WinForm应用程序中使用C#创建的下拉列表。



在下拉列表的TextChanged事件中,我有一些TextBox,我想填写由我下载的列表中选定的某个字段的表格收到的信息。



所以我说这个表有4个字段。我可以从数据集中将所有4个字段分成值成员,还是告诉我是否不可能?



谢谢。



Datatable dt = dba.getName();
cmb_name.ValueMember =id;
cmb_name.DisplayMember =name;
cmb_name.DataSource = dt;



这是正常的格式..但我有更多的关键字段..所以我需要添加更多关键字段。

解决方案

您可以使用 DataSource 属性将您的源数据绑定到ComboBox(例如实体列表或DataTable等),然后设置 DisplayMember 属性到要显示的字段的(字符串)名称。



用户选择了项目,您可以将 SelectedItem 转回原始行数据类型(Entity,DataRow等,它仍将与您所输入的类型相同),然后您可以将4个复合键检索到原始项目。



这样您就可以完全避免 SelectedValue 问题。 / p>

修改



填充如下:

  cmb_name.DisplayMember =name; 
cmb_name.DataSource = dt;

//忽略ValueMember和所选值完全

当您要检索所选项目

  var selectedRow =(cmb_name.SelectedItem as DataRowView); 

现在,您可以检索PK的4个值,例如 selectedRow [field1],selectedRow [field2],selectedRow [field3] etc



如果然而,您的意思是要向用户 DISPLAY 4列(即与表键无关),然后参见这里如何绑定组合,使显示成员为2个字段的源数据?


I have table with 4 primary key fields. I load that in to drop down list in my WinForm application created by using C#.

On the TextChanged event of drop down list I have certain TextBox and I want to fill the information recived by the table for the certain field I selected by the drop down list.

So as I say the table having 4 fields. Can I get those all 4 fields into value member from the data set, or could you please tell me whether is that not possible?

Thank you.

Datatable dt=dba.getName(); cmb_name.ValueMember="id"; cmb_name.DisplayMember="name"; cmb_name.DataSource=dt;

this is normal format.. but i have more key fields.. so i need to add more key fields..

解决方案

You can use DataSource property to bind your source data to the ComboBox (e.g. a List of Entities, or a DataTable, etc), and then set the DisplayMember property of the ComboBox to the (string) name of the field you want to display.

After the user has selected an Item, you can then cast the SelectedItem back to the original row data type (Entity, DataRow, etc - it will still be the same type as you put in), and then you can retrieve your 4 composite keys to the original item.

This way you avoid the SelectedValue problem entirely.

Edit:

Populate as follows:

cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;

// Ignore ValueMember and Selected Value entirely

When you want to retrieve the selected item

var selectedRow = (cmb_name.SelectedItem as DataRowView );

Now you can retrieve the 4 values of your PK, e.g. selectedRow["field1"], selectedRow["field2"], selectedRow["field3"] etc

If however you mean that you want to DISPLAY 4 columns to the user (i.e. nothing to do with your Table Key), then see here How do I bind a Combo so the displaymember is concat of 2 fields of source datatable?

这篇关于添加2个或更多字段到C#编码的值成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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