例如:需要Gridview选中“栏目名称”为“办事指南”的那行,注意DataKeyNames中必须有“栏目名称”,否则将会出现错误!!不知道谁还有更好的方法解决这个问题,小女子在此热盼高手指教:
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="栏目编号,栏目名称" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Row As GridViewRow
For Each Row In GridView1.Rows
'Dim KeyValue As String = GridView1.DataKeys(Row.RowIndex).Value 取得“栏目编号”列的值,Value也可以写成Values(0) ,Values的索引是与Gridview中的DataKeyNames相对应的
Dim KeyValue As String = GridView1.DataKeys(Row.RowIndex).Values(1) '取得“栏目名称”列的值
If (KeyValue = "办事指南") Then
GridView1.SelectedIndex = Row.RowIndex
End If
Next
End Sub