Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get binding column name of templatefield's texbox

Tags:

c#

asp.net

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 ??
  }
}
like image 888
Vachara Avatar asked Sep 16 '10 04:09

Vachara


2 Answers

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;

like image 89
Bala Avatar answered Oct 06 '22 01:10

Bala


int GetColumIndex(string name)
{
    foreach (DataControlField field in _GridView.Columns)
    {
        if (field.SortExpression == name)
        {
            return _GridView.Columns.IndexOf(field);
        }
    }

    return -1;
}  
like image 32
Yoav Avatar answered Oct 06 '22 01:10

Yoav