This question is locked. New answers and comments are not allowed.
Hi,
I'm using RadGridView to display list of contacts. The grid contains three fixed columns and set of dynamic columns which is fetched from a web service.Dynamic columns are added to the grid from the View model. Each contact has a dictionary(Dictionary<Guid, DynamicCellValue >) .Here key of dictionary is the Id of 'DynamicColumn' object. 'DynamicCellValue' object has Value property and IsRelevant propery. Value property is bounded to DisplayMemberPath of dynamic column. But some contacts doesn't need value for some dynamic columns. Those cells should be empty and disabled. The IsRelevant property for that cells will be false. I want to disable the cell depending on the IsRelevant boolean property of DynamicCellValue. I couldn't find a way to do this.
Can any one please help me in this?
e.g. I want to disable the cell for Dynamic Column1 for first contact since the IsRelevant property is false for that cell for first contact.
And also I'm not sure if using a dictionary is a proper way to bind the data. I appreciate your suggestions regarding this as well.
This is a sample code which explains the scenario.
I'm using RadGridView to display list of contacts. The grid contains three fixed columns and set of dynamic columns which is fetched from a web service.Dynamic columns are added to the grid from the View model. Each contact has a dictionary(Dictionary<Guid, DynamicCellValue >) .Here key of dictionary is the Id of 'DynamicColumn' object. 'DynamicCellValue' object has Value property and IsRelevant propery. Value property is bounded to DisplayMemberPath of dynamic column. But some contacts doesn't need value for some dynamic columns. Those cells should be empty and disabled. The IsRelevant property for that cells will be false. I want to disable the cell depending on the IsRelevant boolean property of DynamicCellValue. I couldn't find a way to do this.
Can any one please help me in this?
e.g. I want to disable the cell for Dynamic Column1 for first contact since the IsRelevant property is false for that cell for first contact.
And also I'm not sure if using a dictionary is a proper way to bind the data. I appreciate your suggestions regarding this as well.
This is a sample code which explains the scenario.
public class DynamicCellValue { public Guid ColumnId { get; set; } public string Value { get; set; } public bool IsRelevant { get; set; } }
public class DynamicColumn
{
public string Header { get; set; }
public Guid Id { get; set; }
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Mobile { get; set; }
public Dictionary<string, DynamicCellValue> DynamicValuesList { get; set; }
}
public class ViewModel : NotifyPropertyChanged
{
MainPage mainPage;
public ViewModel(MainPage mp)
{
mainPage = mp;
GetDynamicColumns();
CreateGrid();
GetContacts();
}
private ObservableCollection<Contact> contactsList = new ObservableCollection<Contact>();
public ObservableCollection<Contact> ContactsList
{
get { return contactsList; }
set
{
contactsList = value;
OnPropertyChanged("ContactsList");
}
}
public List<DynamicColumn> DynamicColumnsList { get; set; }
/// <summary>
/// add dynamic columns to the grid and bind data
/// </summary>
private void CreateGrid()
{
foreach (var col in DynamicColumnsList)
{
GridViewDataColumn column = new GridViewDataColumn();
var bindingPath = "DynamicValuesList[" + col.Id.ToString() + "].Value";
Binding bind = new Binding();
bind = new Binding(bindingPath);
column.DataMemberBinding = bind;
column.Header = col.Header;
mainPage.contactsGrid.Columns.Add(column);
}
}
/// <summary>
/// Get list of dynamic columns from the web service
/// </summary>
private void GetDynamicColumns()
{
//These dynamic columns will be fetched from a web service
DynamicColumnsList = new List<DynamicColumn>();
DynamicColumnsList.Add(new DynamicColumn { Header = "Dynamic Column1", Id = Guid.NewGuid() });
DynamicColumnsList.Add(new DynamicColumn { Header = "Dynamic Column2", Id = Guid.NewGuid() });
}
/// <summary>
/// Get list of contacts with dynamic column values from the web service
/// </summary>
private void GetContacts()
{
//These contacts will be fetched from a web service
ContactsList.Add(new Contact { FirstName = "John", LastName = "Carter", Mobile = "2123d456" });
ContactsList.Add(new Contact { FirstName = "Shane", LastName = "Watson", Mobile = "31266748" });
ContactsList.Add(new Contact { FirstName = "Erik", LastName = "Hansen", Mobile = "88907446" });
for (int i = 0; i < contactsList.Count; i++)
{
var dynamicValues = new Dictionary<string, DynamicCellValue>();
for (int j = 0; j < DynamicColumnsList.Count; j++)
{
bool isRelevant = true;
string value = "";
if (i == 0 && j == 0)
{
isRelevant = false; // Disable first dyanamic column value for first contact
}
else
{
value = contactsList[i].FirstName + " " + DynamicColumnsList[j].Header;
}
var dynamicValue = new DynamicCellValue { Value = value, IsRelevant = isRelevant };
dynamicValues.Add(DynamicColumnsList[j].Id.ToString(), dynamicValue);
}
contactsList[i].DynamicValuesList = dynamicValues;
}
}
}
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MainPage.xaml
<UserControl x:Class="GridViewDemo.MainPage"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" >
<telerik:RadGridView x:Name="contactsGrid" ItemsSource="{Binding ContactsList,Mode=TwoWay}" AutoGenerateColumns="False" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="First name" DataMemberBinding="{Binding FirstName}" />
<telerik:GridViewDataColumn Header="Last name" DataMemberBinding="{Binding LastName}" />
<telerik:GridViewDataColumn Header="Mobile" DataMemberBinding="{Binding Mobile}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</UserControl>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.DataContext = new ViewModel(this);
}
}