一般要取得 GridView 中的单元格值,都是要指定该单元格所在的行列索引,根据单元格在浏览或编辑模式下,需要使用不同的方式来获取。
例如有一个“地区”的 BoundField,它是 GridView 中的第3列,在浏览模式下取得“地区”列的值,如下:
GridViewRow.Cells(3).Text
如果是在编辑模式时,因为该列值是在 Cell 中的 TextBox中,所以要使用下列方式来提取编辑时“地区”列的值,如下:
CType(oRow.Cells(3).Controls(0), TextBox).Text
以上获取 GridView 单元格值的没有良好的通用性,只要改变列顺序或变更列的类型(例如变成 TemplateField),这样程序很容易就发生错误,如果有变化则程序也需要随时修改。
为了在实际应用中解决这个问题,经过几种方式的对比,感觉比较好的方式是以列名来提取值。DataControlField 有一个 ExtractValuesFromCell 方法,不论是浏览或编辑模式都可以简单的取出 Cell 的对应的列,也不用去管它使用那一种 DataControlField (BoundField 、 CheckBoxField 或 TemplateField ) 都可以正确的取得对应的单元格值。
下面的程序示例就是通过 ExtractRowValues 函数获取出 GridView 指定单元格的值。
C#.NET:
private OrderedDictionary ExtractRowValues(DataControlFieldCollection Columns, GridViewRow Row)
{
OrderedDictionary oFieldValues;
OrderedDictionary oDictionary;
DataControlField oColumn;
oFieldValues = new OrderedDictionary(Columns.Count);
oDictionary = new OrderedDictionary();
for (int i = 0; i < Columns.Count; i++)
{
oColumn = Columns[i];
if (oColumn.Visible)
{
oDictionary.Clear();
oColumn.ExtractValuesFromCell(oDictionary, (DataControlFieldCell)Row.Cells[i], Row.RowState, true);
foreach (DictionaryEntry oEntry in oDictionary)
{
oFieldValues.Add(oEntry.Key, oEntry.Value);
}
}
}
return oFieldValues;
}
protected void Button1_Click(Object sender, EventArgs e)
{
GridViewRow oRow;
System.Collections.Specialized.OrderedDictionary oFieldValues;
oRow = (GridViewRow)GridView1.Rows[1];
oFieldValues = ExtractRowValues(GridView1.Columns, oRow);
//输出 “地区”名
this.Response.Write(oFieldValues["地区"].ToString());
}
{
OrderedDictionary oFieldValues;
OrderedDictionary oDictionary;
DataControlField oColumn;
oFieldValues = new OrderedDictionary(Columns.Count);
oDictionary = new OrderedDictionary();
for (int i = 0; i < Columns.Count; i++)
{
oColumn = Columns[i];
if (oColumn.Visible)
{
oDictionary.Clear();
oColumn.ExtractValuesFromCell(oDictionary, (DataControlFieldCell)Row.Cells[i], Row.RowState, true);
foreach (DictionaryEntry oEntry in oDictionary)
{
oFieldValues.Add(oEntry.Key, oEntry.Value);
}
}
}
return oFieldValues;
}
protected void Button1_Click(Object sender, EventArgs e)
{
GridViewRow oRow;
System.Collections.Specialized.OrderedDictionary oFieldValues;
oRow = (GridViewRow)GridView1.Rows[1];
oFieldValues = ExtractRowValues(GridView1.Columns, oRow);
//输出 “地区”名
this.Response.Write(oFieldValues["地区"].ToString());
}
VB.NET:
''' <summary>
''' 获取 GridView 指定列集合。
''' </summary>
''' <param name="Columns">列集合。</param>
''' <param name="Row">行。</param>
Private Function ExtractRowValues(ByVal Columns As DataControlFieldCollection, ByVal Row As GridViewRow) As OrderedDictionary
Dim oFieldValues As OrderedDictionary
Dim oColumn As DataControlField
Dim oDictionary As OrderedDictionary
Dim oEntry As DictionaryEntry
Dim N1 As Integer
oFieldValues = New OrderedDictionary(Columns.Count)
oDictionary = New OrderedDictionary()
For N1 = 0 To Columns.Count - 1
oColumn = Columns.Item(N1)
If oColumn.Visible Then
oDictionary.Clear()
oColumn.ExtractValuesFromCell(oDictionary, TryCast(Row.Cells.Item(N1), DataControlFieldCell), Row.RowState, True)
For Each oEntry In oDictionary
oFieldValues.Item(oEntry.Key) = oEntry.Value
Next
End If
Next
Return oFieldValues
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim oRow As GridViewRow
Dim oFieldValues As OrderedDictionary
oRow = CType(CType(sender, Control).BindingContainer, GridViewRow)
oFieldValues = ExtractRowValues(GridView1.Columns, oRow)
'输出 “地区”名
Me.Response.Write(oFieldValues("地区").ToString())
End Sub
''' 获取 GridView 指定列集合。
''' </summary>
''' <param name="Columns">列集合。</param>
''' <param name="Row">行。</param>
Private Function ExtractRowValues(ByVal Columns As DataControlFieldCollection, ByVal Row As GridViewRow) As OrderedDictionary
Dim oFieldValues As OrderedDictionary
Dim oColumn As DataControlField
Dim oDictionary As OrderedDictionary
Dim oEntry As DictionaryEntry
Dim N1 As Integer
oFieldValues = New OrderedDictionary(Columns.Count)
oDictionary = New OrderedDictionary()
For N1 = 0 To Columns.Count - 1
oColumn = Columns.Item(N1)
If oColumn.Visible Then
oDictionary.Clear()
oColumn.ExtractValuesFromCell(oDictionary, TryCast(Row.Cells.Item(N1), DataControlFieldCell), Row.RowState, True)
For Each oEntry In oDictionary
oFieldValues.Item(oEntry.Key) = oEntry.Value
Next
End If
Next
Return oFieldValues
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim oRow As GridViewRow
Dim oFieldValues As OrderedDictionary
oRow = CType(CType(sender, Control).BindingContainer, GridViewRow)
oFieldValues = ExtractRowValues(GridView1.Columns, oRow)
'输出 “地区”名
Me.Response.Write(oFieldValues("地区").ToString())
End Sub