zoukankan      html  css  js  c++  java
  • WPF中使用DataGridView创建报表控件

      WPF中使用DataGridView创建报表控件

      直入主题。报表数据是只作显示用的,直接用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 { getset; }  
            public string ViewOrder { getset; }  
            public string SearchFieldName { getset; }  
            public string SearchFieldTitle { getset; }         

            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 = "SELECT  * FROM [@TableName] Where @SearchFieldName like '%@SearchFieldValue%'  ORDER BY @ViewOrder";  
                queryStr = queryStr.Replace("@TableName", ViewName);  
                queryStr = queryStr.Replace("@ViewOrder", ViewOrder);  
                queryStr = queryStr.Replace("@SearchFieldName", SearchFieldName);  
                queryStr = queryStr.Replace("@SearchFieldValue", searchText.Text.Trim());  
                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

      

    本文来自Ivan83的博客,原文地址:http://www.cnblogs.com/Ivan83/archive/2011/08/04/2127860.html

  • 相关阅读:
    完全备份、差异备份以及增量备份的区别(转)
    Backup Exec Inventory 与Catalog的含义(转载)
    从客户端中检测到有潜在危险的Request.Form值的解决办法
    IQueryable与IEnumberable的区别(转)
    SQL递归查询(with cte as) 物料分解
    Http权威指南笔记(二) Http状态码大全
    Http权威指南笔记(一) URI URL URN 关系
    echarts在.Net中使用实例(二) 使用ajax动态加载数据
    echarts在.Net中使用实例(一) 简单的Demo
    sql显示12个月数据
  • 原文地址:https://www.cnblogs.com/wancy86/p/2261426.html
Copyright © 2011-2022 走看看