简体   繁体   English

c#CheckedListbox值

[英]c# CheckedListbox values

When I want to put the values in an array that are selected from a checkedlistbox.当我想将值放入从选中列表框中选择的数组中时。 And then say:然后说:

messagebox.show(values[0]); messagebox.show(values[0]);

It's saying : System.Data.DataRowView它说: System.Data.DataRowView

This is my current code:这是我当前的代码:

        string[] itemArr = new string[clbTables.CheckedItems.Count];
        int counter = 0;
        foreach (object item in this.clbTables.CheckedItems)
        {
            string temp = Convert.ToString(item);
            itemArr[counter] = temp;
            counter++;
        }
        MetroMessageBox.Show(this, itemArr[0].ToString());

What am I doing wrong here>?我在这里做错了什么>?

EDIT ::编辑 ::

clbTables.DataSource = sqlDisplayContent.connectDataTable("SELECT ('Tafelnr: '+ CONVERT(varchar,tafelnr)+' Zitplaatsen: '+ CONVERT(varchar,zitPlaatsen)) AS dispValue,tafelnr  FROM tabel");
clbTables.DisplayMember = "dispValue";
clbTables.ValueMember = "tafelnr";



class sqlDisplayContent
        {
            public static DataTable connectDataTable(string query)
            {
                SqlCommand comm= sqlCrud.returnSqlCommand(query);
                SqlDataAdapter sda = new SqlDataAdapter(comm);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }

Thankss

The issue is that:问题是:

Convert.ToString(item);

will simply call the ToString() method of the object and store that, which in this case, is giving you the object's type.将简单地调用对象的 ToString() 方法并存储它,在这种情况下,它为您提供对象的类型。 In this case, the type is System.Data.DataRowView.在本例中,类型为 System.Data.DataRowView。 I suggest that you access the specific field in the row that you want by using:我建议您使用以下方法访问所需行中的特定字段:

((DataRowView)item).Row["FieldName"].ToString();

instead.反而。 You will want to replace "FieldName" with whatever the name of your column that you are wanting is.您将需要用您想要的列名称替换“FieldName”。 Additionally, you can use an int index instead of a string reference.此外,您可以使用 int 索引而不是字符串引用。 Of course, if you need to access multiple fields, you can do this by simple string concatenation.当然,如果需要访问多个字段,可以通过简单的字符串连接来实现。 The issue is that you need to access the specific field that you want.问题是您需要访问所需的特定字段。 Not the entire row as you are currently on.不是您当前所在的整行。

I hope this helps!我希望这有帮助!

A couple references: DataRow , DataRowView几个参考: DataRowDataRowView

I suppose that your values[] array is not right.我想你的 values[] 数组不正确。 Please, refer to this example on MSDN.请参考 MSDN 上的这个例子。

https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems(v=vs.110).aspx

string temp = ((CheckBox)item).Text;

您传入的是对象,而不是对象的文本(这似乎是您希望代码执行的操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM