This is a migrated thread and some comments may be shown as answers.

Again with the Binding a TextBox to a GridView

3 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 23 Sep 2011, 12:12 PM
Hi,

I am new to WPF and Telerik controls.  I have been through the online documentation, the RadControls Demos that install with the controls, various forums and reference material including the telerik forums.  I have tried several pieces of code and I can't get the TwoWay binding to work.  I can achieve what I want to do with CodeBehind but that is sort of defeating the purpose.

So I have a RadGridView that is populated by a dataset.  I do this by the code below. rgvSamples is the RadGridView.  dsMain is a dataset.

rgvSamples.ItemsSource = dsMain.Tables["Samples"];

In XAML I then set the various DataContext and Bindings as seen below.

<StackPanel Height="357" HorizontalAlignment="Left" Margin="736,25,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="211">
            <Grid Height="51" Name="grid1" Width="212" DataContext="{Binding ElementName=rgvSamples, Path=SelectedItem}">
                <TextBox Height="23" HorizontalAlignment="Left" Margin="55,6,0,0" x:Name="textBox1" VerticalAlignment="Top" Width="143" Text="{Binding Path=CodeStr, Mode=TwoWay}"/>
                <Label Content="Label" Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" Width="43" />
            </Grid>
        </StackPanel>

The above code above does not actually work even though it is almost identical to some of the solutions posted on this forum.  I also have a standalone Textbox as seen in the code below which also does not work.

<TextBox Height="36" HorizontalAlignment="Left" Margin="580,145,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=rgvSamples, Path=SelectedItem.CodeStr}"/>

Now by making the following changes I get the first value in the GridView to show but that is it.  If I select a different row then the value in the textbox does not change.

rgvSamples.ItemsSource = dsMain.Tables["Samples"];
            stackPanel1.DataContext = rgvSamples.ItemsSource;

<StackPanel Height="357" HorizontalAlignment="Left" Margin="736,25,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="211">
            <Grid Height="51" Name="grid1" Width="212" DataContext="{Binding CurrentItem}">
                <TextBox Height="23" HorizontalAlignment="Left" Margin="55,6,0,0" x:Name="textBox1" VerticalAlignment="Top" Width="143" Text="{Binding CodeStr, Mode=TwoWay}"/>
                <Label Content="Label" Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" Width="43" />
            </Grid>
        </StackPanel>

So I have been going through forum posts etc for about 3 days now trying to see what i am doing wrong.  can some one please point me in the correct direction.

Thanx
Kind Regards
Richard

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 23 Sep 2011, 03:07 PM
Hello Richard,

 You should better set the ItemsSource of the GridView to the .DefaultView of the table?

rgvSamples.ItemsSource = dsMain.Tables["Samples"].DefaultView;

Please let me know if this helps .

Best wishes,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Richard
Top achievements
Rank 1
answered on 26 Sep 2011, 05:52 AM
Hi,

Thanx for the replay.  This answer works.  The only problem is I don't understand why it works.  I went and read up on DefaultView and there is nothing special about it (besides the filtering and sorting) that would make the binding and twoWay mode between a row in a Grid and a Textbox work.

Can you please explain why DefaultView works over DataTable or DataRow.

Thanx
Kind Regards
Richard
0
Dimitrina
Telerik team
answered on 27 Sep 2011, 04:18 PM
Hello Richard,

The DataTable is an IListSource and the DataView is an IBindingList. That is why in that case, the DefaultView works over DataTable.  

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Richard
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Richard
Top achievements
Rank 1
Share this question
or