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

How to Fill TextBoxes from RadGridView

2 Answers 52 Views
GridView
This is a migrated thread and some comments may be shown as answers.
CAKA
Top achievements
Rank 1
CAKA asked on 13 Nov 2014, 03:10 PM
Hi
I can`t found how to fill my textBoxes with data from selected row in RadGridView.

textBox1.Text = ? //First cell.Value from selected row

Have a nice day!

2 Answers, 1 is accepted

Sort by
0
CAKA
Top achievements
Rank 1
answered on 14 Nov 2014, 08:29 AM
I found it :)

int selectedIndex = radGridView1.Items.IndexOf(radGridView1.SelectedItem);//Index of the row
var selectedRow = radGridView1.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as GridViewRow; //Selected Row

var columnNumber = selectedRow.Cells[0] as GridViewCell; //Number of the column
textBox1.Text = columnNumber.Value.ToString(); //Cell from selected row + number Column
            
columnNumber = selectedRow.Cells[1] as GridViewCell;
textBox2.Text = columnNumber.Value.ToString();
0
Boris
Telerik team
answered on 14 Nov 2014, 03:25 PM
Hello,

Although your approach for populating your TextBoxes is a possible one, I would like to caution you that it  can lead to some issues. Since RadGridView by default uses UI virtualization. For example if you select a row and then scroll down / up and the selected row goes out of the visible area, then your approach will not work. That is so because the ItemContainerGenerator relies on the selected row to be in the viewport. That is why you can not rely on the visual elements when the UI Virtualization is enabled. Please note that all the visual elements (for example GridViewCell, GridViewRow) will be created as they are brought into view. Then all the visual elements will be re-created when they go beyond the visible area and then come back into view.

Another possible approach to this issue is to bind your TextBoxes in XAML like so:

<Grid DataContext="{StaticResource MyViewModel}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
 
    <telerik:RadGridView Grid.Row="0"
                            Name="clubsGrid"
                            ItemsSource="{Binding Clubs}"
                            AutoGenerateColumns="False"
                            Margin="5">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
                                          Header="Est."
                                           DataFormatString="{}{0:yyyy}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}"
                                           Header="Stadium"
                                           DataFormatString="{}{0:N0}"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <TextBlock Text="Selected Row Data: " />
        <TextBox Text="{Binding SelectedItem.Name, ElementName=clubsGrid}" Width="200" />
        <TextBox Text="{Binding SelectedItem.Established, ElementName=clubsGrid}" Width="200" />
        <TextBox Text="{Binding SelectedItem.StadiumCapacity, ElementName=clubsGrid}" Width="200" />
    </StackPanel>
</Grid>


I attached a sample project that demonstrates the suggested approach.

I hope this helps.

Regards,
Boris
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
CAKA
Top achievements
Rank 1
Answers by
CAKA
Top achievements
Rank 1
Boris
Telerik team
Share this question
or