Hi
Say I have a column called CategoryId that contains a numeric value and I want to display a text representation of that value in each cell plus allow the user to edit the value by using a ComboBox editor.
In the asp.net/windows forms grid i could do this by use of a ValueList or ComboBox, setting the datasource of the ComboBox and setting the displaymember and valuemember properties. How can i do this with the xamWebGrid?
Thanks again.
Kevin
We have an example of exactly how to do this using the TempalteColumn in our samples: http://samples.infragistics.com/silverlight/
xamWebGrid > Data Binding and Interaction > Cell Editor Templates
The basic XAML looks like this:
<igGrid:TemplateColumn Key="Category" Width="120" HorizontalContentAlignment="Stretch"> <igGrid:TemplateColumn.ItemTemplate> <DataTemplate> <TextBlock x:Name="CategoryDisplay" Text="{Binding Category}" /> </DataTemplate> </igGrid:TemplateColumn.ItemTemplate> <igGrid:TemplateColumn.EditorTemplate> <DataTemplate> <ComboBox x:Name="CategoryEditList" ItemsSource="{StaticResource categoryProvider}" SelectedItem="{Binding Category, Mode=TwoWay, Converter={StaticResource categoryConverter}, ConverterParameter={StaticResource categoryProvider}}" DisplayMemberPath="Value" /> </DataTemplate> </igGrid:TemplateColumn.EditorTemplate></igGrid:TemplateColumn>
Thanks for that example. I didn't notice you could scroll to the right for even more samples !
I have followed the example and it is not quite the same as my situation. The sample has a category column that displays category names from the itemsource and the combobox is also populated with category names. What I have is a categoryId column with the numeric Id of the category. The itemsource of the combobox is a list of categories with ID and Name properties. I want the grids categoryId column to display the category.name resolved from combobox editor using the id's. This is achievable with asp.net grid and win forms grid.
Can this be done?
Thanks
Assuming I fully understand your scenario, yes this is possible. What I hear you saying is that in addition to the ComboBox value editor, you want the cell to display a string based on the cells bound numeric value.
To do this all I would do is create a custom ValueConverter that does the ID to string resolution (probably by simply executing a LINQ query against the same items source that I am binding to the combobox), then add that to the binding statement of the TextBlock in the ItemTemplate.
Devin
That helped a lot. Thanks.
Thanks for the quick reply. Unfortunately, I am working on a different section and can't yet try this out. The project isn't using mvvm, but I should be able to get what I need from this.I will report back when I can try it out.
Hi,
Ok, so here is a basic sample, that just tries to show the concept of what i believe you want.
1. You have a Person and an Employee class:
public class Employee
{
public string ID
get;
set;
}
public class Person
public string Name
2. Then you have your ViewModel, which has a collection of People and a Collection of Employees:
public class ViewModel
ObservableCollection<Employee> _employees;
public ObservableCollection<Employee> Employees
get
if (this._employees == null)
this._employees = new ObservableCollection<Employee>();
for (int i = 0; i < 10; i++)
this._employees.Add(new Employee() { ID = i.ToString() });
return this._employees;
ObservableCollection<Person> _people;
public ObservableCollection<Person> People
if (this._people == null)
this._people = new ObservableCollection<Person>();
this._people.Add(new Person() {Name="Name of: " + i.ToString() });
return this._people;
3. Then you'll need a converter to convert back and forth between an Employee and a Person
public class EmployeConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
ViewModel vm = parameter as ViewModel;
if (vm != null)
ObservableCollection<Person> people = vm.People;
string id = value as string;
if (id != null)
foreach (Person p in people)
if (p.Name.Contains(id))
return p;
return value;
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
ObservableCollection<Employee> employees = vm.Employees;
Person p = value as Person;
if (p != null)
foreach (Employee e in employees)
if (p.Name.Contains(e.ID))
return e.ID;
4. Finally, you create your xamGrid, and use a TemplateColumn with bindings using your ViewModel and your Converter:
<local:ViewModel x:Key="vm"/>
<local:EmployeConverter x:Key="ec"/>
<ig:XamGrid x:Name="grid" Height="400" Width="400" ItemsSource="{Binding Source={StaticResource vm}, Path=Employees}">
<ig:XamGrid.Columns>
<ig:TemplateColumn Key="ID">
<ig:TemplateColumn.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"/>
</DataTemplate>
</ig:TemplateColumn.ItemTemplate>
<ig:TemplateColumn.EditorTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource vm}, Path=People}" DisplayMemberPath="Name" SelectedItem="{Binding Path=ID, Converter={StaticResource ec}, ConverterParameter={StaticResource vm}, Mode=TwoWay}" />
</ig:TemplateColumn.EditorTemplate>
</ig:TemplateColumn>
</ig:XamGrid.Columns>
<ig:XamGrid.EditingSettings>
<ig:EditingSettings AllowEditing="Cell"/>
</ig:XamGrid.EditingSettings>
</ig:XamGrid>
Note: this is a very simplistic example of doing conversions with a Converter. But hopefully it'll provide you with the basic understanding of what you'll need to do to make this work.
-SteveZ
HI,
Thanks for the reply.
Specifically what I am trying to do:
I have a xamdatagrid which bound to employee daily activity data. I need a combobox in that grid that shows employee names from another table, but returns an employeeid to the grid. I can do this all day long in your asp.net grid, but am struggling with accomplishing the same thing in xamdatagrid.I have poured over the examples in here and in the samples browser and for some reason I am still stuck.
My setup is entity framework > domain services > observable collections in the code behind. I work in vb mostly, but can convert if needed.
I hope that makes sense. Sorry for the snarky tone in the post. It was at the end of an all-night session.
This is an old thread, and i'm not 100% sure what exactly are you looking for a sample of.
Using a ValueConverter?