Raj
Raj

Reputation: 4010

two way wpf datagrid binding to database

HI all,

I want to bind WPF datagrid in two way. I had tried following XAML:

<Grid>
    <my:DataGrid x:Name="dataGrid"  AutoGenerateColumns="False" Margin="8">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Header="Header" Binding="{Binding pCode}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pName}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pStock}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pGroup}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />              
        </my:DataGrid.Columns>
    </my:DataGrid>
</Grid>

UPDATED

ProductsTableAdapters.TempTA tempTA = new WpfDataGridBinding.ProductsTableAdapters.TempTA();
Products.TempDataTable tempDT = new Products.TempDataTable();

 public Window1()
    {
        InitializeComponent();

        tempDT = tempTA.GetData();
        dataGrid.ItemsSource = tempDT;

    }

This is how I am binding to datagrid. Now I want to update DB whenever I change price filed in DataGrid. I more thing I would like to ask that i would update only row whose value has changed, not all rows.

Thanks Please code(help) me....

Upvotes: 5

Views: 5319

Answers (1)

Nigel Shaw
Nigel Shaw

Reputation: 1016

You don't need to explicitly set two way binding. It's the default. Because you're binding to the TableAdapter, whenever you make changes in the grid those changes will be reflected in the TableAdapter. All you need to do now is write those changes to the database. In your RowChanged event of the TableAdapter just call TableAdapter.Update to write the changes to the database.

Upvotes: 4

Related Questions