In ASP.net, I'm using textbox in templatefield's itemtemplate. I got it data-bound with no problem. But my problem is, I'm trying to write a function to find column index by its data-bounding table's column name.
Something like this :
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
{
if (((BoundField)cell.ContainingField).DataField.Equals(SearchColumnName))
{
return columnIndex;
}
}
else if (cell.ContainingField is TemplateField)
{
//Finding column name of data-bound textbox or dropdownlist ??
}
}
will this helps you?
DataControlFieldCell fieldCell = HeaderRow.Cells[i] as DataControlFieldCell;
DataControlField field = fieldCell.ContainingField;
string strHdrTxt = field.HeaderText.ToString()
This one?
string colName = ds.Columns[0].ColumnName;
int GetColumIndex(string name)
{
foreach (DataControlField field in _GridView.Columns)
{
if (field.SortExpression == name)
{
return _GridView.Columns.IndexOf(field);
}
}
return -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With