在C#的清单列表框中获取多个清单项目时出现问题 [英] Problem while getting multiple checked items in the checkedlistbox in C#

查看:82
本文介绍了在C#的清单列表框中获取多个清单项目时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 checkedlistbox 控件.在检查了清单中的两项之后,当我将这些值存储在表中时,只有第二个值正在存储表,而不是第一个被检查值.

我们可以将items属性的功能从单个扩展到多个吗?

请大家为这个问题提供解决方案.

谢谢
拉吉(Rajj)

Hi all i am working with checkedlistbox control. After checking two items in the checkedlist and when i am storing these values in a table only 2nd value is storing the table not the 1st checked value.

Can we extend the functionality of items property from single to multiple?

Plz anybody can give solution for this problem.

Thanks
Rajj

if (chklbForms.CheckedItems.Count != 0)
            {
                cmd = new OleDbCommand();
                cmd.Connection = conn;
                conn.Open();
                int i1 = gdvUser.SelectedCells[0].RowIndex;
                string uname = Convert.ToString(gdvUser["UserName", i1].Value);
                cmd.CommandText = "Select UCode From UserMaster where Username='" + uname + "'";
                string ucode = Convert.ToString(cmd.ExecuteScalar());

                int i2 = gdvModule.SelectedCells[0].RowIndex;
                string mname = Convert.ToString(gdvModule["ModName", i2].Value);
                cmd.CommandText = "Select ModCode From MstModule where ModName='" + mname + "'";
                string modcode = Convert.ToString(cmd.ExecuteScalar());

列表< object> CheckedList = new List< object>();

List<object> checkedList = new List<object>();

foreach (object item in chklbForms.CheckedItems)
                {


                    checkedList.Add(chklbForms.CheckedItems.ToString());

string fname = chklbForms.SelectedValue.ToString();
                       cmd.CommandText = "Select FrmCode From MstForms Where FrmName='" + fname + "'";
                        string frmcode = Convert.ToString(cmd.ExecuteScalar());

                        string str = "";
                        str = "Insert into MstUserRights(UCode,ModCode,FrmCode) values('" + ucode + "','" + modcode + "','" + frmcode + "')";
                        cmd = new OleDbCommand(str, conn);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Successful");

}

               conn.Close();

推荐答案

CheckedListBox控件具有SelectedItems属性,可用于检索多个选定项.如果您需要通过索引访问每个项目,它也具有SelectedIndices属性.请参见 CheckedListBox类 [
The CheckedListBox control has a SelectedItems property, which you can use to retrieve multiple selected items. It also has a SelectedIndices property, if you need to access each item by it''s index. See CheckedListBox Class[^] for more details.


StringBuilder objStringBuilder = new StringBuilder();
for(int i = 0; i <= (CheckBoxList1.Items.Count - 1); i++)
 {

     if (CheckBoxList1.Items[i].Selected)
         objStringBuilder.Append("," + (CheckBoxList1.Items[i].Value));


 }
if (objStringBuilder.Length > 0)
    objStringBuilder.ToString().Substring(1);




此代码将为选中的项目返回逗号分隔的字符串.
如果您不是仅获得第一个检查项目.请检查商品价值...
希望这对您有帮助...并尝试重新发布或编辑您的问题..




this code will return comma separated string for checked items.
if you not getting first checked item only. please check the item value...
hope this will help... and try to re post or edit your question..


这篇关于在C#的清单列表框中获取多个清单项目时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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