How to show and hide the Edit and Select links in a datagridview in asp.net

I have a DataGridView on my page that has the AutoGenerateSelectButton and AutoGenerateEditButton set to true.  When the user clicks on the select link, the selected row is highlighted and information is displayed in another update panel on the page.  When the user clicks on the Edit link they can edit the information in the gridview as well as the data displayed in the other UpdatePanel.

I need to prevent the user from clicking on the Edit link on any row except the one selected.

I tried to hide the Edit link using the following code in the RowCreated event:
e.Row.Cells[-].Controls[0].Visible = false

This worked great.  Only the Select link appears.  

In the SelectedIndexChanged event I unhide the Edit link and hide the Select link using the following code:
// Show Select Link and Hide Edit Link for previous row selected
GridViewRow prow = gvSchedules.Rows[gvSchedules.SelectedIndex];
prow.Cells[0].Controls[0].Visible = false;
prow.Cells[0].Controls[2].Visible = true;
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].Visible = true;
row.Cells[0].Controls[1].Visible = false;

This also works great.  Only the Edit link appears in the Selected row.

The problem occurs when I click on the Edit Link.  I only get the Cancel link.  I don't get the Update link.  Can anyone tell me how to get the Update link to appear with my current setup?  If I don't try and hide/show the Edit/Select links then when I click on Edit, the Update and Cancel links appear and work appropriately.  Any help is greatly appreciated!
dyaroshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
row.Cells[0].Controls[1].Visible = false;

Wouldn't 1 be the update link?  

If you set a break point on that line, you can interrogate each of the controls via the immediate window to find out which control needs to be visible.
0
guru_samiCommented:
Check if the RowState is Edit then unhide the [0] control. May be RowEditing is the event that you should look doing it.
0
dyaroshAuthor Commented:
I put a break in the RowEditing Event and this is what I found:

gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls.Count = 3
....Cells[0].Controls[0].Text = "Edit"
....Cells[0].Controls[0].Visible = "true"
....Cells[0].Controls[1].Text is not defined
....Cells[0].Controls[1].Visible = "true"
....Cells[0].Controls[2].Text = "Select"
....Cells[0].Controls[2].Visible = "false"

Here is what gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[1] shows:
? gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[1]
{System.Web.UI.LiteralControl}
    [System.Web.UI.LiteralControl]: {System.Web.UI.LiteralControl}
    AppRelativeTemplateSourceDirectory: "~/"
    BindingContainer: {System.Web.UI.WebControls.GridViewRow}
    ClientID: "dynamiccontainer_onebox_onebox_gvSchedules_ctl02_0"
    ClientIDMode: Inherit
    Controls: {System.Web.UI.EmptyControlCollection}
    DataItemContainer: {System.Web.UI.WebControls.GridViewRow}
    DataKeysContainer: {System.Web.UI.WebControls.GridView}
    EnableTheming: true
    EnableViewState: false
    ID: "ctl02"
    NamingContainer: {System.Web.UI.WebControls.GridViewRow}
    Page: {ASP.maintainscheduledescriptions_aspx}
    Parent: {System.Web.UI.WebControls.DataControlFieldCell}
    RenderingCompatibility: {4.0}
    Site: null
    SkinID: ""
    TemplateControl: {ASP.maintainscheduledescriptions_aspx}
    TemplateSourceDirectory: "/"
    UniqueID: "ctl00$ctl00$ctl00$dynamiccontainer$onebox$onebox$gvSchedules$ctl02$ctl02"
    ViewStateMode: Inherit
    Visible: true

I can't find the Update and Cancel links and only the Cancel link shows.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

guru_samiCommented:
If you set breakpoint in RowCreated event do you see Update link?
0
dyaroshAuthor Commented:
In the RowCreated event I still do not see the Update and Cancel Links:

e.Row.Cells[0].Controls[0]
{Text = "Edit"}
    [System.Web.UI.WebControls.DataControlLinkButton]: {Text = "Edit"}
    AppRelativeTemplateSourceDirectory: "~/"
    BindingContainer: {System.Web.UI.WebControls.GridViewRow}
    ClientID: "ctl00_0"
    ClientIDMode: Inherit
    Controls: {System.Web.UI.ControlCollection}
    DataItemContainer: {System.Web.UI.WebControls.GridViewRow}
    DataKeysContainer: null
    EnableTheming: true
    EnableViewState: true
    ID: "ctl00"
    NamingContainer: {System.Web.UI.WebControls.GridViewRow}
    Page: null
    Parent: {System.Web.UI.WebControls.DataControlFieldCell}
    RenderingCompatibility: {4.0}
    Site: null
    SkinID: ""
    TemplateControl: null
    TemplateSourceDirectory: "/"
    UniqueID: "ctl00"
    ViewStateMode: Inherit
    Visible: true

 e.Row.Cells[0].Controls[1]
{System.Web.UI.LiteralControl}
    [System.Web.UI.LiteralControl]: {System.Web.UI.LiteralControl}
    AppRelativeTemplateSourceDirectory: "~/"
    BindingContainer: {System.Web.UI.WebControls.GridViewRow}
    ClientID: "ctl01_0"
    ClientIDMode: Inherit
    Controls: {System.Web.UI.EmptyControlCollection}
    DataItemContainer: {System.Web.UI.WebControls.GridViewRow}
    DataKeysContainer: null
    EnableTheming: true
    EnableViewState: false
    ID: "ctl01"
    NamingContainer: {System.Web.UI.WebControls.GridViewRow}
    Page: null
    Parent: {System.Web.UI.WebControls.DataControlFieldCell}
    RenderingCompatibility: {4.0}
    Site: null
    SkinID: ""
    TemplateControl: null
    TemplateSourceDirectory: "/"
    UniqueID: "ctl01"
    ViewStateMode: Inherit
    Visible: true

 e.Row.Cells[0].Controls[2]
{Text = "Select"}
    [System.Web.UI.WebControls.DataControlLinkButton]: {Text = "Select"}
    AppRelativeTemplateSourceDirectory: "~/"
    BindingContainer: {System.Web.UI.WebControls.GridViewRow}
    ClientID: "ctl02_0"
    ClientIDMode: Inherit
    Controls: {System.Web.UI.ControlCollection}
    DataItemContainer: {System.Web.UI.WebControls.GridViewRow}
    DataKeysContainer: null
    EnableTheming: true
    EnableViewState: true
    ID: "ctl02"
    NamingContainer: {System.Web.UI.WebControls.GridViewRow}
    Page: null
    Parent: {System.Web.UI.WebControls.DataControlFieldCell}
    RenderingCompatibility: {4.0}
    Site: null
    SkinID: ""
    TemplateControl: null
    TemplateSourceDirectory: "/"
    UniqueID: "ctl02"
    ViewStateMode: Inherit
    Visible: true
0
Kyle AbrahamsSenior .Net DeveloperCommented:
try the following:

//set all visible in currently selected row.
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].Visible = true;
row.Cells[0].Controls[1].Visible = true;
row.Cells[0].Controls[2].Visible = true;
0
dyaroshAuthor Commented:
Did as you suggested and it still just shows the Cancel link when Edit is clicked.
0
guru_samiCommented:
Try this in RowCreated event:
if (e.Row.RowType == DataControlRowType.DataRow )
        {
            if (e.Row.RowState == (DataControlRowState.Edit|DataControlRowState.Selected))
            {
                e.Row.Cells[0].Controls[0].Visible = true;
            }
        
            else
            {
                e.Row.Cells[0].Controls[0].Visible = false;
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Thank you.  This will make is so much easier for my user.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.