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