By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

How to find the Column Index By Name in an ASP.Net DataGrid

P: n/a
Howdy All,

I am wondering if there is a way to find the column index by the header
name. What I am trying to get away from is using a hardcoded number in
the ItemDataBound event. Something like this:

private void grdTestScores_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataRowView drvData = (DataRowView)e.Item.DataItem;
Button btnDelete;

if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
btnDelete = (Button)e.Item.Cells[16].Controls[0];

if (btnDelete.CommandName == "Delete")
{
btnDelete.Attributes.Ad("onclick", "return ConfirmDelete();");
}

}
....

I don't want to hardcode the column number 16 in the above example.

Thanks in advance.

dbl

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If the columns are not auto-generated, you can use datagrid Columns
collection. Write a utility function that will take a column name as a
parameter, loop through Columns collection, find a column with matching
name, get it's index and return Cells element with the same index.

Eliyahu

"DBLWizard" <ib*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Howdy All,

I am wondering if there is a way to find the column index by the header
name. What I am trying to get away from is using a hardcoded number in
the ItemDataBound event. Something like this:

private void grdTestScores_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataRowView drvData = (DataRowView)e.Item.DataItem;
Button btnDelete;

if ((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
btnDelete = (Button)e.Item.Cells[16].Controls[0];

if (btnDelete.CommandName == "Delete")
{
btnDelete.Attributes.Ad("onclick", "return ConfirmDelete();");
}

}
...

I don't want to hardcode the column number 16 in the above example.

Thanks in advance.

dbl

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.