Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Disable or Halt EDIT in Gridview

Posted on 2008-10-22
8
Medium Priority
?
707 Views
Last Modified: 2013-11-26
I have a basic gridview report with EDIT enabled for UPDATING records. (VS 2005)
Is it possible to either disable or HALT the EDIT feature for an individual record based on if a cell in the gridview is not Null.   Im able to capture the gridview's cell value thru  'row.cells' and am using the different gridview methods (rowupdating, rowupdated, rowediting, etc.)
I now need to either disable the EDIT feature for THAT PARTICULAR RECORD or just HALT the row updating process if cell is NOT NULL.  Is there a code that just "kills" the process?

thanks for any assistance.
0
Comment
Question by:bretbel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22782976
Hi,
You misht be able to hide the edit function. Assuming the edit button is in the last cell you could set Visible=False or change the CssClass to a class that sets display:none;
That way the user would not be able to click edit.
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22786161
You could use M3mph15's solution, but just to clarify you have two ways of doing this:

Create your last column as a template column with link buttons and set the link buttons to have a CommandName of "Edit", "Delete", etc (whatever buttons you will need).  Note that this method is a bit of a pain because you will have to manually swap out the Edit button and replace it with Update and Cancel buttons when you initiate the update.

The better path is to add a RowDataBound event to the gridview and check to see if the cell you're concerned about has a null value.  If it does, you can do

foreach(Control c in row.Cells[MyCommandColumn#].Controls)
{
   if (typeof(c) == typeof(LinkButton) && ((LinkButton)c).Text == "Edit")
   {
       ((LinkButton)c).Visible = false;
   }
}

Your final alternative is within the rowediting event.  You can check the cell value prior to doing anything.  If the cell value is null, you can leave the gridview's EditIndex at -1 and then do a return; to exit out of the event.
0
 

Author Comment

by:bretbel
ID: 22791954
Bane83,
I went with your option 3.  It partially worked, it halts the EDIT functionality if column 2 has data, however it also halts ALL EDITING for every record.  Heres my code.  Any thoughts on why?

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        Dim revoke As String = GridView1.Rows(e.NewEditIndex).Cells(2).Text

        If revoke <> "" Then
            ' Cancel the edit operation.
            e.Cancel = True
            TextBox1.Text = "You cannot edit this record."
        Else
            TextBox1.Text = ""
        End If
    End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bretbel
ID: 22792233
I think I found the problem, when I debugged the code I found column 2 has "nbsp" within the record.  The SQL Source table is all NULLS so I don't know how this "non breaking space" character gets in there.  Any thoughts on this, if not.
Plan B:  Column 1 has a checkbox, that I can use what is syntax to determine if checkbox is true or false using ROWEDITING above, I can't seem to capture the value of the checkbox for the IF statement.

thanks
0
 
LVL 6

Assisted Solution

by:M3mph15
M3mph15 earned 600 total points
ID: 22792848
The nbsp is inserted into null fields so there is still technically something in there and when the gridlines get put in it will show up. Otherwise if the cell actually is null the gridview wont draw the gridlines...from what i understand.
You could always check for null or nbsp e.g If revoke <> "" and revoke <> "&nbsp;" then
As for the checkbox you will need to use the DirectCast method and the FindControl Method.

Dim chk As CheckBox = DirectCast(GridView1.Rows(e.NewEditIndex).Cells(0).FindControl("CheckBox1"), CheckBox)
If chk.Checked Then
'Code
End If
0
 
LVL 10

Accepted Solution

by:
Bane83 earned 1400 total points
ID: 22793271
Do a check for revoke.Trim() <> string.Empty instead.

As M3mph15 said, use DirectCast, but FindControl will only work if you're using a TemplateColumn and not a GridViewField.  If you're using a GridViewField, you'll need to do something like this:

Dim chk As CheckBox = DirectCast(GridView1.Rows(e.NewEditIndex).Cells(0).Controls(0), CheckBox)

If chk.Checked Then
'Code
End If
0
 

Author Comment

by:bretbel
ID: 22797823
Perfect! the Direct cast for Gridview and chk.check did it.
One more question
how can I format the cell to RED COLOR when the checkbox is checked
OR format the second column next to checkbox RED if the cell is not null?
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22797862
[cell].Styles.Add("background-color", "Red")

Replace [cell] with the full code required to access the cell.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question