Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

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!
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

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.
Check if the RowState is Edit then unhide the [0] control. May be RowEditing is the event that you should look doing it.
Avatar of dyarosh
dyarosh

ASKER

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.
If you set breakpoint in RowCreated event do you see Update link?
Avatar of dyarosh

ASKER

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
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;
Avatar of dyarosh

ASKER

Did as you suggested and it still just shows the Cancel link when Edit is clicked.
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh

ASKER

Thank you.  This will make is so much easier for my user.