Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to bind two DataGrid DataGridBoundColumn to the same source

$
0
0

In Visual Studio 2010 / .NET Framework 4.0 I would like to bind two DataGridBoundColumn in a single DataGrid to the same data source (DataSet).

The frist column should be used for editing the value, the second column should be ReadOnly and display additional information (by use of a ValueConverter) upon PropertyChanged or LostFocus on the first column.

Unfortunately in my code the second column is not refreshing unless I forcibly refresh the ItemsSource of the DataGrid.

To reproduce the problem in VisualStudio 2010 follow these steps:

  • Create a new WPF application called BindingTest
  • Change .NET Framework to 4.0
  • Create a class ViewModel using the following code:
    using System;
    using System.Data;
    namespace BindingTest
    {publicclassViewModel
    {
    DataSet _DataSet;

    public
    ViewModel()
    {
    _DataSet =newDataSet();

    var
    dataTable = new DataTable();
    dataTable.Columns.Add("ID",typeof(int));
    dataTable.Columns[0].AutoIncrement =true;
    dataTable.Columns[0].AllowDBNull =false;
    dataTable.Columns.Add("Text",typeof(string));           

    _DataSet.Tables.Add(dataTable);
       }

        publicDataView MyData
        {
        get {return _DataSet.Tables[0].DefaultView; }
        }
        }
        }
  • Creat a new window MainWindow
  • Replace the content of MainWindo.xaml with the following:
    <Window x:Class="BindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:BindingTest"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.DataContext>
    <local:ViewModel />
    </Grid.DataContext><DataGrid Grid.Row="0" Margin="2" Name="MyDataGrid" AutoGenerateColumns="False"CanUserAddRows="True" CanUserDeleteRows="True" ItemsSource="{Binding MyData}">
    <DataGrid.Columns>
    <DataGridTextColumn Header="ID"IsReadOnly="True" Binding="{Binding ID, Mode=OneWay}"/>
    <DataGridTextColumn Header="Text" Binding="{Binding Text}"/>
    <DataGridTextColumn Header="Text (R/O)" IsReadOnly="True" Binding="{Binding Text, Mode=OneWay}"/>
    </DataGrid.Columns>
    </DataGrid>
    <StackPanel Grid.Row="1" Orientation="Horizontal">
    <Button Margin="2" Name="RefreshButton" Click="RefreshButton_Click">Refresh</Button>
    </StackPanel>
    </Grid>
    </Window>
  • Replace the content of MainWindow.xaml.cs with the following:
    using System;
    using System.Windows;
    namespace BindingTest
    {
    ///<summary>
    /// Interaction logic for MainWindow.xaml
    ///</summary>
    publicpartialclass MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    privatevoid RefreshButton_Click(object sender,RoutedEventArgs e)
    {
    MyDataGrid.Items.Refresh();
    }
    }
    }
  • Compile and run the application
  • Enter a string into column 1:
  • Tab off the grid to update the DataSet.The second column will not immediately update:
  • Press "Refresh" to force refreshing the DataGrid

Anticipative thanks and kind regards

-- 
Sebastian Daser
Softfount IT Solutions

Baumweg 16
60316 Frankfurt
Telefon +49-171-5853738

USt.-Nr. 012 811 08436


Viewing all articles
Browse latest Browse all 18858

Trending Articles