I am experimenting with data binding in WPF
I have this datagrid related to mysql view, it will be read-only, but I would like it to automatically update when changes are made to the database.
I couldnβt find anything useful on Google, and I donβt know where to start.
This is the dataprovider class.
class ConDataProvider { private MySqlDataAdapter adapter; private Data data; private DataTable table; public ConDataProvider(string query) { data = new Data(); table = new DataTable("con_FullGrid"); adapter = data.getAdapter(query); adapter.Fill(table); } public DataView GetDView() { dv = new DataView(); dv.Table = table; return dv; } }
And XAML for the window
<Window x:Class="HelioWPF_Alpha01.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:HelioWPF_Alpha01" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="676" Width="924" MinHeight="300" MinWidth="463" Background="{x:Null}"> <Window.Resources> <ObjectDataProvider x:Key="ConDataProvider" ObjectType="{x:Type local:ConDataProvider}"> <ObjectDataProvider.ConstructorParameters> <sys:String>SELECT * FROM con_FullGrid</sys:String> </ObjectDataProvider.ConstructorParameters> </ObjectDataProvider> <ObjectDataProvider x:Key="btable" ObjectInstance="{StaticResource ConDataProvider}" MethodName="GetDView"/> </Window.Resources> <Grid Name="mainGrid" DataContext="{Binding Source={StaticResource btable}" KeyDown="mainGrid_KeyDown" FlowDirection="LeftToRight" > <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dataGrid1" FrozenColumnCount="20" RowHeight="25" Grid.ColumnSpan="4" ColumnWidth="*" MouseDoubleClick="dataGrid1_MouseDoubleClick" SelectionMode="Single" Grid.Row="2" Grid.Column="1" TabIndex="1" GotKeyboardFocus="dataGrid1_GotKeyboardFocus"> <DataGrid.AlternatingRowBackground> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#16436DF8" Offset="0" /> </LinearGradientBrush> </DataGrid.AlternatingRowBackground></DataGrid> </Grid>
What I'm doing is faking it by reinitializing the ConDataProvider class for the code and re-binding the datagrid to various events. But this is an ugly hack.
I would not mind redoing all this if necessary.
The Data class simply contains a connection string and some methods for interacting with the database. You can see it here: http://pastebin.com/m9HLfwEQ
Thanks in advance. Hope to get some good tips.
source share