Click here to Skip to main content
13,047,567 members (59,750 online)
Rate this:
Please Sign up or sign in to vote.
See more:

How to load data from database into listview in C#?
Posted 14-Feb-13 23:30pm
Updated 28-Jun-17 20:47pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi.if your Data is provided By LINQToSQL classes and can do following.i assumed that your mainWindow.xaml is :
        <ListView x:Name="myListView" Background="Gray" FlowDirection="RightToLeft" Loaded="myListView_Loaded">
                <GridView AllowsColumnReorder="True" >
                        <GridViewColumn Header="ProductName" Width="150" DisplayMemberBinding="{Binding ProductName}"/>
                        <GridViewColumn Header="Unit Price" Width="100" DisplayMemberBinding="{Binding UnitPrice}"/>

and you must add a LINQTOSqlClasses object to your project and place tables of NorthWind Database on it by Drag and Drop.
your mainWindow.cs is :
private void myListView_Loaded(object sender, RoutedEventArgs e)
//MainDataContext is the name of LINQTOSQL object that you add to your project
MainDataContext context=new MainDataContext();

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Fill dataset with your data and pass it to below function-
private void LoadList(DataSet _DataSet)
    // Get the table from the data set
    DataTable dtable = _DataSet.Tables["table_name"];
    // Clear the ListView control
    // Display items in the ListView control
    for (int i = 0; i < dtable.Rows.Count; i++)
        DataRow drow = dtable.Rows[i];
        // Only row that have not been deleted
        if (drow.RowState != DataRowState.Deleted)
            // Define the list items
            ListViewItem lvi = new ListViewItem(drow["column1"].ToString());
            lvi.SubItems.Add (drow["column2"].ToString());
            lvi.SubItems.Add (drow["column3"].ToString());
            lvi.SubItems.Add (drow["column4"].ToString());
            // Add the list items to the ListView

Good luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 29 Jun 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100