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

Discussion in 'ASP .Net' started by Christian Hvid, Nov 29, 2004.

  1. 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
     
    Christian Hvid, Nov 29, 2004
    #1
    1. Advertising

  2. Christian Hvid

    Karl Seguin Guest

    BlogEntry entry = ((BlogEntry)e.Item.DataItem);

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


    "Christian Hvid" <> wrote in message
    news:%...
    > 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
    >
    >
    >
     
    Karl Seguin, Nov 29, 2004
    #2
    1. Advertising

  3. Christian Hvid

    Scott Allen Guest

    On Mon, 29 Nov 2004 16:23:01 +0100, "Christian Hvid"
    <> wrote:

    >
    >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).

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/
     
    Scott Allen, Nov 29, 2004
    #3
  4. Christian Hvid

    aa7im Guest

    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();
     
    aa7im, Nov 29, 2004
    #4
  5. 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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:<>...
    > BlogEntry entry = ((BlogEntry)e.Item.DataItem);
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Christian Hvid" <> wrote in message
    > news:%...
    > > 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
    > >
    > >
    > >
     
    Christian Hvid, Dec 1, 2004
    #5
  6. Christian Hvid

    aa7im Guest

    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?
     
    aa7im, Dec 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bart
    Replies:
    0
    Views:
    352
  2. sangmin.park

    receiving calendar event by javascript

    sangmin.park, Feb 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    sangmin.park
    Feb 28, 2006
  3. mahesh
    Replies:
    1
    Views:
    375
    Greg R. Broderick
    Nov 2, 2007
  4. debartsa

    Composite Control not receiving DataGrid (child control) events?

    debartsa, Apr 19, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    186
    debartsa
    Apr 19, 2004
  5. Replies:
    1
    Views:
    135
Loading...

Share This Page