2016-04-26 55 views
3

后,我有了在复选框选择项目选定行成为非选择过滤

如何才能筛选网格视图,而不会丢失已检查的项目网格视图。

的GridView

<asp:GridView ID="gridpur" CssClass="table table-bordered text-nowrap" runat="server" AutoGenerateColumns="False" DataKeyNames="Pro_ID" > 
<Columns> 
<asp:TemplateField HeaderText="Select"> 
<ItemTemplate> 
<asp:CheckBox ID="chkSel" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

滤膜代码

StoreClass s = new StoreClass(); 
gridpur.DataSource = s.SearchPurchase(hdnSearchParam.Value, txtsearch.Text); 
gridpur.DataBind(); 

GridView的数据源

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     StoreClass s = new StoreClass(); 
     gridpur.DataSource = s.getpurchase(); 
     gridpur.DataBind(); 
    } 
} 
+0

商店的复选框的状态然后再过滤,然后恢复它们,或者将数据绑定到网格的复选框部分设置为状态。 –

回答

3

你必须ST之前你DataBind它矿石您选择复选框(这似乎是Pro_ID列/属性的键),然后之后再次选择它们:

在您的过滤方法:

List<string> selectedProIDs = gridpur.Rows.Cast<GridViewRow>() 
    .Select(row => new { 
     CheckBox = (CheckBox)row.FindControl("chkSel"), 
     ProID = gridpur.DataKeys[row.RowIndex].Value.ToString() 
    }) 
    .Where(x => x.CheckBox.Checked) 
    .Select(x => x.ProID) 
    .ToList(); 

StoreClass s = new StoreClass(); 
gridpur.DataSource = s.SearchPurchase(hdnSearchParam.Value, txtsearch.Text); 
gridpur.DataBind(); 

foreach(GridViewRow row in gridpur.Rows) 
{ 
    var checkBox = (CheckBox)row.FindControl("chkSel"); 
    string proID = gridpur.DataKeys[row.RowIndex].Value.ToString(); 
    checkBox.Checked = selectedProIDs.Contains(proID); 
}