WPF报表控件

     直入主题。报表数据是只作显示用的,直接用DataTable比生成ORM集合快,并且高灵活性。以后要修改报表,只要简单修改一下数据库中的视图。

     但是WPF中的DataGrid对于数据稍多时,就会很慢。600行的数据显示要半分钟。通过断点查看,发现其实是DataTable绑定到DataGrid时耗时,数据返回到DataTable是很快的。看来要用以前的DataGridView了。用System.Windows.Forms.DataGridView修改后性能大大提高。600行的数据五,六秒。

   报表控件的XAML

<UserControl x:Class="WpfApplication1.ReportControl"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600" 
             xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <Grid  Background="DarkGray">
        <Grid.RowDefinitions>
            <RowDefinition Height="34"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock VerticalAlignment="Center" Text="{Binding SearchFieldTitle}"  Margin="5"></TextBlock>
            <TextBox Name="searchText" Width="200"  Margin="5"></TextBox>
            <Button Content="查找" Width="50" Margin="5" IsDefault="True" Click="Button_Click"></Button>
        </StackPanel>
        <WindowsFormsHost Grid.Row="1" Name="windowsFormsHost1" Background="LightGray" >
            <winform:DataGridView Name="listDataGrid" ReadOnly="True" AllowDrop="True" 
                                  AllowUserToAddRows="False" AllowUserToDeleteRows="False"  
                                  Font="15" RowHeadersWidth="20" SelectionMode="FullRowSelect"
                                  ShowEditingIcon="False" ShowCellToolTips="False" ShowCellErrors="False" 
                                  ShowRowErrors="False" AutoSizeColumnsMode="AllCells" >
            </winform:DataGridView>
        </WindowsFormsHost>
    </Grid>
</UserControl>

报表控件的C#(ReportControl.xaml.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Forms;

namespace WpfApplication1
{
    /// <summary>
    /// BaseReport.xaml 的交互逻辑
    /// </summary>
    public partial class ReportControl : System.Windows.Controls.UserControl
    {
        private System.Windows.Forms.DataGridView listDataGrid;

        public string ViewName { get; set; }
        public string ViewOrder { get; set; }
        public string SearchFieldName { get; set; }
        public string SearchFieldTitle { get; set; }
    
        public ReportControl()
        {
            InitializeComponent();
            listDataGrid = windowsFormsHost1.Child as System.Windows.Forms.DataGridView;

            this.SearchFieldTitle = "";
            this.DataContext = this;
        }



        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string queryStr =string.Format("SELECT  * FROM [{0}] Where {1} like '%{2}%'  ORDER BY {3}",ViewName,SearchFieldName, searchText.Text.Trim(),ViewOrder);

            listDataGrid.DataSource = DB.GetDataTable(queryStr);
        }


    }
}
DB辅助类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WpfApplication1
{
    public class DB
    {
        private static  string connectionStr = "";

        public static void InitConnectionStr(string conStr)
        {
            connectionStr = conStr;
        }

        public static SqlConnection  GetNewConnection()
        {
            return new SqlConnection(connectionStr);
        }

        public static DataTable GetDataTable(string selectCmdStr)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter adapter=null;
            SqlConnection conn=null;
            try
            {
                conn=GetNewConnection();
                conn.Open();
                adapter = new SqlDataAdapter(selectCmdStr, conn);
                adapter.Fill(dt);
            }
            catch
            {

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
                if (adapter != null)
                {
                    adapter.Dispose();
                }
            }


            return dt;
        }
    }
}

调用

1。初始化连接字符串。

            DB.InitConnectionStr(@"Data Source=PC2011012718UQF\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=dbReader;Password=888888");
2。在要用的XAML中使用ReportControl

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
    <Grid>
        <my:ReportControl x:Name="reportControl1" 
                          ViewName="Alphabetical list of products" ViewOrder="ProductID"
                          SearchFieldName="ProductName" SearchFieldTitle="Product Name:" />
    </Grid>
</Window>

------------------------------------------------------------
如非注明都是原创,如需转载请注出处。
原文地址:https://www.cnblogs.com/Ivan83/p/2127860.html