This is a migrated thread and some comments may be shown as answers.

Find data item in SelectedIndexChanged

2 Answers 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peichung
Top achievements
Rank 1
Peichung asked on 05 Aug 2009, 05:32 PM
I was trying to get the data item in SelectedIndexChanged.  Here is what I have:

    protected void Grid1_SelectedIndexChanged(object sender, System.EventArgs e)  
    {  
          
        GridItem item = Grid1.SelectedItems[0] as GridItem;  
        Customer cust = item.DataItem as Customer;  
    }  

where Customer is my data type for data binding.  The item object in the above codes is null.  Please help.  Thanks,

Peichung

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 06 Aug 2009, 06:05 AM
Hi Peichung,

I have read in the forums that the DataItem is null after post-back - this object is available only during data-binding. i.e when used in data bound context (inside the ItemDataBound handler of the grid). You may go through the following forum discussion for more details.
dataitem is null

Shinu
0
Peichung
Top achievements
Rank 1
answered on 06 Aug 2009, 01:54 PM
Hi Shinu,

Thank you for pointing this out.  I guess wasn't thinking clearly when I tried to get the DataItem.  It makes sense to not have DataItem available after data bind.  I access the grid item instead:


GridDataItem item = Grid1.SelectedItems[0] as GridDataItem;  
string s = item["CustomerName"].Text; 



Thank you.

Peichung
Tags
Grid
Asked by
Peichung
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Peichung
Top achievements
Rank 1
Share this question
or