您的位置:首页 > 其它

wpf 根据DataTable在后台自动创建DataGrid,而且可以对数据进行初步处理,显示差异

2014-01-13 19:00 627 查看
1  前台代码

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:MODEL="clr-namespace:WpfApplication2.MODEL" mc:Ignorable="d" x:Class="WinShowData" x:Name="WinShowData"
Title="查看報表" Height="700" Icon=" Images\Report.png" Width="900" WindowStartupLocation="CenterScreen" >
<Window.Resources>
<CollectionViewSource x:Key="ClstblUserAndQueryViewSource" d:DesignSource="{d:DesignInstance {x:Type MODEL:clstblUserAndQuery}, CreateList=True}"/>
<CollectionViewSource x:Key="ClstblColumnGroupViewSource" d:DesignSource="{d:DesignInstance {x:Type MODEL:clstblColumnGroup}, CreateList=True}"/>
<CollectionViewSource x:Key="ClstblShopListViewSource" d:DesignSource="{d:DesignInstance {x:Type MODEL:clstblShopList}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource ClstblUserAndQueryViewSource}" >

<Viewbox Margin="10,10,-10,10" >
<Canvas Width="912" Height="736" >
<DataGrid x:Name="myGrid" AutoGenerateColumns="False" Height="411" Canvas.Top="315" Width="902" />
<ListView x:Name="ClstblUserAndQueryListView" ItemsSource="{Binding}" SelectionMode="Single" Height="270" HorizontalAlignment="Left" Width="277" VerticalAlignment="Top" Canvas.Top="31" >
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked ="{Binding Path=Seli}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="90" Header="列編號" DisplayMemberBinding="{Binding Path=QueryColumnID}"/>
<GridViewColumn Width="140" Header="列名稱" DisplayMemberBinding="{Binding Path=QueryColumnDesci}"/>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ClstblColumnGroupListView" ItemsSource="{Binding Source={StaticResource ClstblColumnGroupViewSource}}" SelectionMode="Single" Height="268" Canvas.Left="282" Canvas.Top="31" Width="268" >
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked ="{Binding Path=Seli}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="90" Header="組編號" DisplayMemberBinding="{Binding Path=code}"/>
<GridViewColumn Width="140" Header="組名稱" DisplayMemberBinding="{Binding Path=Desci}"/>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ClstblShopListListView" ItemsSource="{Binding Source={StaticResource ClstblShopListViewSource}}" SelectionMode="Single" Height=" 270" Canvas.Left="555" Canvas.Top="31" Width="142">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked ="{Binding Path=Seli}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="90" Header="店鋪編號" DisplayMemberBinding="{Binding Path=ShopCode}"/>

</GridView>
</ListView.View>
</ListView>
<Button Name="btnQuery" Content="查詢" HorizontalAlignment="Left" VerticalAlignment="Top" Width="94" Height="25" Margin="794,79,0,0"/>
<Button Name="btnShwReport" Content="顯示圖表" HorizontalAlignment="Left" VerticalAlignment="Top" Width="94" Height="25" Margin="794,129,0,0"/>
<ComboBox Name="cmbShow" HorizontalAlignment="Left" VerticalAlignment="Top" Width="94" Height="25" RenderTransformOrigin="0.528,0.542" Canvas.Left="794" Canvas.Top="31"/>
<Label Content="顯示選項" HorizontalAlignment="Left" VerticalAlignment="Top" Width="67" Height="25" Canvas.Left="711" Canvas.Top="31"/>
<Label Content="顯示列" HorizontalAlignment="Left" VerticalAlignment="Top" Height="31" Width="184" Canvas.Top="-5"/>
<Label Content="分組條件" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.225,0.308" Height="26" Width="192" Canvas.Left="282"/>
<Label Content="選擇顯示店鋪" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.45,0.192" Width="104" Canvas.Left="555"/>
<Image Name="myimg" Height="100" Width="100"/>
</Canvas>

</Viewbox>

</Grid>

</Window>


2  后台代码
#Region "查询按钮单击"
Private Sub btnQuery_Click(sender As Object, e As RoutedEventArgs) Handles btnQuery.Click
Dim objStrColumns As String
objStrColumns = ""
Dim i As Integer
If objListUserAndQuery.Count > 0 Then
For i = 0 To objListUserAndQuery.Count - 1
If objListUserAndQuery.Item(i).Seli = 1 Then
If objStrColumns.Length > 0 Then
objStrColumns = objStrColumns + "," + objListUserAndQuery.Item(i).QueryColumnID.ToString
Else
objStrColumns = objStrColumns + objListUserAndQuery.Item(i).QueryColumnID.ToString
End If
End If
Next
End If
Dim objDA As New System.Data.SqlClient.SqlDataAdapter
Dim objDS As New System.Data.DataSet
Dim strSql = GetSql(objStrColumns)
objDA = New System.Data.SqlClient.SqlDataAdapter(DAL.clsDBHandler.getSQLCommand(strSql, DAL.clsDBHandler.getSQLConnection))
objDA.Fill(objDS)
Dim objTableA, objTableB As New DataTable
objTableB = objDS.Tables(0)
gbtableShow = objDS.Tables(0)
gbstrHeader = cmbShow.SelectedValue
'************************** 构建objtableA*************************
Dim j As Integer
Dim objListNonStrColName As New List(Of String)
Dim objListStrColName As New List(Of String)
For j = 0 To objTableB.Columns.Count - 1
Dim strColumnName As String
Dim type As System.Type
strColumnName = objTableB.Columns(j).ColumnName
type = objTableB.Columns(j).DataType
objTableA.Columns.Add(strColumnName, objTableB.Columns(j).DataType)

If type.ToString <> "System.String" Then
objListNonStrColName.Add(strColumnName)
objTableA.Columns.Add(strColumnName + "_Diff".ToString, objTableB.Columns(j).DataType)
objTableA.Columns.Add(strColumnName + "_Diff_Img", GetType(String))
End If
If type.ToString = "System.String" Then
objListStrColName.Add(strColumnName)
End If
Next

'************************** 构建objtableA*************************

'***********************在objatableA中添加相应的行*******************
Dim objArray(objTableA.Columns.Count - 1) As Object
Dim h As Integer
For h = 0 To objTableB.Rows.Count - 1
objTableA.Rows.Add(objArray)
Next

'***********************在objatableA中添加相应的行*******************

'********************给objTableA中相应的行赋值*******************************
For h = 0 To objTableB.Rows.Count - 1
Dim jj As Integer
Dim strType, strName As String
For jj = 0 To objTableB.Columns.Count - 1
strType = objTableB.Columns(jj).DataType.ToString
strName = objTableB.Columns(jj).ColumnName
If strType = "System.String" Then
objTableA.Rows(h)(strName) = objTableB.Rows(h)(strName)
Else
objTableA.Rows(h)(strName) = objTableB.Rows(h)(strName)
If h = 0 Then
objTableA.Rows(h)(strName + "_Diff") = 1
objTableA.Rows(h)(strName + "_Diff_Img"
bc81
) = "Images/top.jpg"
Else
objTableA.Rows(h)(strName + "_Diff") = (objTableB.Rows(h)(strName) - objTableB.Rows(h - 1)(strName)) / objTableB.Rows(h - 1)(strName)
objTableA.Rows(h)(strName + "_Diff") = Math.Round((objTableA.Rows(h)(strName + "_Diff")), 2)
If objTableA.Rows(h)(strName + "_Diff") >= 0 Then
objTableA.Rows(h)(strName + "_Diff_Img") = "Images/top.jpg"
ElseIf objTableA.Rows(h)(strName + "_Diff") < 0 Then
objTableA.Rows(h)(strName + "_Diff_Img") = "Images/down.jpg"
End If
End If
End If
Next
Next

'********************给objTableA中相应的行赋值*******************************

'**************************后台动态创建dataGrid******************************************
myGrid.Columns.Clear()
Dim ii As Integer
For ii = 0 To objTableA.Columns.Count - 1
Dim strName As String
strName = objTableA.Columns(ii).ColumnName
If Right(strName, 5) <> "_Diff" And Right(strName, 4) <> "_Img" Then
Dim objColumn As New DataGridTextColumn
objColumn.Binding = New Binding(strName)
objColumn.Header = strName
objColumn.Width = 100
myGrid.Columns.Add(objColumn)
ElseIf Right(strName, 5) = "_Diff" Then
Dim objTemColumn As New DataGridTemplateColumn
Dim objTem As New DataTemplate
Dim objFactory = New FrameworkElementFactory(GetType(StackPanel))
objFactory.Name = "myFactory"
objFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical)
Dim myTextBlock As New FrameworkElementFactory(GetType(TextBlock))
myTextBlock.SetBinding(TextBlock.TextProperty, New Binding(strName))
objFactory.AppendChild(myTextBlock)
Dim myImage As New FrameworkElementFactory(GetType(Image))
myImage.SetBinding(Image.SourceProperty, New Binding(strName + "_Img"))
objFactory.AppendChild(myImage)
objTem.VisualTree = objFactory
objTemColumn.CellTemplate = objTem
objTemColumn.Header = strName + "_Diff"
objTemColumn.Width = 60

myGrid.Columns.Add(objTemColumn)
End If
Next
myGrid.ItemsSource = objTableA.DefaultView

End Sub

#End Region
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐