Deleting a row in a datagrid

Discussion in 'ASP .Net Datagrid Control' started by Lisa, Aug 13, 2004.

  1. Lisa

    Lisa Guest

    Hey all,
    I've spent an hour reading posts to see how to delete a row in a
    datagrid but none of them work for me (most are VB.NET and I'm coding
    in C#). Specifically, I have a checkbox next to each row and a
    "Delete" button at the bottom. What I want is for each row that is
    checked to get deleted on the click event.... this is what I got..

    <asp:datagrid id="grdUpload" runat="server" DataKeyField="UploadID"
    AutoGenerateColumns="false" width="424px">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox id="delete" runat="server"></asp:CheckBox>
    <asp:HyperLink target=_blank id="hlUploadLink" NavigateUrl='<%#
    DataBinder.Eval(Container.DataItem, "URL") %>' Runat="server"
    CssClass="NormalBold">
    <%# DataBinder.Eval(Container.DataItem, "File") %>
    </asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    <asp:Button id="btnDelete" runat="server" Text="Delete"></asp:Button>

    What do I put in the code behind private void btnDelete_Click to
    delete the record selected?

    Thanks in advance!
    Lisa
    Lisa, Aug 13, 2004
    #1
    1. Advertising

  2. I'll take a stab at this since nobody else is stepping up. Granted it is not
    pretty but it works.

    The way you have your elements setup you essentially have a DataGrid with
    one cell that has two controls in it. Well really it has 5 controls but we
    are only concerned with the two... the checkbox and the hyperlink.

    Your DataKeyField is accessiable through the grid's DataKeys property.
    Given all this we can get to all the data in the Button Click event.

    Again this is my first attempt so there may be better ways.

    // go through all the items in the grid
    for (int i = 0; i < grdUpload.Items.Count; i++)
    {
    // your checkbox is at index 1 in the controls collection
    // the hyperlink is at index 4
    CheckBox chkBox = (CheckBox)grdUpload.Items.Cells[0].Controls[1];
    if (chkBox.Checked)
    {
    int key = (int)grdUpload.DataKeys;
    // do whatever you need to do here
    }
    }

    HTH,

    --
    Addison Ritchie, MCSD.NET
    Ritch Consulting, Inc.

    "Lisa" wrote:

    > Hey all,
    > I've spent an hour reading posts to see how to delete a row in a
    > datagrid but none of them work for me (most are VB.NET and I'm coding
    > in C#). Specifically, I have a checkbox next to each row and a
    > "Delete" button at the bottom. What I want is for each row that is
    > checked to get deleted on the click event.... this is what I got..
    >
    > <asp:datagrid id="grdUpload" runat="server" DataKeyField="UploadID"
    > AutoGenerateColumns="false" width="424px">
    > <Columns>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:CheckBox id="delete" runat="server"></asp:CheckBox>
    > <asp:HyperLink target=_blank id="hlUploadLink" NavigateUrl='<%#
    > DataBinder.Eval(Container.DataItem, "URL") %>' Runat="server"
    > CssClass="NormalBold">
    > <%# DataBinder.Eval(Container.DataItem, "File") %>
    > </asp:HyperLink>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    > <asp:Button id="btnDelete" runat="server" Text="Delete"></asp:Button>
    >
    > What do I put in the code behind private void btnDelete_Click to
    > delete the record selected?
    >
    > Thanks in advance!
    > Lisa
    >
    C Addison Ritchie, Aug 17, 2004
    #2
    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. H.G.Srivatsa

    Datagrid row deleting event

    H.G.Srivatsa, Aug 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    895
    Srivatsa
    Aug 22, 2005
  2. Lauchlan M
    Replies:
    2
    Views:
    729
    Lauchlan M
    Aug 26, 2003
  3. David
    Replies:
    0
    Views:
    356
    David
    Oct 25, 2003
  4. Tim Seaburn

    deleting datagrid row

    Tim Seaburn, Jul 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,932
    ocrow
    Jun 28, 2006
  5. D
    Replies:
    0
    Views:
    203
Loading...

Share This Page