DataGrid.SelectedIndex 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 DataGrid 控件中的选定项的索引。
public:
virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
public virtual int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.SelectedIndex : int with get, set
Public Overridable Property SelectedIndex As Integer
属性值
DataGrid 控件中的选定项的索引。
- 属性
例外
指定的索引小于 -1。
示例
下面的代码示例演示如何使用 SelectedIndex 属性来确定控件中 DataGrid 选定项的索引号。 它还演示如何以编程方式选择和取消选择项。
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="C#" runat="server">
DataTable Cart = new DataTable();
DataView CartView;
void Page_Load(Object sender, EventArgs e)
{
DataRow dr;
if (Session["ShoppingCart"] == null)
{
Cart.Columns.Add(new DataColumn("Qty", typeof(String)));
Cart.Columns.Add(new DataColumn("Item", typeof(String)));
Cart.Columns.Add(new DataColumn("Price", typeof(String)));
Session["ShoppingCart"] = Cart;
// Create rows and add sample data.
for (int i = 1; i <= 9; i++)
{
dr = Cart.NewRow();
if (i % 2 != 0)
dr[0] = "2";
else
dr[0] = "1";
dr[1] = "Item " + i.ToString();
dr[2] = (1.23 * (i + 1)).ToString();
Cart.Rows.Add(dr);
}
}
else
Cart = (DataTable)Session["ShoppingCart"];
CartView = new DataView(Cart);
CartView.Sort="Item";
if (!IsPostBack)
BindGrid();
}
void MyDataGrid_Select(Object sender, EventArgs e)
{
Label1.Text = "You selected " +
MyDataGrid.SelectedItem.Cells[1].Text +
".<br />" +
MyDataGrid.SelectedItem.Cells[1].Text +
" has an index number of " +
MyDataGrid.SelectedIndex.ToString() + ".";
}
void Select_Button_Click(Object sender, EventArgs e)
{
MyDataGrid.SelectedIndex = 4;
}
void UnSelect_Button_Click(Object sender, EventArgs e)
{
MyDataGrid.SelectedIndex = -1;
Label1.Text = "";
}
void BindGrid()
{
MyDataGrid.DataSource = CartView;
MyDataGrid.DataBind();
}
</script>
<head runat="server">
<title>DataGrid Selection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Selection Example</h3>
<asp:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Font-Names="Verdana"
Font-Size="8pt"
OnSelectedIndexChanged="MyDataGrid_Select"
AutoGenerateColumns="false">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<SelectedItemStyle BackColor="Yellow">
</SelectedItemStyle>
<Columns>
<asp:ButtonColumn
HeaderText="Select Item"
ButtonType="LinkButton"
Text="Select"
CommandName="Select">
</asp:ButtonColumn>
<asp:BoundColumn
HeaderText="Item"
ReadOnly="true"
DataField="Item"/>
<asp:BoundColumn
HeaderText="Quantity"
DataField="Qty"/>
<asp:BoundColumn
HeaderText="Price"
DataField="Price"/>
</Columns>
</asp:DataGrid>
<br /><br />
<asp:Button id="Button1"
Text="Select Item 5"
OnClick="Select_Button_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Unselect Item"
OnClick="UnSelect_Button_Click"
runat="server"/>
<br /><br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="VB" runat="server">
Dim Cart As New DataTable
Dim CartView As DataView
Sub Page_Load(sender As Object, e As EventArgs)
Dim dr As DataRow
If Session("ShoppingCart") Is Nothing Then
Cart.Columns.Add(New DataColumn("Qty", GetType(String)))
Cart.Columns.Add(New DataColumn("Item", GetType(String)))
Cart.Columns.Add(New DataColumn("Price", GetType(String)))
Session("ShoppingCart") = Cart
' Create rows and add sample data.
Dim i As Integer
For i = 1 To 9
dr = Cart.NewRow()
If i Mod 2 <> 0 Then
dr(0) = "2"
Else
dr(0) = "1"
End If
dr(1) = "Item " & i.ToString()
dr(2) =(1.23 *(i + 1)).ToString()
Cart.Rows.Add(dr)
Next i
Else
Cart = CType(Session("ShoppingCart"), DataTable)
End If
CartView = New DataView(Cart)
CartView.Sort = "Item"
If Not IsPostBack Then
BindGrid()
End If
End Sub 'Page_Load
Sub MyDataGrid_Select(sender As Object, e As EventArgs)
Label1.Text = "You selected " & MyDataGrid.SelectedItem.Cells(1).Text & _
".<br />" & MyDataGrid.SelectedItem.Cells(1).Text & " has an index number of " & _
MyDataGrid.SelectedIndex.ToString() & "."
End Sub 'MyDataGrid_Select
Sub Select_Button_Click(sender As Object, e As EventArgs)
MyDataGrid.SelectedIndex = 4
End Sub 'Select_Button_Click
Sub UnSelect_Button_Click(sender As Object, e As EventArgs)
MyDataGrid.SelectedIndex = - 1
Label1.Text = ""
End Sub 'UnSelect_Button_Click
Sub BindGrid()
MyDataGrid.DataSource = CartView
MyDataGrid.DataBind()
End Sub 'BindGrid
</script>
<head runat="server">
<title>DataGrid Selection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Selection Example</h3>
<asp:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Font-Names="Verdana"
Font-Size="8pt"
OnSelectedIndexChanged="MyDataGrid_Select"
AutoGenerateColumns="false">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<SelectedItemStyle BackColor="Yellow">
</SelectedItemStyle>
<Columns>
<asp:ButtonColumn
HeaderText="Select Item"
ButtonType="LinkButton"
Text="Select"
CommandName="Select">
</asp:ButtonColumn>
<asp:BoundColumn
HeaderText="Item"
ReadOnly="true"
DataField="Item"/>
<asp:BoundColumn
HeaderText="Quantity"
DataField="Qty"/>
<asp:BoundColumn
HeaderText="Price"
DataField="Price"/>
</Columns>
</asp:DataGrid>
<br /><br />
<asp:Button id="Button1"
Text="Select Item 5"
OnClick="Select_Button_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Unselect Item"
OnClick="UnSelect_Button_Click"
runat="server"/>
<br /><br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
注解
使用 SelectedIndex 属性确定控件中 DataGrid 用户选择的项的索引。 还可以使用此属性以编程方式指定控件中选择的 DataGrid 项。
若要取消选择控件中的 DataGrid 项,请将此属性设置为 -1。