Click here to Skip to main content
13,045,019 members (41,563 online)
Rate this:
Please Sign up or sign in to vote.
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":
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="" CurrentProfile="(Default)" GeneratedClassNamespace="WpfApplication1.Properties" GeneratedClassName="Settings">
  <Profiles />
    <Setting Name="Field_3" Type="System.String" Scope="User">
      <Value Profile="(Default)">[Field_3]</Value>

And altering my XAML-code to:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns:properties="clr-namespace:WpfApplication1.Properties" Height="350" Width="525" ResizeMode="NoResize" Initialized="Window_Initialized">
        <properties:Settings x:Key="ProgramSettings" />
        <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Name="db_dataGrid" VerticalAlignment="Top" Width="503" >
                <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}" />


        <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" />

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?
Posted 5-Apr-12 14:01pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100