Home All Groups Group Topic Archive Search About

Bind Generic Dictionary to GridView

Author
19 May 2006 5:48 PM
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,

--
Sean

Author
19 May 2006 6:04 PM
Galcho[MCSD.NET]
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
Are all your drivers up to date? click for free checkup

Author
19 May 2006 6:15 PM
Fao, Sean
Galcho[MCSD.NET] wrote:
Show quoteHide quote
> 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
Author
19 May 2006 7:07 PM
Gary Holbrook
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,
>
Author
19 May 2006 7:17 PM
Fao, Sean
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
Author
19 May 2006 7:21 PM
Galcho[MCSD.NET]
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
Author
19 May 2006 7:17 PM
Galcho[MCSD.NET]
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
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

[attached file: TestWeb.zip]
Author
19 May 2006 7:55 PM
Fao, Sean
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



Post Thread options