如何在checkedlistbox中获取显示成员的值成员c#

编程入门 行业动态 更新时间:2024-10-25 08:21:29
本文介绍了如何在checkedlistbox中获取显示成员的值成员c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

//这是我的数据绑定代码

//here is my databinding code

public void FillCheckedListBox1party() { try { if (con.State == ConnectionState.Closed) { con.Open(); } string commandString = "SELECT NAME ,AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'"; DataTable accTable = new DataTable(); SqlCommand cmd = new SqlCommand(commandString, con); SqlDataAdapter adpObj = new SqlDataAdapter(cmd); accTable.TableName = "tbl"; adpObj.Fill(accTable); con.Close(); checkedListBox1.DataSource = accTable; checkedListBox1.ValueMember = "AC_CODE"; checkedListBox1.DisplayMember ="NAME"; checkedListBox1.BindingContext = new BindingContext(); // myChkListBox.BindingContext = new BindingContext(); } catch (Exception ex) { throw ex; } finally { con.Close(); } }

感谢先进 lakhan 删除了SHOUTING,添加了代码块,将我的内容视为纯文本......选项被禁用 - OriginalGriff [/ edit]

thanks in advanced lakhan [edit]SHOUTING removed, Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]

推荐答案

if(checkedListBox1 .CheckedItems.Count>0) { DataRow row; row = ((DataRowView) this.checkedListBox1.CheckedItems[i]).Row; val= (row[this.checkedListBox1.ValueMember]).ToString(); row = null; query.AppendFormat("PITBROK.AC_CODE='{0}' ", val.ToString());

嗨sanoth, 绑定CheckedListBOx或任何使用displaymember和valuemember的其他控件非常简单,你只需要指定控件的datasource属性以及displaymember和valuemember。 关注工作代码100%为我工作我测试过:P $ / $ / * checkedlistbox bindig代码* / DataSet ds = new DataSet(); string strChechboxlist =选择Subject_ID作为代码,SubjectName作为显示来自dbo.Mst_Subject_Detail; / * filldataset()是我创建的用于返回数据集的函数。 * / ds = dc.FillDataSet(strChechboxlist); if(ds.Tables [0] .Rows.Count> 0) { checkedListBox1.DataSource = ds.Tables [0]; checkedListBox1.DisplayMember =Display; checkedListBox1.ValueMember =码; } / *用于从checkedlistbox中获取值成员或显示成员* / $ / b for(int i = 0; i< checkedListBox1.CheckedItems.Count; i ++) { / *现在使用以下代码,我们可以根据您的要求获得valemember和displaymember,您可以将其存储在表中。 * / DataRow r; r =((DataRowView)this.checkedListBox1.CheckedItems [i])。Row; string val =( r [this.checkedListBox1.ValueMember])。ToString(); string dis =(r [this.checkedListBox1.DisplayMember])。ToString(); r = null ; } 注意: - 我正在附加工作演示代码 Hi sanoth, Bind CheckedListBOx or any other control with displaymember and valuemember is quite simple you just need to specify datasource property of control as well as displaymember and valuemember. Following is working code 100 % work for me i have tested:P /* checkedlistbox bindig code */ DataSet ds = new DataSet(); string strChechboxlist = "select Subject_ID as code, SubjectName as Display from dbo.Mst_Subject_Detail"; /* filldataset() is function i have created to return dataset. */ ds = dc.FillDataSet(strChechboxlist); if (ds.Tables[0].Rows.Count > 0) { checkedListBox1.DataSource = ds.Tables[0]; checkedListBox1.DisplayMember = "Display"; checkedListBox1.ValueMember = "code"; } /* for fetching valuemember or displaymember from checkedlistbox */ for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { /*Now with the following code we can get valemember and displaymember as per your requirement you can store in table. */ DataRow r; r = ((DataRowView)this.checkedListBox1.CheckedItems[i]).Row; string val = (r[this.checkedListBox1.ValueMember]).ToString(); string dis = (r[this.checkedListBox1.DisplayMember]).ToString(); r = null; } Note :- I am attaching working demo of code

更多推荐

如何在checkedlistbox中获取显示成员的值成员c#

本文发布于:2023-11-16 16:19:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   如何在   checkedlistbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!