如何在gridview中获取文本框的单元格索引

时间:2014-11-12 08:27:29

标签: c# asp.net gridview textbox

如何在gridview中获取文本框的单元格索引?

我的代码:

protected void txt_1_TextChanged(object sender, EventArgs e)
{
     int progSer = int.Parse(Session["prog"].ToString());
     RadNumericTextBox txt = (RadNumericTextBox)sender;

     GridViewRow r = (GridViewRow)txt.NamingContainer;
     //now i want to get the cell index of my fired textbox ,say this text box is in the second column so i want to get index 2
}

1 个答案:

答案 0 :(得分:2)

您可以使用TableCellCollection.GetCellIndex和循环来查找TextBox的单元格:

TableCell cell = null;
Control parent = txt;
while ((parent = parent.Parent) != null && cell == null)
    cell = parent as TableCell;
int indexOfTextBoxCell = -1;
if (cell != null)
    indexOfTextBoxCell = r.Cells.GetCellIndex(cell);