ElektroStudios
ElektroStudios

Reputation: 20494

Telerik RAD Controls, how to set a List(Of GridViewRowInfo) as a RadGridView DataSource?

I'm looking for a C# or VB.NET solution for this.


UPDATE:

I have a RadGridView with 5 manualy defined columns by me: enter image description here

AutoGenerateColumns property is set to False.

When I instace a GridViewRowInfo class I can set a lot of properties for this object:

Dim MyRow As New GridViewRowInfo(Me.RadGridView1.MasterView)
With MyRow
    .Cells(0).Value = "My Value for Column 1"
    .Cells(1).Value = "My Value for Column 2"
    .Cells(2).Value = "My Value for Column 3"
    .Cells(3).Value = "My Value for Column 4"
    .Cells(4).Value = "My Value for Column 5"
    .Height = 50
    .Tag = New Object
    .IsSelected = True
End With

And when I add that row the properties that I've previously set for that row takes effect inmediately:

RadGridView1.Rows.Add(MyRow)

enter image description here

If I want to add a collection of those rows just I can set a new collection of GridViewRowInfo that implements the IList interface:

Dim MyRows As New List(Of GridViewRowInfo)
MyRows.Add(MyRow1)
MyRows.Add(MyRow2)
MyRows.Add(MyRow3)

RadGridView1.Rows.AddRange(MyRows.ToArray)

Well, so my intention is to set a collection of those rows as DataSource, for example:

RadGridView1.DataSource = MyRows

So the first thing to notice is that I've set a collection of GridViewRowInfo and I've set different properties for each GridViewRowInfo that should take effect when adding the datasource-collection, the second thing is that if I update the datasource-collection to remove or add more rows then the RadGridView control should perform the updates automatically without more intervention ...not?

The problem is that any of those things happens:

enter image description here

As you could see in the image above, when I set a List(of GridViewRowInfo) as my DataSource, it only adds empty rows, and if I previously have set for example the Height property of one of the GridViewRowInfo inside it does not take effect when setting the Datasource:

I would like to perform this in the more direct way and the less extravagant way, I mean i'm not looking for create a custom class to be able to set that class as DataSource, and reproducing all the properties that exposes the GridViewRowInfo class or something so tricky in my custom class, 'cause If the RadGridView exposes a good GridViewRowInfo class with all that I need why I should consider to create a custom class to set it aa my DataSource?.

If I don't have a good idea or a missunderstanding of these concepts please clarify me them, I know that the usage of the datasource should not be used in that way (or I think so) but I really would like to do it to simplify the things even more to work directly with the datasource (and each row property) instead the control itself.


Also I've tried the oficial example in this link (but just using a list(Of String) instead), but it just adds a new column in my gridview named 'Length' (with a numeric data) in that column cell.

Upvotes: 1

Views: 1536

Answers (2)

If I don't have a good idea or a missunderstanding of these concepts please clarify them

You are confusing WHAT to display with HOW to display them which are 2 very different things. From their online guide: The RadGridView supports the standard Windows Forms data binding model which is to say the datasource provides data for the control, not presentation information.

Public Class MyObject
   Public Property MyInt() As Integer
   Public Property MyString() As String
...

Dim myList As New List(Of MyObject)()
myList.Add(New MyObject(1, "Outdoor"))
myList.Add(New MyObject(2, "Hardware"))
myList.Add(New MyObject(3, "Tools"))
myList.Add(New MyObject(4, "Books"))
myList.Add(New MyObject(5, "Appliances"))
RadGridView1.DataSource = myList

Result:

enter image description here

Note how the control automatically picks up the Property Names from the custom class: MyInt and MyString become column names. That is the only layout (HOW) related thing the grid picks up from the DataSource, and thats only when AutoGenerateColumns is True (otherwise they would be blank). When False, you control it by laying out the columns.

Now look at what you are attempting:

With MyRow
    .Cells(0).Value = "My Value for Column 1"
    .Cells(1).Value = "My Value for Column 2"
     ...
    .Height = 50
    .Tag = New Object
    .IsSelected = True

There are many problems with this as a DataSource. Mainly, you are not supplying data for cells, but data in cells for cells. Your approach sort of mixes the unbound approach (explicitly specify what is to go into each cell), with binding to a datasource.

But, how is it supposed to know Value is a data display item? How is it supposed to know to drill into the cell collection to find Value? How is it supposed to know that Value is data but Tag is not? How is it supposed to know not to try to display Height as column data? Just because the name matches a control's property name?

What would happen if myObject by chance had a Height property related to my class? You want the control to interpret it as control layout information, but the rest of the world would want the patient's height, or building height or geo formation height to show as data. We would have to design classes with unique names not found in the control in order to get our data to show and prevent the control presentation from whimsically changing based on data and property names in the DataSource.

The GridviewRowInfo as the name suggests provides row information, not row data. Extensive presentation information is present in your List, but it doesnt work like you want because controls do not use the datasource to get presentation infomation.


The control does include Four ways to customize RadGridView appearance: Themes, UI editor, Events and Conditional Formatting.

There other elements like GridTableElement and GridTableBodyElement. Actually these elements contain some properties that control behavior and appearance of grid cells and rows, like GridTableElement.RowHeight, RowSpacing and so on.

tl;dr

Data passed in a DataSource, does not affect the presentation (aside from Column names with AutoGenerateColumns, but that is a function of that property, not the datasource.)

Upvotes: 1

checho
checho

Reputation: 3120

RadGridView supports two ways of populating with data:

So, you cannot bind the grid to collection of GridViewRowInfos (as you will only see the GridViewRowInfo type properties). You should either continue manually adding the as you are right now, or you can populate a DataTable for example with your data and set it as DataSource for the grid.

*All of the links and information are from the Telerik UI for WinForms documentation

Upvotes: 2

Related Questions