Fill ListBox2 with SelectedItems from ListBox1 when ListBox1 filled using LINQ

Discussion in 'Hardware' started by jbarry820, 2 Feb 2008.

  1. jbarry820

    jbarry820 What's a Dremel?

    Joined:
    2 Feb 2008
    Posts:
    1
    Likes Received:
    0
    I have filled a ListBox from LINQ with the following code:
    private CateringDataContext Db = new CateringDataContext();
    private void Form1_Load(object sender, EventArgs e)
    { var allSides = from foodItem in Db.FoodItems
    where foodItem.FoodType.Equals("Side")
    select foodItem;
    foodItemBindingSource2.DataSource = allSides;
    }

    I am trying to fill a ListBox with the SelectedItems from another ListBox as follows:
    int rowSide = 0;
    Object[] valueSide = new Object[SideListBox1.SelectedItems.Count];
    foreach (Object dataRowSide in SideListBox1.SelectedItems)
    {
    valueSide[rowSide] = ((DataRowView)dataRowSide).Row.ItemArray[1];
    rowSide++;
    }
    MenuListBox.Items.AddRange(valueSide);

    I get an error titled “InvalidCastException” saying “Unable to cast object of type 'CateringManagement2.FoodItem' to type 'System.Data.DataRowView'.”

    Will someone tell me what I am doing wrong. This code works if I use DataSets instead of LINQ
     

Share This Page