如何使用Linq to SQL绑定checkboxlist中的数据 [英] How to bound data in checkboxlist using Linq to SQL

查看:58
本文介绍了如何使用Linq to SQL绑定checkboxlist中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想在windows窗体中使用数据填充 checkBoxList 的项目Linq。

并为每个项目提供 MemberValue DisplayMember ,如:



 var accessories =来自DB.Maintenance_Accessories中的m_accessories 
选择m_accessories;

DataTable dt_Accessories = new DataTable();
dt_Accessories.Columns.Add(a_ID,typeof(int));
dt_Accessories.Columns.Add(a_Name,typeof(string));
foreach(附件中的var a)
{
dt_Accessories.Rows.Add(a.Id,a.Name);
}
DataRow dr = dt_Accessories.NewRow();
dr [0] = -1;
dr2 [1] =其他;
dt_Accessories.Rows.InsertAt(dr,dt_Accessories.Rows.Count);

listBox_Accessories.DataSource = dt_Accessories;
listBox_Accessories.DisplayMember =a_Name;
listBox_Accessories.ValueMember =a_ID;





我遇到一个问题,告诉我 DisplayMember ValueMember

此属性与此课程无关

并且同时它不是错误



提前致谢,





祝你好运,

Michael Waguih

:)

解决方案

奇怪怎么回事'in,因为我确定CheckListBox不应该有.DataSource属性。



为了设置DataSource,DisplayMember和ValueMember CheckListbox将它作为ListBox投射,如下所示:



 DataTable temp =  DataTable(); 
temp.Columns.Add( ValueMember typeof byte ));
temp.Columns.Add( DisplayMember typeof string ));
temp.Rows.Add( 0 );
temp.Rows.Add( 1 );
temp.Rows.Add( 2 两个);
temp.Rows.Add( 3 );
temp.Rows.Add( 4 四个);
temp.Rows.Add( 5 );

this .checkedListBox1 as ListBox).DataSource = temp;
this .checkedListBox1 as ListBox).DisplayMember = DisplayMember;
this .checkedListBox1 as ListBox).ValueMember = ValueMember;





希望这有助于


Hi all,

I want to fill the items of the checkBoxList in windows Form with data using Linq.
and to give each item its MemberValue and DisplayMember like :

var accessories = from m_accessories in DB.Maintenance_Accessories
                  select m_accessories;

DataTable dt_Accessories = new DataTable();
dt_Accessories.Columns.Add("a_ID", typeof(int));
dt_Accessories.Columns.Add("a_Name", typeof(string));
foreach (var a in accessories)
{
    dt_Accessories.Rows.Add(a.Id, a.Name);
}
DataRow dr = dt_Accessories.NewRow();
dr[0] = -1;
dr2[1] = "Others";
dt_Accessories.Rows.InsertAt(dr, dt_Accessories.Rows.Count);

listBox_Accessories.DataSource = dt_Accessories;
listBox_Accessories.DisplayMember = "a_Name";
listBox_Accessories.ValueMember = "a_ID";



I faced a problem telling about DisplayMember and ValueMember:
This property is not relevant to this class
and in the same time it is not an error

Thanks in advance,


Best regards,
Michael Waguih
:)

解决方案

Strange how it is not error'in, since I'm sure there shouldn't be a .DataSource property for a CheckListBox.

In order for you to set the DataSource, DisplayMember and ValueMember for the CheckListbox is to cast it as a ListBox, like so:

DataTable temp = new DataTable();
temp.Columns.Add("ValueMember", typeof(byte));
temp.Columns.Add("DisplayMember", typeof(string));
temp.Rows.Add(0, "Zero");
temp.Rows.Add(1, "One");
temp.Rows.Add(2, "Two");
temp.Rows.Add(3, "Three");
temp.Rows.Add(4, "Four");
temp.Rows.Add(5, "Five");
 
(this.checkedListBox1 as ListBox).DataSource = temp;
(this.checkedListBox1 as ListBox).DisplayMember = "DisplayMember";
(this.checkedListBox1 as ListBox).ValueMember = "ValueMember";



Hope this Helps.


这篇关于如何使用Linq to SQL绑定checkboxlist中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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