GridView: conditional editing

  • Thread starter Thread starter JDC
  • Start date Start date
J

JDC

Hi all,

How do I allow or disallow editing in a GridView on a row-by-row basis,
depending on the value of a field in that row?

TIA, JC
 
Use the RowDataBound event handler, and do something like

TextBox MyTextBox = (TextBox)e.Row.FindControl("IDOfControl");

if (MyTextBox.Text == "ValueYouAreLookingFor")
{
LinkButton MyLinkButton = (Linkbutton)e.Row.FindControl("IDOfEditLinkbutton");
MyLinkButton.visible = false;
}
 
Thanks, that seems to be the right track. However, I'm trying to hide
(or disable) a CommandField (which uses images, not links), and I can't
figure out what control ID to pass to FindControl():


protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView vw = (DataRowView)e.Row.DataItem;

if (vw["Locked"].ToString() == "True")
{
//e.Row.FindControl(String.Format("Edit${0}",
e.Row.RowIndex)).Visible = false;
}
}
}

Any ideas?

Cheers, JC
 
Turn your CommandField into a TemplateField, you can then set the ID of the
ImageButton that provides the edit command.
 
Yep, that works perfectly, thanks. Better than I originally envisaged,
actually, as my "status" flag was already another template column, and
now I have just one "smart" template column.

Cheers, JC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top