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

List of Hashtable or dictionary

3 Answers 220 Views
Grid
This is a migrated thread and some comments may be shown as answers.
M
Top achievements
Rank 1
M asked on 06 Mar 2011, 07:51 PM
Hi!

I am building a custom list on the fly where I build a Hashtable (it could also be a dictionary if needed... it does not matter to me). Then, I insert the hashtables in a list and want to bind the list in a RadGrid.  The problem is that every field of each row is blank (empty). The row count is good which means to me that something is kinda happening but I'm not doing everything correctly!

I've seen another example on the forum where a user was doing a similar thing through a webservice but even when downloading the code, I could not make it work. So here is what I'm doing :

My ASPX :
<telerik:RadGrid ID="gridList" AutoGenerateColumns="False" runat="server">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn
                 HeaderText="Ticker" DataField="SecurityTicker" >
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

My code behind :
Private Sub gridList_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gridList.NeedDataSource
 
    Dim myList As New List(Of Hashtable)
 
    Dim h1 As New Hashtable
    h1.Add("SecurityTicker", "MAX")
    h1.Add("SecPrice", 99)
 
    Dim h2 As New Hashtable
    h2.Add("SecurityTicker", "ZZZ")
    h2.Add("SecPrice", 100)
 
    myList.Add(h1)
    myList.Add(h2)
 
    gridList.DataSource = myList
 
End Sub

What is missing to make that work ?

Thank you!

3 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 08 Mar 2011, 05:14 PM
Hello M,

You cannot bind the grid directly to a list of dictionaries, but you can implement a class that holds each dictionary item and bind the grid to list of objects of your class.
Here is an example how to do this:

ASPX:
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="False" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
  <MasterTableView>
    <Columns>
      <telerik:GridBoundColumn HeaderText="Field1" DataField="Field1">
      </telerik:GridBoundColumn>
      <telerik:GridBoundColumn HeaderText="Field2" DataField="Field2">
      </telerik:GridBoundColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    List<MyProvider> list = new List<MyProvider>();
 
    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict.Add("field1", "value for field1 on the first row");
    dict.Add("field2", "value for field2 on the first row");
    list.Add(new MyProvider(dict));
    dict = new Dictionary<string, string>();
    dict.Add("field1", "value for field1 on the second row");
    dict.Add("field2", "value for field2 on the second row");
    list.Add(new MyProvider(dict));
    RadGrid1.DataSource = list;
}
 
public class MyProvider
{
    Dictionary<string, string> element;
    public MyProvider(Dictionary<string, string> element)
    {
        this.element = element;
    }
 
    public string Field1
    {
        get
        {
            return element["field1"];
        }
        set
        {
            element["field1"] = value;
        }
    }
    public string Field2
    {
        get
        {
            return element["field2"];
        }
        set
        {
            element["field2"] = value;
        }
 
    }
}

VB:
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
    Dim list As New List(Of MyProvider)()
 
    Dim dict As New Dictionary(Of String, String)()
    dict.Add("field1", "value for field1 on the first row")
    dict.Add("field2", "value for field2 on the first row")
    list.Add(New MyProvider(dict))
    dict = New Dictionary(Of String, String)()
    dict.Add("field1", "value for field1 on the second row")
    dict.Add("field2", "value for field2 on the second row")
    list.Add(New MyProvider(dict))
    RadGrid1.DataSource = list
End Sub
 
Public Class MyProvider
    Private element As Dictionary(Of String, String)
    Public Sub New(element As Dictionary(Of String, String))
        Me.element = element
    End Sub
 
    Public Property Field1() As String
        Get
            Return element("field1")
        End Get
        Set
            element("field1") = value
        End Set
    End Property
    Public Property Field2() As String
        Get
            Return element("field2")
        End Get
        Set
            element("field2") = value
        End Set
    End Property
 
End Class

Greetings,
Vasil
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
M
Top achievements
Rank 1
answered on 09 Mar 2011, 04:31 PM
Well... I was finally able to do it with a gridTemplateColumn.

Thanks.
0
Matt
Top achievements
Rank 1
answered on 11 Apr 2018, 06:21 PM
This would require that the backend have knowledge of the fields that need to be populated.  This is never a good idea to couple the UI with the backend.  Passing it through the backend directly to the UI is the best option.
Tags
Grid
Asked by
M
Top achievements
Rank 1
Answers by
Vasil
Telerik team
M
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Share this question
or