I am having issues with populating a listview in WPF.
The problem I am facing is exactly the same as
Bind an ObservableCollection to a ListView.
But the solution is not working for me. I am still getting Error: 40 : BindingExpression path error with the binded properties I have in my xaml file.
My xaml is as follows:
<ListView x:Name="qList" ItemsSource="{Binding Question}">
<ListView.View>
<GridView>
<GridViewColumn Header="h1" DisplayMemberBinding="{Binding qNo}"/>
<GridViewColumn Header="h2" DisplayMemberBinding="{Binding question}"/>
<GridViewColumn Header="h3" DisplayMemberBinding="{Binding imageName}"/>
<GridViewColumn Header="h4" DisplayMemberBinding="{Binding a1}"/>
<GridViewColumn Header="h5" DisplayMemberBinding="{Binding a2}"/>
<GridViewColumn Header="h5" DisplayMemberBinding="{Binding a3}"/>
<GridViewColumn Header="h6" DisplayMemberBinding="{Binding a4}"/>
<GridViewColumn Header="h7" DisplayMemberBinding="{Binding ca}"/>
</GridView>
</ListView.View>
</ListView>
In my .cs file I have defined the ObservableCollection and Question class as follows:
ObservableCollection<Question> _Questions = new ObservableCollection<Question>();
public ObservableCollection<Question> Questions
{ get { return _Questions; } }
public class Question
{
public int qNo { get; set; }
public string question { get; set; }
public string imageName { get; set; }
public string a1 { get; set; }
public string a2 { get; set; }
public string a3 { get; set; }
public string a4 { get; set; }
public int ca { get; set; }
}
In order to populate the listview (qList) I am using the following code:
DataSet ds = db.GetQList(qSetNo); //This is returning a correct dataset
_Questions.Add(new Question { });
if (ds.Tables.Count > 0)
qList.ItemsSource = ds.Tables[0].DefaultView;
The listview is being populating with correct data. This is noticed as relevant textboxes are populated with data from the dataset (ds) as the listview rows are clicked.
Edit:
new SqlDataAdapter(new SqlCommand("SELECT " + q_column_qNo + "," + q_column_question + "," + q_column_imageName + "," + q_column_a1 + "," + q_column_a2 + "," + q_column_a3 + "," + q_column_a4 + "," + q_column_ca + " FROM " + q_table_name + " WHERE " + q_column_qSet + "=" + qSetNo, conn)).Fill(ds);