By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,337 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,337 IT Pros & Developers. It's quick & easy.

Bind Generic Dictionary to GridView

P: n/a
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,

--
Sean
May 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

May 19 '06 #2

P: n/a
Galcho[MCSD.NET] wrote:
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,

--
Sean
May 19 '06 #3

P: n/a
I'm guessing the grid doesn't know how to format the object. Does your
object have ToString() defined?

Fao, Sean wrote:
Galcho[MCSD.NET] wrote:
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,

May 19 '06 #4

P: n/a
Gary Holbrook wrote:
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.

--
Sean
May 19 '06 #5

P: n/a
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

May 19 '06 #6

P: n/a
Galcho[MCSD.NET] wrote:
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!

--
Sean
May 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.