简体   繁体   中英

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]);

It's saying : 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. In this case, the type is 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. Additionally, you can use an int index instead of a string reference. 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

I suppose that your values[] array is not right. Please, refer to this example on MSDN.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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