选择没有将AutoGenerateSelectButton设置为true的行

时间:2012-10-08 14:14:29

标签: asp.net

如何选择未将AutoGenerateSelectButton设置为true的行并检查GridView_SelectedIndexChanged中的selectedIndex?

如何识别用户选择表格上的特定字段(例如选择的row2,col3)

2 个答案:

答案 0 :(得分:0)

使用按钮添加列(使用模板字段)并将CommandName设置为“Select”。这应该有用。

答案 1 :(得分:0)

这是一个没有选择按钮的平均网格视图。 RowDataBound方法使您的网格可突出显示。您可以检查某个列是否可能选中了SelectedIndexChanged方法中的条件,因为它会告诉您选择了哪一行,然后您可以根据列的顺序从特定列中获取值,如下所示。这不会显示他们点击x和y的确切位置,但应该让我朝着正确的方向前进。但是,这将根据突出显示的行获取列数据,然后点击返回帖子。

<asp:GridView ID="PropertyGridView" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="PropertyGridView_SelectedIndexChanged" OnRowDataBound="PropertyGridView_RowDataBound">
                <RowStyle HorizontalAlign="Left" />
                <EmptyDataTemplate>
                    <div>
                        <asp:Label runat="server" ID="EtLbl" Text="No properties were found."</asp:Label>
                    </div>
                </EmptyDataTemplate>
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="User Name" Width="20%" /> </asp:BoundField>
                    <asp:BoundField DataField="Age" HeaderText="User Age" Width="20%" /> </asp:BoundField>
                    <asp:BoundField DataField="Height" HeaderText="User Height" Width="20%" /> </asp:BoundField>
                </Columns>
</asp:GridView>


public void PropertyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.DataItemIndex == -1) 
        return;
    e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';");
    e.Row.Attributes.Add("onclick",    this.GetPostBackClientEvent(PropertyGridView,"Select$" + e.Row.RowIndex.ToString()));
}

public void PropertyGridView_SelectedIndexChanged(object sender, EventArgs e)
{

    string Name = PropertyGridView.SelectedRow.Cells[0].Text;
    string Age= PropertyGridView.SelectedRow.Cells[1].Text;
    string Height PropertyGridView.SelectedRow.Cells[2].Text;

    if( Height != null)
    {
         // The user selected a row and this Column ( Height has data )
    }


}