DataGridv.Controls.OfType(Of VScrollBar).SingleOrDefault.Visible always returns False

SHISHIR

New member
Joined
Dec 26, 2013
Messages
3
Programming Experience
5-10
Hi,

I added a DataGridView control named dgExpense in a tab page of a tabcontrol. My problem is that the following statement always returns False even if the vertical scroll bar is visible:
dgExpense.Controls.OfType(Of VScrollBar).SingleOrDefault.Visible

Please help.
 
First of all, why would you use SingleOrDefault in that case? The whole point of SingleOrDefault is that it will return the one and only item if there is one or else it will return Nothing. You are obviously assuming that it will not return Nothing though, so why call it in the first place? If you know for a fact that there will be one and only one item then you call Single and if there might not be any item then you call SingleOrDefault and then check whether it's Nothing before trying to use it.

As for the code, what are you actually trying to achieve because, whatever it is, that doesn't seem to be the way to achieve it. Are you trying to see whether all rows are visible?
 
Thanks for the reply.
Here is what I am trying to do (adjusting the width of column 2 based on the visibility of vertical scroll bar):

Private Sub frmTrans_Load(ByVal senderAs System.Object, ByVal eAs System.EventArgs) Handles MyBase.Load
Fill_Combo()
Fill_IDataGrid()
Fill_EDataGrid()

MsgBox(dgExpense.Controls.OfType(Of VScrollBar).SingleOrDefault.Visible)
End Sub

Private Sub Fill_EDataGrid()
If conHOFIN.ConnectDB(My.Settings.DB, My.Settings.Schema, My.Settings.Pass) = True Then
dsEHG.Clear()
adpEHG.Fill(dsEHG, "ex_details")
dgExpense.DataSource= dsEHG
dgExpense.DataMember= "ex_details"
dgExpense.Columns(0).Visible= False
dgExpense.Columns(5).Visible= False
dgExpense.Columns(6).Visible= False
dgExpense.Columns(1).Width= dgExpense.Width* 0.13
dgExpense.Columns(2).Width= IIf(dgExpense.Controls.OfType(Of VScrollBar).SingleOrDefault.Visible, (dgExpense.Width* 0.27) - dgExpense.Controls.OfType(Of VScrollBar).SingleOrDefault.Width, dgExpense.Width* 0.27)
dgExpense.Columns(3).Width= dgExpense.Width* 0.155
dgExpense.Columns(4).Width= dgExpense.Width* 0.44
End If
End Sub

--------------------------

May be I am doing it the wrong way, please suggest the correct way to achieve it.


 
Back
Top