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

D

DBLWizard

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
 
E

Eliyahu Goldin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top