首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

GridView透过列名取索引

2012-10-19 
GridView通过列名取索引代码?public int GetColumnIndex(GridView gridView, string fieldName)??????? {?

GridView通过列名取索引

代码
?public int GetColumnIndex(GridView gridView, string fieldName)
??????? {
??????????? int index = -1;
??????????? for (int i = 0; i < gridView.Columns.Count; i++)
??????????? {
??????????????? DataControlField dataControlField = gridView.Columns[i];
??????????????? BoundField boundField = dataControlField as BoundField;??????????????
??????????????? if (boundField != null && boundField.DataField == fieldName)
??????????????????? index =? i;
??????????? }
??????????? return index;
??????? }

或者可以利用HeaderText与FieldName进行比较,返回列索引:
?
代码
?public int GetColumnIndex(GridView gridview, string fieldName)
??????? {
??????????? int index = -1;
??????????? foreach (DataControlField dataControlField in gridview.Columns)
??????????? {
??????????????? if (dataControlField.HeaderText == fieldName)
??????????????? {
??????????????????? index = gridview.Columns.IndexOf(dataControlField);
??????????????? }
??????????? }
??????????? return index;
??????? }

热点排行