473,405 Members | 2,262 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,405 software developers and data experts.

DataGridItem.DataItem property?

The help pages make it look like the DataGridItem.DataItem property would
return a row from the bound table. Like so:

//(during setup)
myGrid.DataSource = myDataset.MyTable;
myGrid.DataBind();

//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;
But this doesn't work! The DataItem property always returns null (so the
cast does also). Am I using this correctly? Is there a different way to
retrieve the DataSet row that was bound to the Grid row?

-Joel

Nov 18 '05 #1
2 6132
Joel, the DataItem is only not-null during the visit when you call the
DataBind() method. I am assuming when the SelectedIndexChanged event
handler is running, it's on a postback, and you are only binding the
data to the grid on the first page load, no? This is why DataItem is
null in the SelectedIndexChanged event handler.

Typically what is done is the DataKeyField property is set to the
primary key field of the data being displayed in the grid. Then, in the
SelectedIndexChanged event handler, you could get the primary key field,
then hit the database and get the detailed information about this row.

hth
Joel Lyons wrote:
The help pages make it look like the DataGridItem.DataItem property would
return a row from the bound table. Like so:

//(during setup)
myGrid.DataSource = myDataset.MyTable;
myGrid.DataBind();

//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;
But this doesn't work! The DataItem property always returns null (so the
cast does also). Am I using this correctly? Is there a different way to
retrieve the DataSet row that was bound to the Grid row?

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
Nov 18 '05 #2
Interesting. Thanks Scott!
-Joel

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:Nm**************@newssvr13.news.prodigy.com.. .
Joel, the DataItem is only not-null during the visit when you call the
DataBind() method. I am assuming when the SelectedIndexChanged event
handler is running, it's on a postback, and you are only binding the data
to the grid on the first page load, no? This is why DataItem is null in
the SelectedIndexChanged event handler.

Typically what is done is the DataKeyField property is set to the primary
key field of the data being displayed in the grid. Then, in the
SelectedIndexChanged event handler, you could get the primary key field,
then hit the database and get the detailed information about this row.

hth
Joel Lyons wrote:
The help pages make it look like the DataGridItem.DataItem property would
return a row from the bound table. Like so:

//(during setup)
myGrid.DataSource = myDataset.MyTable;
myGrid.DataBind();

//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;
But this doesn't work! The DataItem property always returns null (so the
cast does also). Am I using this correctly? Is there a different way to
retrieve the DataSet row that was bound to the Grid row?

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Lee | last post by:
Hi all, I have the following problem and I cannot solve it. If anyone can help me solve this problem. I use the following code <%#DataBinder.Eval(Container.DataItem, "Property")%> to display...
2
by: ric carrasquilla | last post by:
first, thx to martin for helping me with my initial post. im a newbie and appreciate the help. if someone can help me with another question, please do. i have a repeater pulling job...
4
by: Souri Challa | last post by:
Hello All, When a datagrid in a web form is getting re populated from view state on post back, it is firing the datagrid Item Created event but the DataGridItem(e.Item.DataItem is null) in the...
2
by: JohnZing | last post by:
Hi, Should I use <%# Container.DataItem("Author")%> or <%# DataBinder.Eval(Container.DataItem,"Author") %> What's the difference between both? Thank You
3
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
15
by: Laser Lu | last post by:
Hi, all, I was tangled by the DataGridItem.DataItem property. I tried to invoke that property to get and display the contents that was bound to the currently selected row. However, an exception...
1
by: Art | last post by:
I'm subscribing to the RowCreated event during GridView render process to modify properties of individual rows. All works great when there is no paging involved but once I specify that I'd like to...
4
by: adiel_g | last post by:
I am trying to loop through a repeater to retrieve a dataitem field but am getting a NullReferenceException. I can find a checkbox control but cannot find a dataitem field. Here is the code that...
1
by: Bresco | last post by:
I'm using ObjectDatasource in combination with a Formview. The ObjectDatasource is connected to a FormView. I have a business object property which I can't update through the normal binding...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.