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

Dynamic Grid - Use of objects instead of strings

7 Answers 69 Views
GridView
This is a migrated thread and some comments may be shown as answers.
HDC
Top achievements
Rank 1
HDC asked on 25 May 2012, 05:12 AM
Hi,

I have create a dynamic grid based on the demo:
http://demos.telerik.com/silverlight/#GridView/DataSources

using the MyDataRow.

This works perfectly if i just enter strings as value for each column the datarow. However, i would like to store a custom object in those values:

public class DynamicDataColumn : Common.NotifyBaseClass
  {
    private Dictionary<int, string> _items;
 
    public Dictionary<int, string> Items
    {
      get
      {
        return _items;
      }
      set
      {
        _items = value;
 
        this.OnPropertyChanged("MenuResume");
      }
    }
 
    public DynamicDataColumn()
    {
      Items = new Dictionary<int, string>();
 
      Items.Add(1, "Abc");
      Items.Add(2, "Def");
    }
 
  }


I created a template:
<DataTemplate x:Key="ComboColumn">
            <ComboBox ItemsSource="{Binding Items}">               
            </ComboBox>
</DataTemplate>

Within the AutoGeneratingColumn event, i managed to assign the template to each column. This works fine and i can see the comboboxes.

However, the combo box inside the template should bind to the object that i have stored in the value of each column of MyDataRow and this doesn't work, when i debug the binding, i noticed that the template binds to the entire MyDataRow and not to the individual value of each column.

Is it possible to specify a binding here or how do i go about realizing the scenario?

Best Regards,

Peter


7 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 25 May 2012, 10:45 AM
Hi Peter,

I am not sure I understand the part: "However, the combo box inside the template should bind to the object that i have stored in the value of each column of MyDataRow".

In case you can send me your runnable project with some details on what data you need displayed in the combo I will gladly  set up this one for you.

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
HDC
Top achievements
Rank 1
answered on 25 May 2012, 07:14 PM
Hi Pavel,

Thanks a lot for the assistance. You'll find the project in:

https://skydrive.live.com/?cid=b8aa36071c47a567#cid=B8AA36071C47A567&id=B8AA36071C47A567%21113

It's a public folder on skydrive, i hope you'll be able to access it. There is only one file, you can't miss it.

Here is some more clarification.

You'll see from my demo, that i create a dynamic grid using the MyDataRow object. The values that i store in the columns are of type DynamicDataColumn:

MenuResume = new ObservableCollection<MyDataRow>();
 
      MyDataRow row = new MyDataRow();
 
      row["Column1"] = new DynamicDataColumn() { Id = 1, Description = "Abc" };
      row["Column2"] = new DynamicDataColumn() { Id = 2, Description = "Def" };
 
      _menuresume.Add(row);
 
      MyDataRow row2 = new MyDataRow();
 
      row2["Column1"] = new DynamicDataColumn() { Id = 1, Description = "Abc" };
      row2["Column2"] = new DynamicDataColumn() { Id = 2, Description = "Def" };
 
      _menuresume.Add(row);

When you run the code, you'll notice that the grid will display the name of the DynamicDataColumn class, that is the first problem, it should display "Abc" or "Def".

When you click on the cell, it wil enter edit mode and you'll see a combo. That combo is empty, it should contain the values "Abc" and "Def" which i add in the constructor of the DynamicDataColumn. That is problem number two

Problem number 3 is putting it all together... how can i make sure that when i select something in the combo, that the underlying data in MyDataRow is changed for that cell. (for example Id = 2 instead of = 1)

Right now the combo content is hardcoded, but this will vary per cell, one cell will contain "Abc","Def" another one may contain "Xyz", "Klm". This is not a problem, i will be able to do that easily using a small modification on the current code.

It would be great if you could help me out here.

Best Regards,

Peter

PS: another problem is that i cannot bind the grid to the "MenuResume" collection on the viewmodel, if i do that the grid will contain only empty values. I need to set the itemsource from codebehind or it doesn't work at all. I also wonder what i'm doing wrong there or if this is by design.
0
Pavel Pavlov
Telerik team
answered on 30 May 2012, 03:30 PM
Hello Peter,

You can modify the combo template as follows :
<DataTemplate x:Key="ComboColumn">
      <ComboBox ItemsSource="{Binding [Column1].Items}" SelectedValue="{Binding Id}" DisplayMemberPath="Value" SelectedValuePath="Key">
      </ComboBox>
  </DataTemplate>

Also for the column you will need to set the DataMember binding to point to the Id property in order to ensure proper display when the cell is in display rather than in edit mode.

*However RadGridView offers the GridViewComboBoxColumn for such purposes.  Let me know in case you decide to use GridViewComboBoxColumn  and need some sample code within your project.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
HDC
Top achievements
Rank 1
answered on 31 May 2012, 06:58 AM
Hi Pavel,

I'll take a look at it this evening, thanks a lot already for the assistance.

I'm not sure i fully understand what you mean with:

"Also for the column you will need to set the DataMember binding to point to the Id property in order to ensure proper display when the cell is in display rather than in edit mode."

it's a dynamic grid, so i'm not defining any columns, do you mean that i will have to set this binding in the event that is fired when the grid generates the columns? If so can you give a sample of code that would work in the demo code?

With regards to the "GridViewComboBoxColumn", i have the same remark... columns are added dynamically, i couldn't quite figure out how you can change the type of the column as they are being generated, maybe you could provide some sample code for that too?

Best Regards,

Peter




0
HDC
Top achievements
Rank 1
answered on 03 Jun 2012, 07:27 AM
Hi Pavel,

i'm trying this suggestion:

However RadGridView offers the GridViewComboBoxColumn for such purposes.

I can't get this to work, the combo box doesn't want to appear, i'm using following code:

void Resume_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
      {
          GridViewComboBoxColumn g = new GridViewComboBoxColumn();         
          g.ItemsSourceBinding = new Binding("[" + e.Column.UniqueName + "].Items");
          g.DisplayMemberPath = "Value";
          g.SelectedValueMemberPath = "Key";         
          e.Column = g;       
      }

But all it displays is an empty grid.

Can you show me on the demo how i would have to go about binding this column and also on how i can make sure that changes are reflected back to the underlying data of the grid.

Best Regards,

Peter
0
Accepted
Pavel Pavlov
Telerik team
answered on 05 Jun 2012, 01:18 PM
Hi ,

I am attaching a modified version of your demo.I have modified it to use a combo box column .

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
HDC
Top achievements
Rank 1
answered on 06 Jun 2012, 09:44 AM
Hi Pavel,

Thank you for the demo code, it resolves the problem.

What i find troublesome though is that the combo's need to be loaded up entirely before you bind the grid in code behind. In my case i used WCF to load some of the combo's. Although the combo's do contain the data, the grid will not display any content in the cells on startup. This is caused because at the time that the grid is bound the combo's have not been loaded up just yet.

I finaly resolved that by loading all the combo's and grid data in a single operation... yet another gift of Silverlight's asynchronous way of executing WCF request. Now i have to figure out how i can reload the combo in column 2 when the combo in column 1 is changed. Given the fact that the columns are dynamic that will probably not be so easy either.

Best Regards and thanks a million for the support,

Peter
Tags
GridView
Asked by
HDC
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
HDC
Top achievements
Rank 1
Share this question
or