Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Deleting a row in a datagrid

Reply
Thread Tools

Deleting a row in a datagrid

 
 
Lisa
Guest
Posts: n/a
 
      08-13-2004
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
 
Reply With Quote
 
 
 
 
C Addison Ritchie
Guest
Posts: n/a
 
      08-17-2004
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[i].Cells[0].Controls[1];
if (chkBox.Checked)
{
int key = (int)grdUpload.DataKeys[i];
// 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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting datagrid row Tim Seaburn ASP .Net 3 06-28-2006 05:00 AM
Datagrid row deleting event H.G.Srivatsa ASP .Net 2 08-22-2005 06:20 AM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
Login failed for user when deleting row from datagrid David ASP .Net 0 10-25-2003 05:33 PM
alert messages and deleting a datagrid row. Lauchlan M ASP .Net 2 08-26-2003 02:04 AM



Advertisments