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

    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. 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. DevCraft banner
  4. Boris
    Admin
    Boris avatar
    276 posts

    Posted 14 Nov 2014 Link to this post

    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.

     
Back to Top