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.