Cannot clear this list - vb.net

I'm getting the above error message on a DataGridView. This dgv is bound to a datatable. Below is the code that I use to check it.
If TypeOf ctl Is DataGridView Then
Dim ctl1 As DataGridView = DirectCast(ctl, DataGridView)
If Not IsNothing(ctl1.DataSource) = False Then
ctl1.DataSource = Nothing
Else
ctl1.Rows.Clear()
End If
End If
The code is taking the Else branch so I don't know why it's throwing this exception.

Are you sure it is taking the else branch?
If the DGV is databound the the Rows.Clear() will throw an exception.
Try changing the if statement to
If IsNothing(ctl1.Datasource) = False Then

You can clear the rows to what ctl1 DataGridView is pointing to instead of clearing ctl1 directly:
myDataTable.Rows.Clear()
See why i cannot clear the rows in the datagridview control?
Or,
https://www.codeproject.com/Questions/332902/how-to-clear-datagridview-in-csharp

Related

Selected Value for combobox not working for DirectCast or CType

I am trying to clear out the selection of a combobox on my form, but when setting the .SelectedValue property to -1, I instead get whatever the first value is.
To explain headerControls and controlsList, I create all of my controls at runtime from a config file, loaded into controlsList and when I add the control to the screen I also add it to headerControls
Public headerControls As New Dictionary(Of String, Control)
Try
For each dynamicControl As KeyValuePair(Of Integer, CustomControl) In controlsList
Select dynamicControl.Value.ControlType.ToLower()
Case "textbox"
DirectCast(headerControls.Item(dynamicControl.Value.ControlName), TextBox).Text = ""
Case "combobox"
DirectCast(headerControls.Item(dynamicControl.Value.ControlName), ComboBox).SelectedValue = -1
End Select
Next
Catch ex As Exception
'Error Handling
End Try
The code that creates the controls:
Try
For each dynamicControl As KeyValuePair(Of Integer, CustomControl) In controlsList
Select Case dynamicControl.Value.ControlType.ToLower()
Case "textbox"
Dim textBoxControl As New TextBox
With textBoxControl
'Set Textbox properties here
End With
headerControls.Add(dynamicControl.Value.ControlName, textBoxControl)
Controls.Add(textBoxControl)
Case "combobox"
Dim comboBoxControl As New ComboBox
With comboBoxControl
'Set ComboBox properties here
End With
LoadCombo(comboBoxControl)
headerControls.Add(dynamicControl.Value.ControlName, comboBoxControl)
Controls.Add(comboBoxControl)
End Select
Next
Catch ex As Exception
'Error Handling
End Try
Clearing the text boxes works, but ComboBox is not showing the same behavior as if I placed one on the screen and used .SelectedValue = -1
Perhaps you should check the difference between SelectedValue and SelectedIndex. Setting SelectedValue to a value can't possibly select no item. If you want no item selected then you set SelectedIndex to -1, SelectedItem to Nothing or, depending on your binding, SelectedValue to Nothing.

Dynamically added control (DataGridView) doesn't work properly

I posted a question here and, after many attempts, I accepted the answer who suggested me to use an hidden control instead of an added control.
This way my code run correctly but I would like to understand the mistake.
That's what happens:
When I press a button a new DataGridView is added but
a) it isn't visible even if his property visible is set True;
b) if (while the added DGV is on the form) I set DGV property visible to False and then I re-set it to True the DGV appears but columns aren't resized;
c) if I remove the added DGV and re-add it, it is "invisible" again.
ALL THESE TROUBLES DON'T HAPPEN IF I RUN ANOTHER SUB
If:
a) I run a sub that hides my first form and shows another form;
b) then I close the 2nd form to go back to the first form;
all works fine:
the DGV is correctly added;
it is visible;
all columns are correctly resized;
if I remove the DGV or re-add it, all still works.
Where am I wrong?
This is the code I got from your earlier link. It shows the datagridview as expected in my form. As you can see, it is exactly your code, with the exception of that "using" block --- and the way you retrieve the datatable. That's the only two differences.
Private Sub ShowHideTbl()
Dim DTemp As DataTable = GetTable()
Dim DGV_Tbl As DataGridView = Nothing
Try
DGV_Tbl = CType(Me.Controls("DGV_Tbl"), DataGridView)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
If DGV_Tbl Is Nothing Then
If Me.CBox_ProcType.Text = "Select a Procedure" Then
MsgBox("You need To select a Procedure", vbInformation, "Unable to show table")
Exit Sub
End If
End If
DGV_Tbl = New DataGridView
With DGV_Tbl
.Name = "DGV_Tbl"
.DataSource = DTemp
Me.Controls.Add(DGV_Tbl)
.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
.RowHeadersVisible = False
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
End With
Dim DGV_H As Integer = 0
Dim DGV_W As Integer = 0
For Each S As DataGridViewRow In DGV_Tbl.Rows
DGV_H += S.Height
Next
DGV_H += DGV_Tbl.ColumnHeadersHeight
'Add more space to include spaces between cells
DGV_H += CInt(DGV_Tbl.Rows.Count * 0.45)
For Each S As DataGridViewColumn In DGV_Tbl.Columns
DGV_W += S.Width
Next
'Add more space to include spaces between cells
DGV_W += CInt(DGV_Tbl.Columns.Count * 0.45)
DGV_Tbl.Height = DGV_H
DGV_Tbl.Width = DGV_W
'Resize the Form
Me.Height += DGV_H + 30
Me.Controls("DGV_Tbl").Location = New Point(15, Me.Height - DGV_H - 30)
End Sub
Your problem is most strange, and since I don't have the code to try and test, my only suggest is that you paste this code into a new form, see if it works. If it does, then add your other control of form 1 into it, run the code again, see if it still work. Repeat the process until you find out what is the cause.
I believe you can find out what is really happening in your form with this. Who know, that new form will probably work.

Verify multiple comboboxes/textboxes have value

I have a winform that has about 6 comboboxes and 3 textboxes. I also have a button that takes the values of aforementioned controls and inserts them into SQL. Is there a better way, than a bunch of nested if/then statements, to verify that the controls all have values before inserting the data? This gets ugly very fast. I have tried googling this answer, but I get too many ASPx answers. I will entertain any ideas you may have. I am just trying to find a better way to do this. Thanks for any help.
I think its not too hard.
This code shows error message about the first empty textbox or combobox. But you can easily modify to show error message for all empty comboboxes and textboxes if required.
Private Function ValidateMyControls() As Boolean
Dim allComboBoxes() As ComboBox = {ComboBox1, ComboBox2, ComboBox3} ' add all your comboboxes here
Dim allTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3} ' add all your textboxes here
Dim emptyTB As TextBox = allTextBoxes.Where(Function(f) f.Text = "").FirstOrDefault
Dim emptyCB As ComboBox = allComboBoxes.Where(Function(f) f.SelectedIndex = -1).FirstOrDefault
If emptyTB IsNot Nothing Then
MessageBox.Show("Please fill value in " & emptyTB.Name)
Return False
ElseIf emptyCB IsNot Nothing Then
MessageBox.Show("Please select a value in dropdown " & emptyCB.Name)
Return False
Else
' All set to go!
Return True
End If
End Function

Datasource for every control in GroupBox

Is something as this even possible. I'm pasting code bellow and hopping anyone could show me the right way to do it.
For Each tbbox As TableLayoutPanel In GroupBox3.Controls
'looping through all controls in my tablelayoutpanle
For Each ctl As Control In tbbox.Controls
If ctl.Name.StartsWith("cb_barva") Then
'im stuck here...
With (ctl)
.DataSource = ds_barve.Tables("moje_barve")
.DisplayMember = "barva"
.ValueMember = "barva"
.SelectedIndex = 0
End With
End If
Next
Next
You need the type conversion
With (ctl)
Convert the ctl to ComboBox
ctype(ctl,ComboBox)
If your not able to convert the control with "With" statement then change each line of your code like below....
ctype(ctl,ComboBox).DataSource = ds_barve.Tables("moje_barve")
ctype(ctl,ComboBox).DisplayMember = "barva"
ctype(ctl,ComboBox).ValueMember = "barva"
ctype(ctl,ComboBox).SelectedIndex = 0

Clearing check boxes in VB.NET

I'm doing an assignment for Uni and in my VB.NET form I have some checkboxes, I'm trying to loop through and clear them (I have a button which will clear the form)
My problem is that there seems to be no property I can use to set the state of a checkbox when not explicitly telling VB which checkbox I want to use. for example, I can go
WineCheckBox.Checked = False
That will check the box, but I wand to DRY the code up a bit and not have to repeat this for each check box I have, this is what I was trying to do:
If TypeOf element Is CheckBox Then
element.Checked = False
End If
I've tried using element.CheckState and element.Checked and both times I get "Checked (or CheckState) is not a member of System.Windows.Forms.Control"
I've looked through all the attributes that I can find for this and none of them seem of use to me...
Am I missing something? or is this just not possible to do
Thanks
EDIT:
this is the whole block of code:
'clear the controls
For Each element As Control In Me.Controls
If TypeOf element Is TextBox Then
element.Text = ""
End If
If TypeOf element Is CheckBox Then
element.Checked = False
End If
Next
What type have you declared element as? If its just a Control then this is a base type for CheckBox that doesn't have the checked property. Maybe try:
If TypeOf element Is CheckBox Then
DirectCast(element,CheckBox).checked = False
End If
How about:
For Each element As Control In Me.Controls
If TypeOf element Is TextBox Then
element.Text = ""
End If
If TypeOf element Is CheckBox Then
Dim chk As CheckBox = CType(element, CheckBox)
chk.Checked = False
End If
Next