- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsHello - I have a datagridview that has a button column. Based on a value of another cell, I want the button to hide or appear. How can I do this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: culshajaPosted on 2007-06-25 at 00:34:17ID: 19353892
You need to use the ItemDataBound event (VS2002/2003) or the RowDataBound event (VS2005). This is fired wheneverr a row is added to the datagrid. The following code is taken from an artilce I wrote a while back for VS2003:
Val sender As System.Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgResults.ItemDataBound em Then
m, DataRowView).Row.Item(0), Integer) < 0 Then
(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles LeaveRequests.RowDataBound
ts.Request ListRow
Then , System.Data.DataRowView).R ow, _ ts.Request ListRow) unt - 2) unt - 1) ) Class = CANCELLED_RIGHT_TEXT_STYLE Class = CANCELLED_TEXT_STYLE ) ) Class = CANCELLED_RIGHT_TEXT_STYLE Class = CANCELLED_TEXT_STYLE
()) if the value in another column meets certain criteria.
Private Sub dgResults_ItemDataBound(By
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
If CType(CType(e.Item.DataIte
e.Item.Cells(0).Text = " "
e.Item.BackColor = System.Drawing.Color.White
End If
End If
End Sub
What it is doing is checking the value in the first column of the current row of the dataset and carrying out a task based on the result. What you want to do is very similar.
The following is code I use in a VS2005 app to do a smiliar thing:
Protected Sub LeaveRequests_RowDataBound
Dim boundRow As Dgc.Fls.Engine.LeaveReques
Dim statusCell, cancelCell As TableCell
If e.Row.RowType = DataControlRowType.DataRow
boundRow = CType(CType(e.Row.DataItem
Dgc.Fls.Engine.LeaveReques
statusCell = e.Row.Cells(e.Row.Cells.Co
cancelCell = e.Row.Cells(e.Row.Cells.Co
Select Case boundRow.status_name
Case REQUEST_PENDING
statusCell.CssClass = PENDING_STYLE
Case REQUEST_REJECTED
statusCell.CssClass = REJECTED_STYLE
cancelCell.Controls.Clear(
For cellIndex As Integer = 0 To (e.Row.Cells.Count - 2)
If cellIndex = DURATION_COLUMN_INDEX Then
e.Row.Cells(cellIndex).Css
Else
e.Row.Cells(cellIndex).Css
End If
Next
Case CANCELLATION_PENDING
statusCell.CssClass = PENDING_STYLE
Case CANCELLATION_REJECTED
statusCell.CssClass = REJECTED_STYLE
statusCell.Text = REQUEST_APPROVED
cancelCell.Controls.Clear(
Case CANCELLATION_APPROVED
cancelCell.Controls.Clear(
For cellIndex As Integer = 0 To (e.Row.Cells.Count - 2)
If cellIndex = DURATION_COLUMN_INDEX Then
e.Row.Cells(cellIndex).Css
Else
e.Row.Cells(cellIndex).Css
End If
Next
End Select
End If
End Sub
In this case I am actually removing the button in the button column (cancelCell.Controls.Clear
Hope this helps
James :-)