I have a datatable im trying to set as the source of listbox but only get System.Data.DataRow in place of my records i want to see in the list

Odpovědět I have a datatable im trying to set as the source of listbox but only get System.Data.DataRow in place of my records i want to see in the list

  • 23. března 2012 20:47
     
      Obsahuje kód

    i have a datatable that is returning 3 records from my table. That works..

    I list a listbox on my WPF application that i want to display those records.. but when the app loads, it just displays:

    "System.Data.DataRow" 3 times i know that its returning the correct number because i can add or remove and the list displays the correct number of records, but i cant seem to get the actual data i want to display in the list.. below is my datatable code i have in my db.cs file..

            public DataTable getList(int ID)
            {
                DataTable dtList = new DataTable();
                NpgsqlConnection conn = new NpgsqlConnection(_dbConn);
                NpgsqlCommand command = new NpgsqlCommand("select * FROM case, info WHERE case.id = info.id AND case.id = " + ID + ";", conn);
                try
                {
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    NpgsqlDataAdapter npgData = new NpgsqlDataAdapter(command);
                    npgData.Fill(dtList);
                }
                finally
                {
                    conn.Close();
                }
                return dtList;
            }

    Then within my window_loaded event i have this code to set the listbox.

                DataTable dtList = dac.getList(2);
                lbList.ItemsSource = dtList.AsEnumerable();

Všechny reakce

  • 23. března 2012 20:51
     
     Odpovědět

    You need a DataTemplate. Please read http://msdn.microsoft.com/en-us/library/ms742521.aspx.

    Good luck!


    noorbakhsh حميد نوربخش


    • Upravený noorbakhsh 23. března 2012 21:10
    • Označen jako odpověď Cubangt 24. března 2012 2:42
    •  
  • 24. března 2012 2:15
     
      Obsahuje kód

    i got it working but not as i need it..

    i changed the line for the ItemsSource to this and that works and displays the names, i selected two columns in my statement to join them so they i can display the first and lastname but when i do that it doesnt display.. when i debug it, the value returns is the combined names..

    what am i missing.. the column being returned in the select statement is called fullName

    lbList.ItemsSource = ((IListSource)dtList).GetList();
    lbList.DisplayMemberPath = "firstname";

    If i replace firstname with the fullname nothind shows up..
  • 24. března 2012 2:21
     
     Odpovědět
    You are missing a DataTemplate as I mentioned in my first post. I guess you can use "fullName" in DisplayMemberPath to show them.

    noorbakhsh حميد نوربخش


    • Upravený noorbakhsh 24. března 2012 2:22
    • Označen jako odpověď Cubangt 24. března 2012 2:42
    •  
  • 24. března 2012 2:33
     
     

    thats the problem, i cant use it in the displaymemberpath..

    i can use all my column names, except that one.. i can display lastname, city, state and so on but using fullname doesnt work..

  • 24. března 2012 2:42
     
     

    thanks.. i think i figured it out.. i was using the datatemplate, but had my binding wrong, atleast i think..

    i redid the datatemplate to have just a textblock and bind it to the fullname column and it works.