GridView RowCreated event when using ObjectDataSource...GridView.DataSource is Null...

I have used the RowCreated event in the past to create grouping in a standard GridView without issues. However, in all of these cases the DataSource I used for each GridView had always been a DataTable. When I use the same code against a GridView that has its DataSource as an ObjectDataSource (the ODS returns a DataTable) the code fails because the GridView.DataSource is NULL. Is this expected behaviour for a GridView that is attached to an ObjectDataSource or is there another way to access the original DataSource of a GridView in the RowCreated event?

DataTable example that has worked in the past...

Private Sub MyGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGrid.RowCreated

    Select Case e.Row.RowType

        Case DataControlRowType.DataRow

            If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
                Dim objGrid As GridView = CType(sender, GridView)

                **** This works when DataSource is a DataTable... ****
                Dim objSource As DataTable = CType(objGrid.DataSource, DataTable)

            End If

        End Select

End Sub

ObjectDataSource example that does not work...
DataTable is Null...

Private Sub MyGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGrid.RowCreated

    Select Case e.Row.RowType

        Case DataControlRowType.DataRow

            If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
                Dim objGrid As GridView = CType(sender, GridView)

                **** This instantiates to Null when ObjectDataSource is used that returns a DataTable... ****
                Dim objSource As DataTable = CType(objGrid.DataSource, DataTable)

               If objSource Is Nothing Then
                   **** This fires because objSource is in fact Null...****
                   Controls.Add(New LiteralControl("objSource is Null!"))
               End If

            End If

        End Select

End Sub

4/10/2009 3:21:05 PM 72751 articles. 3 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 9

Hi Bruce,

When you bind data by setting the GridView's DataSource property, then that property maintains a reference back to the original datasource, and you can reference it as you had previously.  When you bind data with any of the data source controls by setting the GridView's DataSourceID, then the GridView's DataSource property will always be null, so this is expected (for any of the data source controls, including the ODS).  There are a couple of ways around this:

1. You can get the original data by calling the ObjectDataSource's Select method.  Depending on how dynamic your data is, you could end up with inconsistent data if called during RowCreated.

2. You could programmatically set the GridView.DataSource = ObjectDataSoucre.Select() and call DataBind() (and not set the DataSourceID).

3. Here are a few options other options for this type of grouping:,guid,c093836d-8d97-4e5b-8a1c-8218742cb686.aspx

Hope that helps.


Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
4/10/2009 7:39:59 PM

Hi Aaron,

I have always assumed you must set the DataSourceID property when using an ObjectDataSource. Using GridView.DataSource = ObjectDataSource.Select() does retrieve my grid normally, and now I can access the DataSource once again in the RowCreated event. Paging functions normally, only now my column sorting now breaks...but this I can deal with...I think.

Thank you very much...this was a big help!


4/11/2009 12:51:38 AM

Similar Artilces:

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Converting a gridView using an Sql Datasource to a gridview who is not using an SQL DataSource
How can i rewrite this page so that i doesnt make use of an SqlDataSource?  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LoginHistory.ascx.cs" Inherits="Private_UserControls_WebUserControl" %> <html> <head> <%--<link rel="stylesheet" href="BaldwinPortal.css" mce_href="BaldwinPortal.css" type="text/css" />--%> <title>Login History</title> </head> <body style="margin:0 0 0 0;padding:0 0 0 0"> ...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

How to change the SQL used for the GridView, when using a control parameter in the datasource
I'm using a GridVeiw with and Access database. I want to allow users to select a date and then update the GridView to show only data entered after that date. So my question is, how do I modify the SQL used by the datagrid? I'm using a control parameter as part of my AccessDataSource and I think that is what I'm having trouble figuring out how to include. Here is my datasource, with the control parameter in the the GridView. The GridView works fine with these initial settings settings. <asp:AccessDataSource ID="AccessDataSourceForwView" runat="server" ConflictDetection="Compa...

ObjectDataSource and GridView to GridView
Hello, Have gridview (#1) with paging/sorting that is rendered via a ODS connected to a tableadapter reading a sproc in a Dataset (xsd) file. I like the minimal coding of that implementation. Need to, on user demand, grab a rendered page, however sorted, from #1 and place it on an empty gridview (#2) on another web page using, I would think, a session variable. This is not working. Session variable is not null in #2 but it is empty upon Databind. I have tried DataSource/DataTable/DataView. Is this even possible? My only purpose ...

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Gridview Sorting event. Datasource is always null ?? or issue casting datasource as datatable?
The Grid displays with multiple rows. I select to sort of a particular field and the below event is fired, but DataTable is Nothing so code is skipped.  Protected Sub ChargeGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)         Dim dataTable As DataTable = CType(ChargeGridView.DataSource, DataTable)         If Not (dataTable Is Nothing) Then   '****** (THIS IS ALWAYS FALSE) ****             Dim dataView As DataView = New DataView(dataTable)            ...

Gridview DataItem property null during GridView DataBound event?
Let me just start off by saying that I am familar with the normal DataItem = null during a postback when the grid is not being bound. My problem is a bit different.  I am actually in the DataBound event of the grid (meaning it was just bound) but the DataItem property is null.  Does this even make sense?  Here is a code snippet:protected void grdEffects_DataBound(object sender, EventArgs e) { GridView grdEffects = (GridView)sender; foreach(GridViewRow dgrow in grdEffects.Rows) { if(dgrow.RowType == DataControlRowType.Data...

Reterving gridview value without using gridview event
 i want to reterive gridview value without using the gridview eventsi it is possible  Ramesh.D  You will have to loop through all the rows of the gridview and get the value you wanted.Apurva Kaushal just loop through all the rows of gridview and use the findcontrol method to find the controls and their value ----sudo code--- for(int i=0;i<gridview1.items.count;i++){ gridview1.items[i].findcontrol("whatever control") orgridview1.items[i].cell["cellname or cellnumber"].value}Vikram www.vikramlakhotia.comPleas...

code for Custom paging without using sqldatasource control and sql datasource control for gridview...........
 code for Custom paging without using sqldatasource control and sql datasource control for gridview...........i need code for how to show all pagenumbers in gridview with custom paging in gridview..............It is very urgent................................. T.Ashok Kumar  suppose that we have a table like thisCountryId  identity intCountryTitle    nvarchar(50)you can use this sql command for paging the dataUSE [CyberOffice]GO/****** Object:  StoredProcedure [dbo].[CountryGetListPaged]    Script Date: 09/17/2008 14:01:06 ******/SET A...

Problem using two gridviews when using selected value of 1st to control datasource of 2nd
 I have a page with 2 gridviews. When you select a row in the first gridview, the DataKey of that row gets passed to the second gridview, which then displays a list of related records. I've read at that there can be issues around page lifecycle timing that can affect what I'm doing so I've tried to follow the instructions there. However, I'm still having problems.Here's the flow of what I want to happen:a) User clicks row in gridview1.b) Postback occurs.c) gridview1 is selected, gridview2 shows records relating...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Gridview control embedded in another gridview control
I am writing an ambulance crew scheduling application on the web and I need some help completing the last part.  I have an access database that has three tables: user table, shift table (describing the shifts available), and a schedule table (that stores shift by shift_id and user by user_id).  I have a gridview control that will display the available shifts but I cannot get the embedded gridview control that will display the user that is signed up for each shift to display because I cannot figure out how to use the OnRowDataBound method.  I need a simple VB example of how to...

Web resources about - GridView RowCreated event when using ObjectDataSource...GridView.DataSource is Null... -

Forums Forums Database Error
This is a discussion forum powered by vBulletin. To find out about vBulletin, go to .

Using The LINQDataSource
-*+There is a new datasource control available in Visual Studio that works in much the same way as the SQLDataSource and the ObjectDataSource. ...

ASP.NET Web Forms DynamicData FieldTemplates for DbGeography Spatial Types
... a FormView in ASP.NET Web Forms. Notice the ItemType is set, rather than using Eval(). We're also using SelectMethod rather than an ObjectDataSource. ...

LINQ Tutorials : LINQ Tutorials, Articles and Resources
Articles and Helpful Tutorials for LINQ and ASP.NET

About Mikesdotnetting
I started this site as somewhere to store answers to bits and pieces that I see asked frequently in the forums at . Rather than type ...

Runnable code for JQuery, Codeigniter, NodeJS, PHP, Python and more - Runnable
Runnable code for dart-vm, express, paypal, google, evernote, everyauth, ejs, rendr, CakePHP, php-mysql, php-facebook, codeigniter, imagemagick, ...

ASP.NET Pivot Table - RadPivotGrid for ASP.NET AJAX
... of all data reports. Try now! Use any declarative data source control (AccessDataSource, SqlDataSource, LinqDataSource, EntityDataSource, ObjectDataSource, ...

Miscellaneous « AJ\’s blog
AJ\’s blog January 28, 2012 2011 in review Filed under: Miscellaneous — ajdotnet @ 7:41 pm The stats helper monkeys prepared a ...

ObjectDataSource - Selectcount method with custom parameters and custom paging.
protected void obds_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) { if (!e.ExecutingSelectCount) { e.InputParameters["filter"] ...

Archive - Forever Breathes The Lonely Word

Resources last updated: 12/22/2015 10:18:52 AM