0

I'm still learning C# and winform and I can't seem to find a fix to my problem. The problem is whenever I select items in my sql generated checkedlistbox and press the button2 event it won't show the actual data, it just shows "System.Data.DataRowView" am I missing something here is the code..

private void button2_Click(object sender, EventArgs e)
{          

    int counter = checkedListBox1.SelectedItems.Count;
    List<string> selecteditems = new List<string>();
    for (int i = 0; i < counter; i++)
    {
        selecteditems.Add(checkedListBox1.SelectedItems[i].ToString());
    }
    MessageBox.Show(selecteditems[0]);

for the generation of checkedlistbox here is the code.

        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from tbl_members ORDER BY Position", con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        SqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("FullName", typeof(string));
        dt.Load(reader);

        DataSet ds = new DataSet();
        adapter.Fill(ds);
        checkedListBox1.DataSource = ds.Tables[0];
        checkedListBox1.DisplayMember = "FullName";
        checkedListBox1.ValueMember = "FullName";

        checkedListBox1.Enabled = true;
        checkedListBox1.Refresh();
        count = Convert.ToInt32(numericUpDown1.Value);
        con.Close();
Niklas
  • 13,005
  • 23
  • 79
  • 119
  • try http://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c – donstack Feb 18 '14 at 14:42

3 Answers3

0

Try replace your for loop in button2_Click to this:

foreach (DataRowView rowView in checkedListBox1.CheckedItems) 
{
    selecteditems.Add(rowView["FullName"].ToString());
}
etaiso
  • 2,736
  • 3
  • 26
  • 38
0

there is both

 MessageBox.Show(checkedListBox1.CheckedItems[0].ToString());

and

 MessageBox.Show(checkedListBox1.SelectedItems[0].ToString());

use them according to your need I think you were looking for first one

Jithu R Jacob
  • 368
  • 2
  • 17
0

Try using Convert.ToString(checkedListBox1.SelectedItems[i]) instead of .ToString()

.ToString() generally returns variable's class name in string if `.ToString()' not overrided by class.

TC Alper Tokcan
  • 369
  • 1
  • 13