473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Bind Generic Dictionary to GridView

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
6 29132
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of...
2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
5
by: Steven Baggs | last post by:
Hi!, I have defined a List<T> like this List<Container> Container class holds my Item object and it's state as a Enum. Container.Item Container.State I would like to bind it to GridView.
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
0
by: teclioness | last post by:
Hi, I am using gridview for the user to update rows. In a row, there sre two columns, which need to be updated. When the gridview is to be shown, the row should show the values from database....
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
3
by: Steve Kershaw | last post by:
Hi, I need a way to bind a DirectoryInfo array to a GridView without any errors. The code I'm using to create the DirectoryInfo array is: // Define the current directory DirectoryInfo dir...
2
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new...
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.