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

[Solved] Manipulating specific data in C#

5 Answers 184 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.
Adrian
Top achievements
Rank 1
Adrian asked on 08 Dec 2010, 04:28 PM
Hello,

I've problem with editing the content of a GridView.
There is a DataBinding witch displays the name of users in the first column. When this binding is done, I add some more columns in C# one by one an give them a adequate name. This works fine, but my problem is that I can't access the cells of the additionally added columns. As there is no binding to them I've tried to run through my user list, which is working fine (changing user name etc.). But how do I access the other columns? Cane some one give me some ideas to solve that?

Just to give a clear imagination of the task. The added columns represent rooms the a user can have access to. When he has access then I want to display an X. (later maybe a picture)

Thanks ,
Adrian

5 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 08 Dec 2010, 05:03 PM
Hi Adrian,

 
We need some more information about this issue:

1. How your RadGridView is bound to?
2. Have you defined CellTemplates/CellEditTemplates in your columns?
3.Which column types did you use?

Can you please send us a small application via support ticket that represents your case. Also you may share the complete code snippet that will help us to see what is going on and provide you with an appropriate solution.

Kind regards,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Adrian
Top achievements
Rank 1
answered on 09 Dec 2010, 03:57 PM
Hello Vanya,

thank you for fast replay. I'm using a WCF connection to receive data from a database server. Then I do two things with that data: Fist I create all the columns and add rows to the GridView by using DataBindings. Second I want to add some values to the "inner" part of the GridView. I've created a small demo application which is using static data from a generator. The first column is using cell templates but not the dynamic part. I need to know how to access the cells.You can find comment  in the example where I've tried to do this.

The Visual Studio 2010 Solution: AccessMap.rar

XMAL:
<UserControl
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:local="clr-namespace:AccessMap" x:Class="AccessMap.AccessMapPage"
    mc:Ignorable="d" Height="500" Width="700">
  
    <Grid x:Name="AccGrid" >
        <sdk:Label Height="22" HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="label3" VerticalAlignment="Top" Width="194" Content="Access Rights" FontSize="14" FontWeight="Bold" />
  
        <telerikGrid:RadGridView FrozenColumnCount="2" x:Name="accMapDG" Margin="12,40,12,0" ShowInsertRow="False" CanUserReorderColumns="False" CanUserSortColumns="False" ShowGroupPanel="False" Height="420" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserDeleteRows="False" >
            <telerikGrid:RadGridView.Columns>
                <!-- The static columns are defined in xaml code. All other in the c# section dynamically. -->
                <!-- First column should be set to a symbol that indicates the type of user -->
                <telerikGrid:GridViewImageColumn DataMemberBinding="{Binding Icon}" ImageHeight="10" ImageWidth="10" />
                  
                <!-- This column contains the user group name -->
                <telerikGrid:GridViewColumn Header="user group" IsFilterable="True" IsGroupable="False" IsReadOnly="True" IsReorderable="False" IsResizable="False" IsSortable="False" ShowDistinctFilters="True" Width="130">
                    <telerikGrid:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Width="auto" Height="25" VerticalAlignment="Center" Orientation="Horizontal">
                                <TextBlock FontWeight="Bold" Text="{Binding UserGroupName}" x:Name="nameColumn" HorizontalAlignment="Left" VerticalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                    </telerikGrid:GridViewColumn.CellTemplate>
                </telerikGrid:GridViewColumn>
  
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
          
        <telerik:RadButton IsEnabled="True" Height="22" Margin="0,466,12,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" Content="refresh" HorizontalAlignment="Right" Width="98" VerticalAlignment="Top" x:Name="refreshBT" Click="refreshBT_Click" />
    </Grid>
</UserControl>

C#:
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
  
namespace AccessMap {
    public partial class AccessMapPage : UserControl {
        // constructor
        public AccessMapPage() {
            InitializeComponent();
  
            // event handler
            accMapDG.RowLoaded += new EventHandler<Telerik.Windows.Controls.GridView.RowLoadedEventArgs>(accMapDG_RowLoaded);
  
            refreshAccMap();
        }
  
        // receive LockGroupList (generated data in this case)
        private void getLockGroupList() {
            ObservableCollection<LockGroup> lockGroupList = DataGenerator.createDemoData_LockGroupList();
  
            // delete all column except the first and second one
            while( accMapDG.Columns.Count > 2 ) {
                accMapDG.Columns.RemoveAt(2);
            }
  
            // add an column for every lock group
            foreach( LockGroup lg in lockGroupList ) {
                GridViewColumn gvc = new GridViewColumn();
                gvc.Header = lg.LockGroupName;
                // set columns width
                if( lg.LockGroupName.Length * 8 < 160 ) {
                    gvc.Width = lg.LockGroupName.Length * 8;
                } else {
                    gvc.Width = 160;
                }
                gvc.UniqueName = lg.LockGroupID.ToString();
                accMapDG.Columns.Add(gvc);
            }
  
            // get/ generate user groups
            getUserGroupList();
  
        }
          
        private void getUserGroupList() {
            // set item source of the grid view
            accMapDG.ItemsSource = DataGenerator.createDemoData_UserGroupList();
  
            // request the content of table (access rights)
            setValuesForUserGroupAccess();
        }
  
        // run through all cells to add some content 
        private void setValuesForUserGroupAccess() {
            foreach( UserGroup userGroup in (accMapDG.ItemsSource as ObservableCollection<UserGroup>) ) {
                foreach( GridViewColumn gvc in accMapDG.Columns ) {
                    ////////////////////////////////////////////
                    // TODO: add some random content to cell  //
                    ////////////////////////////////////////////
  
                    // e.g.
                    // cell.Text = new Random().Next(100).ToString();
                }
            }
        }
   
        private void refreshBT_Click(object sender, RoutedEventArgs e) {
            refreshAccMap();
        }
  
        private void refreshAccMap() {
            // clear current item source
            accMapDG.ItemsSource = null;
              
            // load new data
            getLockGroupList();
        }
          
        void accMapDG_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) {
            foreach( GridViewCellBase cell in e.Row.Cells ) {
                // edit the header cells
                if( (cell as GridViewHeaderCell) != null ) {
  
                }
                  
                // change the font color of the first column (containing the user group names)
                if( (cell as GridViewCell) != null && ((cell as GridViewCell).Content as StackPanel) != null ) {
                    GridViewCell gvc = cell as GridViewCell;
                    TextBlock tb = ((gvc.Content as StackPanel).FindName("nameColumn")) as TextBlock;
                    // all datasets where UserRep is set should have a different font color
                    if( (e.Row.DataContext as UserGroup).UserRep ) {
                        // DodgerBlue #FF1E90FF
                        tb.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x90, 0xFF));
                        // remove first character 
                        tb.Text = tb.Text.Substring(1);
                    } else {
                        // DarkSlateBlue #FF483D8B
                        tb.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x48, 0x3D, 0x8B));
                    }
                }
            }
        }
  
    }
}


Tanks and regards
Adrian
0
Vanya Pavlova
Telerik team
answered on 09 Dec 2010, 04:31 PM
Hi Adrian,

 
Thank you for submitting your project. The main problem is that you are using GridViewColumn, instead of using GridViewDataColumn. Once you have an appropriate column you may defined its CellTemplate where you can bind various controls to the properties of the underlying object. I have changed a little bit your example where I load the CellTemplate for each of these dynamically generated columns at once.

Please see the updated project and let me know if this works for you.

All the best,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Adrian
Top achievements
Rank 1
answered on 10 Dec 2010, 09:41 AM
Hi Vanya,

thank you for the example it is also useful for my application. What I thought in my first post is a possibility to change the content of a specific cell. For example changing the cell Tom/Office. How can I realize that?

Regards
Adrian
0
Vanya Pavlova
Telerik team
answered on 10 Dec 2010, 06:07 PM
Hi Adrian,

The correct approach is to edit the underlying property of the desired object, instead of editing the value of the cells. If the business object implements the INotifyPropertyChanged interface - the gridview will update automatically.
If you want to access specific particular cell within RadGridView you may use CurrentCellInfo class, read more about  this class in the following article. Once you have access to any cell you may add your custom logic to change its content.

If you need any further assistance please let me know.

Regards,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Adrian
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Adrian
Top achievements
Rank 1
Share this question
or