'Fill a ListView from Postgres Database in WPF C#

I'm using WPF C# Visual Studio 2012. I have a Listview in my xaml like:

<ListView 
    Name="listview" 
    Grid.Column="0"                
    Grid.ColumnSpan="2" 
    Grid.Row="1"
    Grid.RowSpan="15"
    Margin="5"
    BorderThickness="0"
    Background="WhiteSmoke">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn 
                    Header="column1" 
                    DisplayMemberBinding="{Binding path}" />
                <!--other path-->
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

The code behind to fill this Listview (in xaml.cs):

NpgsqlConnection connection = new NpgsqlConnection("Server = " + myModule.Server + ";Port = " + myModule.Port + ";User ID = " + myModule.UserID + ";Password = " + myModule.Password + ";Database = " + myModule.Database);
connection.Open();
NpgsqlCommand command = new NpgsqlCommand("select * from tblstudents_secure order by stud_id ASC", connection);

command.Connection = connection;
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command);


DataSet ds=new DataSet ();
adapter.Fill(ds);
listview.ItemsSource = ds.Tables[0].DefaultView;

When I run my program, I know that it fills the listview because I can see it's rows when I hover my mouse on that, but I cannot see any text on those rows!

What did I miss?



Solution 1:[1]

I found this solution:

In my XAML:

<ListView 
    Margin="10" 
    Name="lvDataBinding">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Student Number" Width="100" DisplayMemberBinding="{Binding list_studid}" />
            <GridViewColumn Header="First Name" Width="100" DisplayMemberBinding="{Binding list_studfname}" />
            <GridViewColumn Header="Middle Name" Width="100" DisplayMemberBinding="{Binding list_studmname}" />
            <GridViewColumn Header="Last Name" Width="100" DisplayMemberBinding="{Binding list_studlname}" />
        </GridView>
    </ListView.View>
</ListView>

In my xaml.cs:

public class User
{
    public string list_studid { get; set; }
    public string list_studfname { get; set; }
    public string list_studmname { get; set; }
    public string list_studlname { get; set; }
}


public void cmdSearch_Click(object sender, RoutedEventArgs e)
{
    NpgsqlConnection iConnect = new NpgsqlConnection
                                    ("Server = " + myModule.Server + ";
                                      Port = " + myModule.Port + ";
                                      User ID = " + myModule.UserID + ";
                                      Password = " + myModule.Password + ";
                                      Database = " + myModule.Database);
    iConnect.Open();
    NpgsqlCommand iQuery = new NpgsqlCommand("Select * from tblstudents_secure", iConnect);
    iQuery.Connection = iConnect;
    NpgsqlDataAdapter iAdapter = new NpgsqlDataAdapter(iQuery);

    DataSet iDataSet = new DataSet();
    iAdapter.Fill(iDataSet, "LIST");
    
    int lstCount = iDataSet.Tables["LIST"].Rows.Count;//lstCount holds the total count of the list from database

    int i = 0;//used as counter
    List<User> items = new List<User>();
    while (lstCount > i)
    {

        items.Add(new User()
        {
            list_studid = iDataSet.Tables["LIST"].Rows[i]["stud_id"].ToString(),
            list_studfname = iDataSet.Tables["LIST"].Rows[i]["stud_fname"].ToString(),
            list_studmname = iDataSet.Tables["LIST"].Rows[i]["stud_mname"].ToString(),
            list_studlname = iDataSet.Tables["LIST"].Rows[i]["stud_lname"].ToString()
        });

        lvDataBinding.ItemsSource = items;//lvDataBinding is the name of my ListView

        i++;
    }
}

Explanation:

In my design (XAML), the DisplayMemberBinding property of the GridViewColumn holds the Binding List, which from code behind is list_studid, list_studfname, list_studmname, list_studlname. Now, we have a "catcher" on our design, we set the "thrower" from our code behind.

The public class User holds and initializes the binding list – list_studid, list_studfname, etc.

Upon user CLICK on my button (cmdSearch), we call the database, open it, perform select query, fill it into the dataset, and loop until the end of the list.

Now the "thrower" starts on the loop, including the List<User> items = new List<User>(); . While looping, the list_studid, etc. holds the data from the database (list_studid = iDataSet.Tables["LIST"].Rows[i]["stud_id"].ToString()) and converts it to string (.ToString()).

Then we "throw" it to the listview in lvDataBinding.ItemsSource = items;. And as I have said, the design (XAML) "catches" this "throw", and loads it to the listview.

Solution 2:[2]

Well It's hard to help you with this little information about your entities... But I would bet the entities in your DB dont have a path property.

This example worked for me:

   <ListView 
            Name="listview" 
            Grid.Column="0"                
            Grid.ColumnSpan="2" 
            Grid.Row="1"
            Grid.RowSpan="15"
            Margin="5"
            BorderThickness="0"
            Background="WhiteSmoke">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn 
                            Header="column1" 
                            DisplayMemberBinding="{Binding Path}" />
                    <!--other path-->
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

CodeBehind:

   public partial class MainWindow : Window
   {
    public MainWindow()
   {
       InitializeComponent();
       var bla = new ObservableCollection<Car>()
       {
          new Car()
       };

       listview.ItemsSource = bla;
   }
 }

 public class Car
 {
   public Car()
  {
     Path = "bla";
  }
   public string Path { get; set; }
 }

notice the Property Path on my Car class and the line DisplayMemberBinding="{Binding Path}" in the view.

regards

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkrieger1
Solution 2 user3292642