|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bind Generic Dictionary to GridViewIs 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 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[MCSD.NET] wrote:
Show quoteHide quote > it should work. code below do exactly this. you must have just empty Thank you for your response. That's close to what I have. My TValue is > 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(); > > } 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 I'm guessing the grid doesn't know how to format the object. Does your
object have ToString() defined? Fao, Sean wrote: Show quoteHide quote > 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, > Gary Holbrook wrote:
> I'm guessing the grid doesn't know how to format the object. Does your I tried that, too. The DropDownList control provides the DataTextField > object have ToString() defined? 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 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 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 Show quoteHide quote "Fao, Sean" <enceladus***@yahoo.comI-WANT-NO-SPAM> wrote in message [attached file: TestWeb.zip]news:%23Hi90A3eGHA.4276@TK2MSFTNGP03.phx.gbl... > 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 Galcho[MCSD.NET] wrote:
> this is how you have to do it: That's it, exactly. Thank you very much!> 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> -- Sean
Validate XML with XSD - Tearing my hair out
HtmlEditor update Suggestions Please !!! Inter-thread message queue in C# How do I compile a C# program? Enable TrackBar DoubleClick event C# Clipboard with Reflection question FireFox cache XSLT - how to use it What is the best way of adding various user Controls? [Win #C] |
|||||||||||||||||||||||