Are you displaying the categoryID in the grid? also are you using BoundField? If so then you can directly get it at RowDeleting event of the Gridview:
string categoryID = GridView1.Rows[e.RowIndex].Cells[index].Text;
//where index is the column index in which the field reside
//if you are using TemplateField then you can use FindControl method to get the value from a control.
This is if your link button has a command name set to delete in its field, you can remove this if statement, and call the DataGridItem container directly, but its useful to have a check if you have more than one link button on the same row, for instance
add or edit.
'System.EventArgs' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
I am using ASP.NET 3.5 with adding the following libraries: -
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
Neethus
Member
15 Points
68 Posts
Datagrid
Jan 23, 2013 10:13 AM|LINK
hi all,
i have a datagrid which contains id, name, address, phone, categoryid, category and a link button DELETE.
When i click the DELETE button how can i get the selected rows categoryid? By DataKeyField i'll get the id only not the categoryid.
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Datagrid
Jan 23, 2013 10:23 AM|LINK
Are you displaying the categoryID in the grid? also are you using BoundField? If so then you can directly get it at RowDeleting event of the Gridview:
Or if you don't want to expose it you can always use a HiddenField and grab the value from there. http://geekswithblogs.net/dotNETvinz/archive/2009/06/22/faq-how-to-get-hidden-columns-value-in-gridview.aspx
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Neethus
Member
15 Points
68 Posts
Re: Datagrid
Jan 23, 2013 10:30 AM|LINK
I am using Datagrid instead of Gridview.
guinnesslee
Participant
1050 Points
197 Posts
Re: Datagrid
Jan 23, 2013 10:32 AM|LINK
if (dataGridView1.SelectedRows.Count != 0) { DataGridViewRow row = this.dataGridView1.SelectedRows[0]; row.Cells["ColumnName"].Value }twitter
www.mashupweb.wordpress.com
ManikandanUl...
Participant
850 Points
253 Posts
Re: Datagrid
Jan 23, 2013 10:40 AM|LINK
//aspx code:
set in DataKeyField="categoryid"// wchih is from your db catogoryid coloum...
//aspx.cs code
get value
tring categoryid=DataGrid1.DataKeyField(e.Item.ItemIndex)
or
string categoryid=DataGrid1.DataKeys(e.Item.ItemIndex).ToString)
Click "…Mark As Answer" if my reply helpful....
Neethus
Member
15 Points
68 Posts
Re: Datagrid
Jan 23, 2013 10:57 AM|LINK
hi,
i got error as
The type or namespace name 'DataGridViewRow' could not be found (are you missing a using directive or an assembly reference?)
guinnesslee
Participant
1050 Points
197 Posts
Re: Datagrid
Jan 23, 2013 11:12 AM|LINK
Ah thats for C# Windows Application.
For DataGrid in ASP.NET ,to get the selected rows try the event handler:
protected void GetItem(object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "delete")
{
DataGridItem container = (DataGridItem)e.Item;
string fieldID = container.Cells[0].Text;
}
}
twitter
www.mashupweb.wordpress.com
guinnesslee
Participant
1050 Points
197 Posts
Re: Datagrid
Jan 23, 2013 11:14 AM|LINK
This is if your link button has a command name set to delete in its field, you can remove this if statement, and call the DataGridItem container directly, but its useful to have a check if you have more than one link button on the same row, for instance add or edit.
twitter
www.mashupweb.wordpress.com
Neethus
Member
15 Points
68 Posts
Re: Datagrid
Jan 23, 2013 11:27 AM|LINK
Again got error as
'System.EventArgs' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
guinnesslee
Participant
1050 Points
197 Posts
Re: Datagrid
Jan 23, 2013 02:38 PM|LINK
I am using ASP.NET 3.5 with adding the following libraries: -
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
twitter
www.mashupweb.wordpress.com