Hello everyone.
I want to bind data from database to DataGrid. Names of field I want to set in project properties. For example, Field_1="[Field_1]", Field_2="[Field_2]", etc. Now I want to make DataBinding. Add anew property "Field_3":
='1.0'='utf-8'
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WpfApplication1.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="Field_3" Type="System.String" Scope="User">
<Value Profile="(Default)">[Field_3]</Value>
</Setting>
</Settings>
</SettingsFile>
And altering my XAML-code to:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:properties="clr-namespace:WpfApplication1.Properties" Height="350" Width="525" ResizeMode="NoResize" Initialized="Window_Initialized">
<Window.Resources>
<properties:Settings x:Key="ProgramSettings" />
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Name="db_dataGrid" VerticalAlignment="Top" Width="503" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="first_column" Header="Field 1" Binding="{Binding Path='[Field_1]'}"/>
<DataGridTextColumn x:Name="second_column" Header="Field 2" Binding="{Binding Path='[Field_2]'}" />
<DataGridTextColumn x:Name="third_column" Header="Field 3" Binding="{Binding Source={StaticResource ProgramSettings}, Path=Field_3}" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="165,242,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="315,270,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
Then I wrote next code:
private void button1_Click(object sender, RoutedEventArgs e)
{
String connectionString ="Data Source='(local)';Initial Catalog=DB_1;User Id=;Password=;Integrated Security=SSPI;";
using (SqlConnection mySQL = new SqlConnection(connectionString))
{
db_dataGrid.ItemsSource = FillDataGrid(mySQL, "SELECT * FROM [Entity1]");
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Binding myBinding=new Binding(Properties.Settings.Default.Field_3);
third_column.Binding = myBinding;
}
When I click on the button named Button1, my program binds first_column and second_column to database, but column "third_column" filled with the value of the
parameter "Field_3".
Ok, I click on the Button2. Then click on the Button1 and my program fill all field that must be=> my DataBinding is wrong.
What is wrong in my DataBinding? How can I use value of the parameter to bind to database?