在多选数据网格中捕获de selected复选框

时间:2018-03-16 16:50:29

标签: c# datagrid

所以我需要增加一些旧代码,并希望得到一些帮助。

我有这个dataGrid:

<asp:DataGrid ID="dgSharedUserList" Runat="server" BorderStyle="Solid" BorderColor="Black" AutoGenerateColumns="False"
DataKeyField="user_id" Width="98%" AlternatingItemStyle-BackColor="LightSteelBlue">
<HeaderStyle BackColor="Gainsboro" Font-Bold="True" BorderColor="Black" BorderStyle="Solid"></HeaderStyle>
<ItemStyle BackColor="White" BorderColor="Black" BorderStyle="Solid"></ItemStyle>
<Columns>
<mbrsc:RowSelectorColumn SelectionMode="Multiple" OnSelectionChanged="saveSharedUserReferrals" AutoPostBack="true"></mbrsc:RowSelectorColumn>
<asp:BoundColumn DataField="User_Id" HeaderText="User_Id" ReadOnly="True" ItemStyle-Width="10%" />
<asp:BoundColumn DataField="FullName" HeaderText="User Name" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="loginId" HeaderText="User Login" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="Phone" HeaderText="Phone" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="Email" HeaderText="E-mail" ReadOnly="True" ItemStyle-Width="20%" />
</Columns>
</asp:DataGrid>

我可以填充网格没有问题。我能够成功捕获行单击事件,并具有以下代码来处理:

protected void saveSharedUserReferrals(object sender, System.EventArgs e)
{
RowSelectorColumn rsc = RowSelectorColumn.FindColumn(dgSharedUserList);
int selIndex = 0;
if (rsc.SelectedIndexes.Length == 0)
{
// How do I get the index of the row de selected??
}
else
{
selIndex = rsc.SelectedIndexes[0];
string user_id = dgSharedUserList.DataKeys[selIndex].ToString();
}
}

我的问题是我需要获取未选择行的Key,以便我也可以取消选择数据库中的行。我错过了什么?我试图在事件和RowSelectorColumn对象中捕获它无济于事。

请注意我希望尽可能保留复选框,以便用户可以看到已选择的内容。

1 个答案:

答案 0 :(得分:1)

尝试使用复选框而不是mbrsc:RowSelectorColumn:

<asp:DataGrid ID="dgSharedUserList" Runat="server" BorderStyle="Solid" BorderColor="Black" AutoGenerateColumns="False"
DataKeyField="user_id" Width="98%" AlternatingItemStyle-BackColor="LightSteelBlue">
<HeaderStyle BackColor="Gainsboro" Font-Bold="True" BorderColor="Black" BorderStyle="Solid"></HeaderStyle>
<ItemStyle BackColor="White" BorderColor="Black" BorderStyle="Solid"></ItemStyle>
<Columns>
<asp:TemplateColumn>
        <ItemTemplate>
          <asp:CheckBox runat="server" UserIdFld='<%# Eval("user_id") %>' ID="RowLevelCheckBox" OnCheckedChanged="saveSharedUserReferrals" AutoPostBack="true"/>
            </ItemTemplate>
   </asp:TemplateColumn>
<%--<mbrsc:RowSelectorColumn SelectionMode="Multiple" OnSelectionChanged="saveSharedUserReferrals" AutoPostBack="true"></mbrsc:RowSelectorColumn>--%>
<asp:BoundColumn DataField="User_Id" HeaderText="User_Id" ReadOnly="True" ItemStyle-Width="10%" />
<asp:BoundColumn DataField="FullName" HeaderText="User Name" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="loginId" HeaderText="User Login" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="Phone" HeaderText="Phone" ReadOnly="True" ItemStyle-Width="20%" />
<asp:BoundColumn DataField="Email" HeaderText="E-mail" ReadOnly="True" ItemStyle-Width="20%" />
</Columns>
</asp:DataGrid>

在服务器中:

 protected void saveSharedUserReferrals(object sender, System.EventArgs e)
        {
            var id = ((CheckBox)sender).Attributes["UserIdFld"].ToString();
            //RowSelectorColumn rsc = RowSelectorColumn.FindColumn(dgSharedUserList);
            //int selIndex = 0;
            //if (rsc.SelectedIndexes.Length == 0)
            //{
            //    // How do I get the index of the row de selected??
            //}
            //else
            //{
            //    selIndex = rsc.SelectedIndexes[0];
            //    string user_id = dgSharedUserList.DataKeys[selIndex].ToString();
            //}
        }