Gridview SelectedIndexChanged Showing no Data


It could happen if we are dynamically creating DataTable and bounding it to our GridView. Searching for it if found the following information on one of the blog

 

The GridView (and actually, all our data controls) does not save data items across postbacks.  This reduces ViewState (if the objects are even serializable) and enables garbage collection to happen and clean up your objects.  So, when you click the button to post back, the GridView has not called DataBind and therefore your data item isn’t there.  This is what you’ve discovered.

Selection is handled slightly differently than choosing an edit item, sorting, paging, or other functions: because those other functions require a change in the control tree or a query to the data source to get different data, they cause a DataBind to happen automatically at some point before the control renders again.  You can handle RowDataBound and get your data item.  Selection, however, doesn’t require new data or a different control tree (all that usually changes is the style properties on the row), so it will never call DataBind.  This makes selection very fast.  It also means you won’t get a DataBound or RowDataBound event unless you call DataBind yourself.  So you can’t use that event to get your data item unless you call it yourself.  If you need the data item, you can call DataBind yourself directly to get it, then handle OnRowDataBound, check the row index against the SelectedIndex, and get the data item.

However, this is kind of the shotgun method- it will get you what you want, but the performance has decreased because you’ve called DataBind again.  Also, if the data in the data store has been modified between your calls to databind, the row index that was the selected item may now point to a different row or just different data.  What data are you really looking for from the selected data item?  Is it just one or two fields?  If so, consider using DataKeyNames to store these fields, and then you can get to them via the DataKeys property.  If you don’t want to suffer the hit to ViewState from DataKeyNames, consider getting the data directly from the controls in the row of the GridView.

So I followed the suggestion highlighted in yellow and was able to retrieve the value for the selected row.

 

  protected void grdPropertyValues_SelectedIndexChanged(object sender, EventArgs e)

    {       

        GridViewRow row = grdPropertyValues.SelectedRow;

        // Get the selected row index

        iRownum = grdPropertyValues.SelectedIndex;

        // call the method that dynamically creates the DataTable and

        // bind the gridview to it

        BindConferenceDayToGrid(myList, grdPropertyValues);

    }

 

 

protected void grdPropertyValues_RowDataBound1(object sender, GridViewRowEventArgs e)

    {

        // getting the GridViewRow

        GridViewRow row = e.Row;

        // if RowType is DataRow and RowIndex is our selected row’s index

        if (e.Row.RowType == DataControlRowType.DataRow && row.RowIndex == iRownum)

        {       

                DataRowView drv = (DataRowView)e.Row.DataItem;

                Response.Write(“The value we need “+drv.Row[1].ToString());          

        }      

    }

 

That’s it ..

 

 

 

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

7 thoughts on “Gridview SelectedIndexChanged Showing no Data”

  1. Great site this nishantrana.wordpress.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor 🙂

    Like

  2. nice site this nishantrana.wordpress.com terrific to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor 🙂

    Like

  3. Dear Sir,
    I faced one issue,using populatingGridview after selectedIndexChanged event.
    ie if the first column in gridview is checkbox and checked any checkbox in the girdview ,the check box is unchecked after refreshing.
    what is needed is even after refreshing the gridview the checked checkbox should not be unchecked. or else should have alternative for populating the gridview which would not refresh all the checked checkboxes.
    Expecting your reply.

    Like

  4. Dear Sir,
    continuation of previous post,
    The checkboxes in first column could be used for deleting the rows.
    finding all the checked checkboxes and pressing delete button would delete all the checked rows!!.
    Thanks

    Like

  5. I am using an asp:GridView to display data in a table.
    In the code behind, I am using OleDbConnection to connect to the DB and execute the select statement. The result set is being read into OleDbDataReader. I am binding the same to the GridView by doing something like this:
    newGrid.DataSource = reader; //reader is OleDbDataReader populated by executing select stmt
    newGrid.DataBind();

    The GridView definition in the aspx page looks like this:

    But I am not getting rowselectedindexchanged event at all. I have defined the function newGrid_SelectedIndexChanged in the code behind and put a breakpoint in the same. But its not calling the function.

    Need help on this asap.

    Like

Leave a Reply to Chaitanya Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

dreaming to be a clean coder and TDD minded programmer.

%d bloggers like this: