Optional edit in GridView's rows?
Suppose I have a GridView on the page. GridView has edit column enabled and is showing some开发者_StackOverflow中文版 records. How can I enable/disable edit in rows based on other fields of data?
You can do this in a number of ways. Two of these are:
First convert the edit column to a template field.
Whatever field you want to base the enable/disable on you can add the the GridView's DataKeyNames property.
Then on the OnRowDataBound event you can do the following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Normal)
{
var LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton1.Enabled = GridView1.DataKeys[e.Row.RowIndex].Value == "SomeValue"; //Or some other logic, like converting to a boolean
}
}
Or,
In the Html markup of you aspx page, edit the linkbutton enabled property to bind the your desired field. Such as:
<asp:LinkButton ID="LinkButton1" runat="server" Text="Edit" Enabled='<%# Convert.ToBoolean(Eval("SomeField")%>'></asp:LinkButton>
Hope that helps.
精彩评论