How to Fill TextBoxes from RadGridView

3 posts, 0 answers
  1. CAKA
    CAKA avatar
    5 posts
    Member since:
    Jun 2014

    Posted 13 Nov 2014 Link to this post

    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. CAKA
    CAKA avatar
    5 posts
    Member since:
    Jun 2014

    Posted 14 Nov 2014 in reply to CAKA Link to this post

    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();
  3. Boris
    Boris avatar
    276 posts

    Posted 14 Nov 2014 Link to this post


    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}">
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        <telerik:RadGridView Grid.Row="0"
                                ItemsSource="{Binding Clubs}"
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
                <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}"
        <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" />

    I attached a sample project that demonstrates the suggested approach.

    I hope this helps.


    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.

Back to Top