473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deleting row in Datagrid

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
Nov 18 '05 #1
4 1562
Lisa,
Something like this should work:
private void btnDelete_Click(object sender, System.EventArgs e) {
for (int i = 0; i < grdUpload.Items.Count; i++){
DataGridItem item = grdUpload.Items[i];
if(item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem){
CheckBox delete = (CheckBox)item.FindControl("delete");
if(delete != null && delete.Checked == true){
int key = (int)grdUpload.DataKeys[item.ItemIndex];
//Do something with the key, like DeleteItem(key);
}
}
}
}

make sure you aren't rebinding your datasource on PostBack.

Karl

"Lisa" <pe*****@yahoo.com> wrote in message
news:89**************************@posting.google.c om...
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

Nov 18 '05 #2
Karl,
Thanks so much for your reply but when I tried it, it does not recognize
that I've checked a record, so the delete.checked == false and it
bypasses my delete code. Any ideas?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
My guess is that you are rebinding on postback. Wrap your databinding code
in a postback check:

turn -->
grdUpload.DataSource = xxx;
grdUpload.DataBind();

to-->
if (!Page.IsPostBack){
grdUpload.DataSource = xxx;
grdUpload.DataBind();
}

Karl
"Lisa Boo" <pe*****@yahoo.com> wrote in message
news:OK**************@tk2msftngp13.phx.gbl...
Karl,
Thanks so much for your reply but when I tried it, it does not recognize
that I've checked a record, so the delete.checked == false and it
bypasses my delete code. Any ideas?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4
Karl,
Such a blonde moment - my Request.IsAuthenticated function in my
Page_load was set to true because I was testing something earlier and
forgot to change it back....so my login was failing.... and my postback
was a little different then what you provided me - so with both changes
- it works b-e-a-u-t-i-f-u-l-l-y. Thanks buddy - you ROCK!

~L~
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is...
1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
2
by: Alex K. | last post by:
I am using datagrid bound to a datatable: DataTable dt = ...; MyGrid.DataSource = new DataView(dt); Now I am trying to intercept delete operation, when user highlights a row and presses...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
2
by: NDady via DotNetMonster.com | last post by:
Hi, I have a datagrid populated from a dataset. On every row in the grid I have a delete button. When the user presses on the delete button I remove the row from the dataset and rebind the...
0
by: Robert Batt | last post by:
Hello I have a problem deleting rows from a datagrid that is bound to a dataset. The datagrid updates and inserts just fin so the binding to the dataset must be ok. However when I try to delete a...
1
by: Robert Batt | last post by:
Hello, I have a problem deleting from a datagrid bound to a dataset. then datagrid is bound as follows MyDatagrid.DataSource = Mydataset.Tables(strtable this works fine for adding and...
10
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option....
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.