Click here to Skip to main content
15,896,063 members

DataBinding for DataGridTextColumn

Maximys asked:

Open original thread
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
<?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:
HTML
<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:
C#
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?
Tags: DataGrid, Data binding

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900