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

Populate Cells in grid, without binding to dataset

9 Answers 160 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eduan Marais
Top achievements
Rank 2
Eduan Marais asked on 19 May 2010, 01:29 PM
HI.

I am building a grid in code, and needs to add rows ( and populate theirs cells ) in runtime.

Creating the columns was easy, but I battle to edit the cell values.

This is what I have tried.

grid.Items.AddNew()  creates a new row in the grid. I would now like to assign a value to cell[0] in the new row.
grid.Items[0] does not contain a method to access the cells.

How do I get to the cells?

I know that binding the grid to a datatable will be much easier, but I was hoping to access the cells directly, without having to create a table for the data first.

Thx!

 

 

9 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 19 May 2010, 02:57 PM
Hi Eduan Marais,

RadGridView has a Property - ShowInsertRow that in case being set to "true", provides a special new row prompting you to add new row to the Grid. 
I hope that helps, but in case I have misunderstood your requirements, please give me more details.


Greetings,
Maya
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Eduan Marais
Top achievements
Rank 2
answered on 19 May 2010, 08:39 PM
Hi Maya.

Not exactly my problem, so let me rephrase.

I need to populate a grid dynamically - all done in code. I will receive a dataset with information, but this information is not all relevant to the grid, so I can not bind directly to the dataset.
So, I have to extract some of the data from the dataset, and add it to specific columns+Rows in the grid.
I have to set values to specific cells. I can't find a way to do this, not sure if I am missing the point here.

So, this is what I have done up to now.

GridViewDataColumn col1 = new GridViewDataColumn();

 

col1.UniqueName = "Column1";

GridViewDataColumn col2 = new GridViewDataColumn();

 

col2.UniqueName = "Column2";

 

grid.Columns.Add(col1);
grid.Columns.Add(col2);

Great! So now I have 2 columns in my grid. I now add a row to the grid:
grid.Items.AddNew();

So now I have 2 columns and 1 row in my grid.
I would now like to make the value on Column1, Row 1 equal to 5. I do not know how to do this, as I do not find a method to access the cell in the row.
In other words, there is no grid.Items[0][0].value or something to get to the actual cell in the row.

Please explain to me how to edit the value in the specific cell.

Hope this is more clear.

Thx!

0
Eduan Marais
Top achievements
Rank 2
answered on 20 May 2010, 06:19 AM
Hi.

I have seen an example on your documentation ( http://www.telerik.com/help/silverlight/gridview-managing-data-add-new-entries.html )
that explains how to enter data in a grid.

I have tried it, but still no data is displaying. I am starting to think that there is a property on the grid preventing me to see the data.
This is what I have tried:

 

GridViewCell cell = new GridViewCell();

 

cell.Content = 100;

 

 

GridViewRowItem item = new GridViewRowItem();

 

item.Cells.Add(cell);

 

 

 

this.grid.BeginInsert();

 

 

this.grid.Items.Add(item);

 

 

this.grid.CommitEdit();

Even after explicitly beginning and insert and commiting it afterwards, the data still does not display.
I have also noticed that I can not enter data into the grid once I am in runtime. The moment you exit the cell, it is cleared again. Makes me think that there are some extra properties that needs to be set for the cell to display its value.

Regards

 

 
0
Vlad
Telerik team
answered on 20 May 2010, 07:33 AM
Hi,

Can you post more info about what kind of benefits you expect from this approach (working with UI) versus working with collection of objects directly?

Kind regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Eduan Marais
Top achievements
Rank 2
answered on 20 May 2010, 07:46 AM
Well,
I am not really thinking of benefits.
I am receiving the data in a table that I can not bind directly to the grid, simply because it is not in the correct format. Each row coming from my dataset will represent 1 cell in the radgrid. So, I need to do some formatting on the idividual cells, based on the information received from the table - formatting like color and font size on cell level.

I have done a lot of research the last 48 hours, and it seems to me that WPF recommends databinding, rather than manually populating the grid.
So, if I do not get joy today, I will have to convert my data from the original table into a new datatable, bind the new datatable to the grid, and do the formatting afterwards.

What do you recommend?
0
Vlad
Telerik team
answered on 20 May 2010, 08:09 AM
Hello,

Generally the best approach will be MVVM. You can create a model where you can reformat the original data and bind the grid to some property of this model (new reformatted data). In this case you will have complete separation between the data and UI.

Kind regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Eduan Marais
Top achievements
Rank 2
answered on 20 May 2010, 08:16 AM
Hi.

Thanks.
I am not familiar with MVVM, but I will do some research on it.
Thanks for the feedback!

Eduan
0
Gene
Top achievements
Rank 1
answered on 25 Aug 2010, 08:15 PM
Eduan,

What approach did you end up taking on this?

Best regards,
Gene
0
Pavel Pavlov
Telerik team
answered on 26 Aug 2010, 06:36 AM
Hello Gene,

Since RadGridView is a databound control , all possible solutions require some kind of view - a  view model , or just a small colelction of items that should serve as  ItemsSource for RadGridView.

Feel free to share some more details on your business scenario and I will be glad to help you further .

Greetings,
Pavel Pavlov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Eduan Marais
Top achievements
Rank 2
Answers by
Maya
Telerik team
Eduan Marais
Top achievements
Rank 2
Vlad
Telerik team
Gene
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or