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

[Solved] Setting Cell content programmatically

8 Answers 723 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nicolas Porto
Top achievements
Rank 1
Nicolas Porto asked on 14 Sep 2009, 03:18 AM
Hi,

I'm trying to set the content of a specific cell programmatically, can you help me please?

8 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 14 Sep 2009, 06:04 AM
Hi Nicolas Porto,

For example, if you want to update the value of the first cell of the current row you can use the following code:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)  
{  
    GridViewRow row = (GridViewRow)this.playersGrid.ItemsControl.ItemsGenerator.ContainerFromItem(this.playersGrid.CurrentItem);  
    GridViewCell firstCell = row.Cells[0] as GridViewCell;  
 
    firstCell.Value = "NewValue";  

You should also use TwoWay binding for your column if you would like to update the underlying data item as well:

<telerik:RadGridView.Columns>  
                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name, Mode=TwoWay}">  
                </telerik:GridViewDataColumn>  
</telerik:RadGridView.Columns> 

All the best,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
omar lara
Top achievements
Rank 1
answered on 14 Sep 2009, 07:12 PM
A lot of thanks for the quick response.

Regards
0
Josh
Top achievements
Rank 1
answered on 12 Feb 2011, 03:36 AM
I'm following this example and although this lets me change the gridview cell's value without error, the underlying property that the column is bound to is not getting updated.  I do have two way binding set.

From the example, after clicking the button, checking firstCell.Value gives me back "NewValue", however, "PlayersGrid.Item(0).Name" returns me the old value.

I will gladly provide sample code if necessary however my test case is exactly the same as the example.  The only difference is that instead of "ItemsControl.ItemsGenerator", I'm using "ItemContainerGenerator".  I'm guessing the method changed.   Modifying the underlying object directly may be difficult since in my actual application the underlying object class is dynamically generated at run time.

Any help would be greatly appreciated.

Thanks,

-Josh
0
Milan
Telerik team
answered on 14 Feb 2011, 04:25 PM

Hi Josh,

The better approach is to set properties on your data items instead of setting values on our UI elements. Could you please provide a sample project to find out if we can figure out a solution that does not involve the UI.



Regards,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Josh
Top achievements
Rank 1
answered on 14 Feb 2011, 05:54 PM
In this sample, if you right click on a cell in a grid a menu appears with the option to "Fill Right".  This copies the value of a cell to the cells on the right of it.  This sort of works but does not update the underlying ItemsSource and if you click on a cell where the value was changed, you will see it revert back to the old value which is not the intended behavior.

You may notice that I am filling the grid using a DataTable class I got from the link below.
DataTable blog

The reason I am using this DataTable is that the column names and number of columns will be unknown until runtime.  Unlike the sample project's static sample data, they are pulled from a database in which the number of "zones" and the name of each zone will change often.

I'm also open to a new approach if you could suggest one that might work.

-Josh

MainPage.xaml
<Grid x:Name="LayoutRoot" Background="White">
    <telerikGridView:RadGridView x:Name="gvZone" ItemsSource="{Binding TestSource}">
        <telerikNav:RadContextMenu.ContextMenu>
            <telerikNav:RadContextMenu Name="cMenu" Opened="cMenu_Opened"/>
        </telerikNav:RadContextMenu.ContextMenu>
    </telerikGridView:RadGridView>
</Grid>

MainPage.xaml.vb
Imports System.ComponentModel
Imports Telerik.Windows
Imports Telerik.Windows.Controls
Imports Telerik.Windows.Controls.GridView
 
Partial Public Class MainPage
    Inherits UserControl
    Implements INotifyPropertyChanged
 
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
 
    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
 
    Public Sub New()
        InitializeComponent()
 
        Me.DataContext = Me
 
        'Begin creating sample data
        Dim Zones As New List(Of Dictionary(Of String, Object))
 
        Dim Row1 As New Dictionary(Of String, Object)
        Row1.Add("Field Zone", 0.0024)
        Row1.Add("Zone 1A", 0.0079)
        Row1.Add("Zone 1B", 0.013)
        Row1.Add("Zone 2", 0.0141)
        Zones.Add(Row1)
 
        Dim Row2 As New Dictionary(Of String, Object)
        Row2.Add("Field Zone", 0.0079)
        Row2.Add("Zone 1A", 0.0055)
        Row2.Add("Zone 1B", 0.0106)
        Row2.Add("Zone 2", 0.0117)
        Zones.Add(Row2)
 
        Dim Row3 As New Dictionary(Of String, Object)
        Row3.Add("Field Zone", 0.013)
        Row3.Add("Zone 1A", 0.0106)
        Row3.Add("Zone 1B", 0.0051)
        Row3.Add("Zone 2", 0.0062)
        Zones.Add(Row3)
 
        Dim Row4 As New Dictionary(Of String, Object)
        Row4.Add("Field Zone", 0.0141)
        Row4.Add("Zone 1A", 0.0117)
        Row4.Add("Zone 1B", 0.0062)
        Row4.Add("Zone 2", 0.0011)
        Zones.Add(Row4)
 
        Dim dt As New SQLWiz.Data.DataTable(Zones)
        'End creation of sample data
 
        TestSource = dt
    End Sub
 
    Private _TestSource As SQLWiz.Data.DataTable
    Public Property TestSource() As SQLWiz.Data.DataTable
        Get
            Return _TestSource
        End Get
        Set(ByVal value As SQLWiz.Data.DataTable)
            _TestSource = value
            NotifyPropertyChanged("TestSource")
        End Set
    End Property
 
    Private Sub cMenu_Opened(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim ClickedGridCell As GridViewCell = cMenu.GetClickedElement(Of GridViewCell)()
        cMenu.Items.Clear()
        CreateMenu(ClickedGridCell)
    End Sub
 
    Private Sub CreateMenu(ByVal ClickedGridCell As GridViewCell)
        Dim mItem As RadMenuItem
 
        mItem = New RadMenuItem
        mItem.Header = "Fill Value Right"
        mItem.Tag = ClickedGridCell
        cMenu.Items.Add(mItem)
        AddHandler mItem.Click, AddressOf FillRight
    End Sub
 
    Sub FillRight(ByVal sender As Object, ByVal e As RadRoutedEventArgs)
        Dim mItem As RadMenuItem = sender
        Dim ClickedGridCell As GridViewCell = mItem.Tag
        Dim NewValue As String = ClickedGridCell.Value
 
        For Each cell As GridViewCell In ClickedGridCell.ParentRow.Cells
            If cell.Column.DisplayIndex > ClickedGridCell.Column.DisplayIndex Then
                cell.Value = NewValue
                cell.Content = NewValue
 
                'At this point cell.Value and cell.Content will be NewValue, however, the underlying datatable "TestSource" remains unchanged.
            End If
        Next
    End Sub
End Class
0
Milan
Telerik team
answered on 18 Feb 2011, 09:07 AM

Hello Josh,

If you download the latest update of our DataTable which is available here you can programatically set value to the data rows (working with the data itself). For example:

Sub FillRight(ByVal sender As Object, ByVal e As RadRoutedEventArgs)
 {
  
    Dim mItem As RadMenuItem = sender
    Dim ClickedGridCell As GridViewCell = mItem.Tag
    Dim row As GridViewRow = ClickedGridCell.ParentRow
  
    Dim data as DynamicObject = row.DataContext
  
    '' Set new value for property Name
    data.SetValue("Name", "NewName");
 }



Kind regards,
Milan
the Telerik team
0
Milan
Telerik team
answered on 18 Feb 2011, 09:08 AM

Hello Josh,

Forgot to paste the link. Here it is.



Greetings,
Milan
the Telerik team
0
Josh
Top achievements
Rank 1
answered on 18 Feb 2011, 04:08 PM
That looks like it will work.  Thanks.

-Josh
Tags
GridView
Asked by
Nicolas Porto
Top achievements
Rank 1
Answers by
Milan
Telerik team
omar lara
Top achievements
Rank 1
Josh
Top achievements
Rank 1
Share this question
or