Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DataGrid DataBinding
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="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?
Posted 5-Apr-12 15:01pm
Maximys832

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 5 Apr 2012
Copyright © CodeProject, 1999-2014
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