开发者

Display datatable in ListView control

How to 开发者_如何学编程display a DataTable in a ListView control in WPF?


listView.ItemsSource = dataTable.DefaultView;


Display datatable in ListView control


If you want a ListView like above, and if your DataTable has 3 columns with name "Date", "PCName", "Price" then between your tags add following code:

<ListView.View>
   <GridView>
     <GridViewColumn DisplayMemberBinding="{Binding Path=Date}"
                     Header="Date"
                     Width="100"/>
     <GridViewColumn DisplayMemberBinding="{Binding Path=PCName}"
                     Header="Computer No."
                     Width="100"/>
     <GridViewColumn DisplayMemberBinding="{Binding Path=Price}"
                     Header="Amount (Tk)"
                     Width="100"/>
   </GridView>
</ListView.View>


*Convert listview to datatable in c#* Just iterate the entire listview table. Here is the code,

private DataTable ConvertList_To_Datatable(ListView lvDetails)
       {
           DataTable dtTable = new DataTable("ExportToPdf");
           if (lvDetails.Items.Count < 1)
           {
               return dtTable;
           }
           else
           {
               for (int ncount = 0; ncount <= lvDetails.Columns.Count-1; ncount++)
               {
                   DataColumn dtColumn =new DataColumn(lvDetails.Columns[ncount].Text);
                   dtTable.Columns.Add(dtColumn);
               }
           }          
           for (int nRowCount = 0; nRowCount <= lvDetails.Items.Count - 1; nRowCount++)
           {
               DataRow dtRow = dtTable.NewRow();
               for (int nItem = 0; nItem <=lvDetails.Items[nRowCount].SubItems.Count - 1; nItem++)
               {                    
                   dtRow[lvDetails.Columns[nItem].Text] = lvDetails.Items[nRowCount].SubItems[nItem].Text;

               } dtTable.Rows.Add(dtRow);

           } return dtTable;

       }

Just try this .It works If any dout .U can reach me at prinsuyakob20@gmail.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜