从后面的代码中触发GridView SelectedIndexChanged

时间:2015-04-24 14:20:45

标签: c# asp.net gridview

我有一个关于SelectedIndexChanged实现的gridview。

fieldset

我在页面上有一个复选框,并在其上实施了CheckedChanged事件。

<asp:GridView ID="gvCalloutTeam" runat="server" OnRowDataBound="gvCalloutTeam_RowDataBound" OnSelectedIndexChanged="gvCalloutTeam_SelectedIndexChanged"
                            AutoGenerateColumns="False" EnableViewState="False" BackColor="White" BorderColor="#cccccc" BorderWidth="1px" CellPadding="2"
                            EmptyDataText="No person found" GridLines="None" Width="100%">
                            <AlternatingRowStyle BackColor="#ededed" />
                            <Columns>
                                .......... List of columns..............
                            </Columns>
                            <FooterStyle BackColor="#cccccc" ForeColor="Black" />
                            <HeaderStyle BackColor="#6699cc" Font-Bold="True" ForeColor="White" BorderColor="#cccccc" BorderWidth="1px" />
                            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                            <RowStyle BackColor="#fefefe" ForeColor="Black" />
                            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                            <SortedAscendingHeaderStyle BackColor="#0000A9" />
                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                            <SortedDescendingHeaderStyle BackColor="#000065" />
                        </asp:GridView>

我正在尝试从此事件中设置GridView的SelectedIndex。

<asp:CheckBox ID="cbTakeOutOrder" runat="server" Text="Take out of order" OnCheckedChanged="cbTakeOutOrder_CheckedChanged" AutoPostBack="true" />

但它不会触发GridView的SelectedIndexChanged事件。如果我在UI上选择行,它会触发但是如果我在代码中更改了索引,则它不会触发该事件。在代码中更改SelectedIndex后,有没有办法调用事件?

protected void cbTakeOutOrder_CheckedChanged(object sender, EventArgs e)
    {
        if (!cbTakeOutOrder.Checked && gvCalloutTeam.Rows.Count > 0)
            gvCalloutTeam.SelectedIndex = 0;
    }

1 个答案:

答案 0 :(得分:2)

如果您使用的是.net 4.5,则可以在GridView上调用SelectRow方法:

protected void cbTakeOutOrder_CheckedChanged(object sender, EventArgs e)
{
    if (!cbTakeOutOrder.Checked && gvCalloutTeam.Rows.Count > 0)
        gvCalloutTeam.SelectRow(0);
}