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;
??????? }