Getting the "row object" from a DataGrid when receiving a command event

C

Christian Hvid

What is the easiest way to get the "row object" or "item object" when a
datagrid is clicked?

I have web form with a datagrid. And I have an array of something called
BlogEntry that I bind to the grid like this:

private void Page_Load(object sender, System.EventArgs e)
{
BlogEntry[] entries = DataLayer.GetAllBlogEntries();
DataGrid1.DataSource = entries;
DataGrid1.DataBind();
}

BlogEntry looks like:

public class BlogEntry
{
...
public int ID { get { return id; } }
public DateTime Date { get { return date; } }
public String Text { get { return text; } }
public String Title { get { return title; } }
}

It all works nicely.

I insert a ButtonColumn with a command named delete and tie in a method
which looks like this:

private void DeleteItem(object source, DataGridCommandEventArgs e)
{
...
}

In the method I can read the title, date and other properties of the row by
using:

e.Item.Cells[0] (the value of the ID property)
e.Item.Cells[1] (the value of the Date property)
e.Item.Cells[2] (the value of the Text property)
....

But how do I get the BlogEntry object?

Obviously I can write entries[e.Item.ItemIndex] and store entries (of the
Page_Load method) in a session variable. But that is very cumbersome and I
hope that there is another way ...

-- Christian
http://vredungmand.dk
 
S

Scott Allen

But how do I get the BlogEntry object?

Obviously I can write entries[e.Item.ItemIndex] and store entries (of the
Page_Load method) in a session variable. But that is very cumbersome and I
hope that there is another way ...

It is cumbersome, but in a web application you'll either have to keep
the entries hanging around between the client's requests (like in a
session variable), or go back and retrieve them from the source.

The drawbacks to keeping them around are that you'll be consuming
memory with blog entries that might never be needed again if the
client has closen her browser and taken a stroll to the local pannini
shop for a bite to eat.

On the other hand, you can always retrieve the entries from the data
source again - but in some applications there is no guarantee the
entries will still be around. Someone else might have deleted the blog
entry that was clicked on. (You can also look at this as an advantage,
in that the the client isn't fooled into working with stale data).
 
A

aa7im

Typically you would have another method in your object modal that would
return a single entity, something like:

BlogEntry entry = DataLayer.GetBlyEntryByID(274);

In the datagrid events you just need to reload the singe entity your
working with from the DB and perform your actions. Set the
DataKeyField of the datagrid to the unique identifier field of your
BlogEntry object, something like:

<asp:datagrid id="dgMyGrid" runat="server" DataKeyField="BlogEntryID"
.........>

Then in the code-behind for the fired event you can access the unquire
id by using:

int entryID = Convert.ToInt32(dgMyGrid.DataKeys[e.Item.ItemIndex]);
BlogEntry entry = DataLayer.GetBlogEntryByID(entryID);
entry.Name = "New Name";
entry.Save();
this.RebindGrid();
 
C

Christian Hvid

That does not work - it is always null and I can can't figure out why :-(

-- Christian
(use the contact information at my homepage http://vredungmand.dk)

Karl Seguin said:
BlogEntry entry = ((BlogEntry)e.Item.DataItem);

--
MY ASP.Net tutorials
http://www.openmymind.net/


Christian Hvid said:
What is the easiest way to get the "row object" or "item object" when a
datagrid is clicked?

I have web form with a datagrid. And I have an array of something called
BlogEntry that I bind to the grid like this:

private void Page_Load(object sender, System.EventArgs e)
{
BlogEntry[] entries = DataLayer.GetAllBlogEntries();
DataGrid1.DataSource = entries;
DataGrid1.DataBind();
}

BlogEntry looks like:

public class BlogEntry
{
...
public int ID { get { return id; } }
public DateTime Date { get { return date; } }
public String Text { get { return text; } }
public String Title { get { return title; } }
}

It all works nicely.

I insert a ButtonColumn with a command named delete and tie in a method
which looks like this:

private void DeleteItem(object source, DataGridCommandEventArgs e)
{
...
}

In the method I can read the title, date and other properties of the row by
using:

e.Item.Cells[0] (the value of the ID property)
e.Item.Cells[1] (the value of the Date property)
e.Item.Cells[2] (the value of the Text property)
...

But how do I get the BlogEntry object?

Obviously I can write entries[e.Item.ItemIndex] and store entries (of the
Page_Load method) in a session variable. But that is very cumbersome and I
hope that there is another way ...

-- Christian
http://vredungmand.dk
 
A

aa7im

e.Item.DataItem is only available at the time the DataGrid is building
itself. Such as in the ItemOnDataBound event....
All other times DataItem is null...

This makes sense, otherwise the datagrid would have to cache your
complete object for
every row in the datagrid in viewstate for it to be available in
postbacks...

Did you read my previous suggestion?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top