• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3637
  • Last Modified:

Hide content of cell in datagridview VB.net

Hi ,

I have an unbound datagridview populated with data, some columns are checkbox columns,
I would like to read a cell in a row and based on the value /or lack of
I would like to hide a checkbox (disable, repaint, .....) in certain cells so that the user does not see it. How can I do this please.
0
MichMat
Asked:
MichMat
  • 4
  • 3
1 Solution
 
funwithdotnetCommented:
Something like this may work:
' Just after GridView is populated, loop through rows ...
Dim row as GridViewRow
For each row in MyGrdiView
If row.Item(0).text = "someval" Then
  row.Item(1).Controls(0).Visible = False ' represents a checkbox. 
End If
Next

Open in new window

0
 
MichMatAuthor Commented:
Hi
there are errors,
 Item not memeber of datagridview
when I changeitem to cells contorls is problem

I probably messed up something
0
 
MichMatAuthor Commented:
Hi ,

I figgured it out myself, probably not the best solution but it works

the solution lies in the padding I realised the control dissapears when there isnt enough room for it so increasing the padding will eventually squeez it out, I also set the cell to read only so that user can click into it ........ I would think that the row hight must be set at fixed height otherwise the row would increase to accomodate the padding ...... anyway the checkbox is no more and that is what I needed

 For R As Integer = 0 To Form1.DataGridView5.Rows.Count - 1
            If Form1.DataGridView5.Rows(R).Cells(2).Value = 0 Then
                Form1.DataGridView5.Rows(R).Cells(3).ReadOnly = True
                Form1.DataGridView5.Rows(R).Cells(3).Style.Padding = New Padding(4)
            End If
        Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jpaulinoCommented:
Do you still want a solution ?
0
 
MichMatAuthor Commented:
If there is one , yes
0
 
jpaulinoCommented:
You can use the cellpainting event to clear the unwanted checkboxes based on some condition.
In this example it doesn't display the checkboxes in the first column if the second column has no values.

    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex <> -1 AndAlso e.ColumnIndex = 0 Then
            If Me.DataGridView1(1, e.RowIndex).Value Is Nothing Then
                e.Paint(e.ClipBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground And Not DataGridViewPaintParts.ContentBackground)
                e.Handled = True
            End If
        End If
    End Sub

Open in new window

0
 
jpaulinoCommented:
Any luck ?
0
 
MichMatAuthor Commented:
Had to change it a little to suit but thank you for your help once again
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now