Bind Generic Dictionary to GridView

  • Thread starter Thread starter Fao, Sean
  • Start date Start date
F

Fao, Sean

Is it possible to bind a GridView to a generic Dictionary object? When
I try it in my ASP.NET application, it throws an exception acknowledging
that the specified field or property does not exist. The problem, I
think, is that the GridView cannot see "inside" of the generic
Dictionary. I suspect this because setting the AutoGenerateColumns
property of the GridView causes the GridView to display only the Key
property, which is, of course, part of the Dictionary object.

Can I bind a GridView to a generic Dictionary or will I need to store my
objects in something else like an ArrayList?

Thank you in advance,
 
it should work. code below do exactly this. you must have just empty
webpage with GridView object called GridView1


Dictionary<string, int> col;
protected void Page_Load(object sender, EventArgs e)
{
col = new Dictionary<string, int>();
col.Add("key 1", 1);
col.Add("key 2", 2);
col.Add("key 3", 3);
col.Add("key 4", 4);
col.Add("key 5", 5);

GridView1.DataSource = col;
GridView1.DataBind();

}

I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com
 
Galcho said:
it should work. code below do exactly this. you must have just empty
webpage with GridView object called GridView1


Dictionary<string, int> col;
protected void Page_Load(object sender, EventArgs e)
{
col = new Dictionary<string, int>();
col.Add("key 1", 1);
col.Add("key 2", 2);
col.Add("key 3", 3);
col.Add("key 4", 4);
col.Add("key 5", 5);

GridView1.DataSource = col;
GridView1.DataBind();

}

Thank you for your response. That's close to what I have. My TValue is
a custom class and it's the properties of this custom class that I am
having trouble accessing. My collection is more along the lines of:

Dictionary<string, MyObject> col;
protected void Page_Load(object sender, EventArgs e)
{
col = new Dictionary<string, int>();
col.Add("key 1", new MyObject(param1, param2));
col.Add("key 2", new MyObject(param1, param2));
col.Add("key 3", new MyObject(param1, param2));
col.Add("key 4", new MyObject(param1, param2));
col.Add("key 5", new MyObject(param1, param2));

GridView1.DataSource = col;
GridView1.DataBind();
}


I can't seem to see the public properties of MyObject through the GridView.

Does that make sense?

Thank you again,
 
I'm guessing the grid doesn't know how to format the object. Does your
object have ToString() defined?
 
Gary said:
I'm guessing the grid doesn't know how to format the object. Does your
object have ToString() defined?

I tried that, too. The DropDownList control provides the DataTextField
and DataValueField properties, which let you specific where the key and
values are in a DataSet. I was hoping that the GridView control would
provide similar functionality so that I can specify which properties
contain the Key's and Value's. Unfortunately, I haven't been able to
implement such functionality into a GridView.
 
this is how you have to do it:
it is done by code in .aspx page
unfotunately you have to do it for all fields in custom object

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# ((MyObject)Eval("value")).Name %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


See attached file


I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com
 
Galcho said:
this is how you have to do it:
it is done by code in .aspx page
unfotunately you have to do it for all fields in custom object

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# ((MyObject)Eval("value")).Name %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

That's it, exactly. Thank you very much!
 
Back
Top