listbox bound to a datatable

I have a "mover" form (two listboxes - lstCodes (list of possible values), and lstSelected (list of selected values))

When I choose something in lstCodes, I lookup the value in a datatable dtCategoryLookup, then I populate lstSelected's datasource with a new row containing the id and description of the lookup.

lstSelected is to a datatable dtSelected.

When I run the code below, the only thing added to lstSelected is the string "System.Data.DataRowView"

DataRow[] dr = dtCategoryLookup.Select("id='"+lstCodes.SelectedValue.ToString().Trim()+"'");

DataRow oRow = dtSelected.NewRow();
oRow["id"]=(
string)dr[0]["id"];
oRow["cDescription"]=(
string)dr[0]["cDescription"];
dtSelected.Rows.Add(oRow);


Any ideas




Answer this question

listbox bound to a datatable

  • MStrange

    Thanks for responding,

    yes the DisplayMember is set to "cDescription" and ValueMember is set to "id"

    in the Load of the form:

    dtSelected = new DataTable();
    dtSelected.Columns.Add("id");
    dtSelected.Columns.Add("cdescription");
    lstSelected.DisplayMember="cDescription";
    lstSelected.ValueMember="id";
    lstSelected.DataSource=dtSelected;
    dtSelected.Rows.Add(
    new object[] {"A","B"});



  • spinu

    At the end of your code you need to call DataBind() of the listbox.


  • Kieme Ile

    Have you set lstSelected's DisplayMemeber to "cDescription" and ValueMember to "id"

  • listbox bound to a datatable