请尝试使用以下代码片段。
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender">
<MasterTableView EditMode="InPlace" DataKeyNames="ID" CommandItemDisplay="Top">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ASPX.CS
public List<int> EditIDs
{
get
{
if (ViewState["EditID"] != null)
{
return (List<int>)ViewState["EditID"];
}
else
{
return new List<int>();
}
}
set { ViewState["EditID"] = value; }
}
public bool IsFilterCommandFire { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EditIDs = new List<int>();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"},
new { ID = 26, Name = "Name26"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.FilterCommandName)
{
IsFilterCommandFire = true;
}
else if (e.CommandName == RadGrid.EditCommandName)
{
int ID = Convert.ToInt32((e.Item as GridEditableItem).GetDataKeyValue("ID"));
EditIDs.Add(ID);
}
else if (e.CommandName == RadGrid.CancelCommandName)
{
int ID = Convert.ToInt32((e.Item as GridEditableItem).GetDataKeyValue("ID"));
EditIDs.Remove(ID);
}
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (IsFilterCommandFire)
{
foreach (GridDataItem item in RadGrid1.Items)
{
if (EditIDs.Contains(Convert.ToInt32(item.GetDataKeyValue("ID"))))
{
item.Edit = true;
}
else
{
item.Edit = false;
}
}
RadGrid1.Rebind();
}
}
让我知道是否有任何问题。