Reputation: 763
I'm fetching data from my SQLite Database and inserting the list in ListView. But the same data is being repeated in my ListView.
Here's the code to fetch data :
public List<Objectdata> getAllData()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
List<Objectdata> dataList = new List<Objectdata>();
Objectdata oD = new Objectdata();
string sql = "select DISTINCT * from data ORDER BY id DESC";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
oD.id = reader["id"].ToString();
oD.name = reader["name"].ToString();
oD.address = reader["address"].ToString();
oD.mobile = reader["mobile"].ToString();
oD.date = reader["date"].ToString();
oD.price = reader["price"].ToString();
oD.warranty = reader["warranty"].ToString();
oD.month = reader["month"].ToString();
dataList.Add(oD);
}
m_dbConnection.Close();
return dataList;
}
And this is the code to add data to ListView:
private void button_refresh_Click(object sender, RoutedEventArgs e)
{
listView.Items.Clear();
TableController a = new TableController();
dataList = a.getAllData();
listView.ItemsSource = dataList;
}
This is the listview code in xaml:
<ListView x:Name="listView" HorizontalAlignment="Left" Height="400" Margin="10,76,0,0" VerticalAlignment="Top" Width="645" >
<ListView.View>
<GridView>
<GridViewColumn Header="Id" Width="20" DisplayMemberBinding="{Binding id}" />
<GridViewColumn Header="Name" Width="90" DisplayMemberBinding="{Binding name}" />
<GridViewColumn Header="Adrress" Width="130" DisplayMemberBinding="{Binding address}" />
<GridViewColumn Header="Mobile" Width="100" DisplayMemberBinding="{Binding mobile}" />
<GridViewColumn Header="Date" Width="80" DisplayMemberBinding="{Binding date}" />
<GridViewColumn Header="Price" Width="60" DisplayMemberBinding="{Binding price}" />
<GridViewColumn Header="Warranty" Width="80" DisplayMemberBinding="{Binding warranty}" />
<GridViewColumn Header="Month" Width="50" DisplayMemberBinding="{Binding month}" />
</GridView>
</ListView.View>
</ListView>
The Helper class :
class Objectdata
{
public String id
{
get; set;
}
public String name
{
get; set;
}
public String month
{
get; set;
}
public String mobile
{
get; set;
}
public String address
{
get; set;
}
public String date
{
get; set;
}
public String price
{
get; set;
}
public String warranty
{
get; set;
}
}
This the output:
Upvotes: 0
Views: 70
Reputation: 17382
You are never creating a new Objectdata
element, but always updating the same one in your loop. Thus all elements in the list are referencing the same object. Add
oD = new Objectdata()
In your loop after adding you object to the list. Or move your current declaration of oD
to be the first statement of the loop body. You don't seem to use it outside the loop anyway.
Upvotes: 1