Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 9th, 2004, 10:57 AM
Registered User
Join Date: Jul 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Casting DataGridItem.DataItem to Object


I'm binding an array of objects to a datagrid. I then want to select a few of these items before proceeding to the next page.

My problem is that I can't cast the DataGridItem.DataItem from the datagrind into a custom type.

Here's the code:

// this is the datagrid
protected System.Web.UI.WebControls.DataGrid CollNoteResults;

// here's the array of my objects - CollNoteStatements
// inherits from ArrayList...
CollNoteStatements cnss = new CollNoteStatements();

// here's the databind
this.CollNoteResults.DataSource = cnss;

// here's the DataGridItem - it's in a loop with index i
DataGridItem dgi = (DataGridItem) CollNoteResults.Items[i];

// here's me trying to cast the DataItem out as a type
// this is where it starts to go wrong
CollNoteStatement cns = dgi.DataItem as CollNoteStatement;

Although there's no build error, the variable cns is simply not set (or set to null).

Please help someone?

Old July 9th, 2004, 11:07 AM
Friend of Wrox
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000

all i know about a datagrid is it can bounded to DataTable Else I Don't think
Why Suffering
So Create Datasource as table and fill it and bind it to datagid ...That's all

Ahmed Ali
Software Developer
Old July 12th, 2004, 02:43 AM
Registered User
Join Date: Jul 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks Ahmed, although all I really want to do is retrieve an object from an object-bound datagrid. So your proposal of binding it into a datatable is not helpful in this instance. But thanks for replying.

Similar Threads
Thread Thread Starter Forum Replies Last Post
<%#Container.DataItem%> SKhna ASP.NET 2.0 Basics 1 March 27th, 2008 02:58 AM
navigate dataitem in datagrid mvenkatesan VS.NET 2002/2003 0 May 24th, 2007 01:52 AM
Extending DataGridItem Phrozen ASP.NET 2.0 Professional 0 March 11th, 2007 09:17 AM
Casting System::Object __gc * to System.Double[] nepsat Visual C++ 3 November 19th, 2005 12:51 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.