Code to show a DataGridView works only if I run another sub before - vb.net

I wrote a code to show/hide content of a certain DataTable into a DataGridView.
DataGridView appears and disappears when I click a button.
I don't get errors but nothing is shown.
But if I click another button (who shows another form) and then go back to the main form, the code works. (??!!??)
I wasn't able to understand what (in the 2nd code) makes the first code to work: it seems there aren't connections between two codes.
EDIT
I made some tests and I can add more informations:
I added a button to show (in a msgbox) the DataGridView properties.
The control was correctly added and all properties are correct.
The property "Visible" is set "True" but the DataGridView is still "invisible".
I added a button that sets DGV_Tbl.Visible = False and DGV_Tbl.Visible = True
And when I click it the DataGridView appears.
But if I click Btn_ShowHideTbl again (to remove DGV) and again (to re-add DGV) DataGridView is still "invisible".
This doesn't happens when I click the button to open the second form and then I close it to go back to the first form.
In this case all works correctly.
I could solve adding DGV_Tbl.Visible = False and DGV_Tbl.Visible = True to the first code but I don't think it's a good idea.
I would like to understand the problem and solve it without "strange instructions".
EDIT 2
I noticed that also the code .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells) doesn't work without opening the 2nd form.
On this istruction DGV_Tbl.Visible = False and DGV_Tbl.Visible = True haven't effect.
EDIT 3
I've done as in the accepted answer, but I posted another question here hoping to understand what's wrong in my code.
This is my code to show/hide DataGridView:
Private Sub Btn_ShowHideTbl_Click(sender As Object, e As EventArgs) Handles Btn_ShowHideTbl.Click
ShowHideTbl()
End Sub
Private Sub ShowHideTbl()
'Look for DGV
Dim DGV_Tbl As DataGridView = Nothing
Try
DGV_Tbl = CType(Me.Controls("DGV_Tbl"), DataGridView)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Try
'If not found I need to show data
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
DGV_Tbl = New DataGridView
'It needs to copy data to another DataTable to show Double as Currency
Using DTemp As DataTable = New DataTable
Dim TblName As String = Me.CBox_ProcType.Text
For C As Integer = 0 To DS_All.Tables(TblName).Columns.Count - 1
DTemp.Columns.Add(DS_All.Tables(TblName).Columns(C).ColumnName, Type.GetType("System.String"))
Next
Dim Arr(DS_All.Tables(TblName).Columns.Count - 1) As String
For R As Integer = 0 To DS_All.Tables(TblName).Rows.Count - 1
For C As Integer = 0 To DS_All.Tables(TblName).Columns.Count - 1
If C = 0 Then
Arr(C) = DS_All.Tables(TblName).Rows(R)(C).ToString
Else
Arr(C) = FormatCurrency(DS_All.Tables(TblName).Rows(R)(C).ToString, 2)
End If
Next
DTemp.Rows.Add(Arr)
Next
'Working on created DataGridView
With DGV_Tbl
.Name = "DGV_Tbl"
'Add control to the Form
Me.Controls.Add(DGV_Tbl)
.DataSource = DTemp
.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
.RowHeadersVisible = False
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
End With
'Dispose the copied DataTable
End Using
'Resizing Form to include new DataGridView
Dim DGV_H As Integer = 0
Dim DGV_W As Integer = 0
For Each R As DataGridViewRow In DGV_Tbl.Rows
DGV_H += R.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 C As DataGridViewColumn In DGV_Tbl.Columns
DGV_W += C.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)
'Align for currency
For x As Integer = 1 To DGV_Tbl.Columns.Count - 1
DGV_Tbl.Columns(x).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Next
Else
'If DGV exists I need to remove it and resize the form
Dim DGV_H As Integer = DGV_Tbl.Height
DGV_Tbl.Dispose()
Me.Height -= (DGV_H + 30)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
This is the code in the button who shows another form (and the code to close the form and go back):
Private Sub Btn_ShowSummary_Click(sender As Object, e As EventArgs) Handles Btn_ShowSummary.Click
Try
If Me.CBox_ProcType.Text = "Select a Procedure" OrElse Me.CBox_ProcValue.Text = "Select a Value" Then
MsgBox("It needs to select a Procedure and a Value",
vbInformation, "Unable to show table")
Exit Sub
End If
Summary = "...Here Some text..." & vbCrLf & Split(Me.CBox_ProcType.Text, ".")(1).Trim & vbCrLf & vbCrLf
Summary &= "...Here Some text..." & Me.CBox_ProcValue.Text & vbCrLf & vbCrLf
Dim C1Wdt% = -50
Dim C2Wdt% = TBox_TotAll.Text.Length
For R As Integer = 1 To 4
Dim CBox_Phase As CheckBox = CType(Me.TLP_Phases.Controls("CBox_Phase" & R.ToString), CheckBox)
Dim TBox_ValPh As TextBox = CType(Me.TLP_Phases.Controls("TBox_ValPh" & R.ToString), TextBox)
If CBox_Phase.Checked Then
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
CBox_Phase.Text, TBox_ValPh.Text) & vbCrLf
Dim TBox_SelVarPh As TextBox = CType(Me.TLP_Phases.Controls("TBox_SelVarPh" & R.ToString), TextBox)
If TBox_SelVarPh.Text = "" OrElse TBox_SelVarPh.Text = "€ 0,00" Then
Summary &= "...Here Some text..." & vbCrLf
Else
Dim SelVarTxt$ = If(Val(TBox_SelVarPh.Text) > 0,
"...Here Some text..." & TBox_SelVarPh.Text,
"...Here Some text..." & TBox_SelVarPh.Text.Substring(1)) & vbCrLf
Summary &= SelVarTxt
End If
End If
Next
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}", "", New String(CChar("_"), C2Wdt)) & vbCrLf
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
"...Here Some text...",
Me.TBox_TotPhases.Text) & vbCrLf
If Me.TBox_PrtAdg.Text <> "€ 0,00" Then
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
"...Here Some text...",
Me.TBox_PrtAdg.Text) & vbCrLf
Summary &= "...Here Some text..." & TBox_PrtRapp.Text & "...Here Some text..." & TBox_CPrt.Text & "...Here Some text..." & vbCrLf
End If
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
"...Here Some text..." & TBox_ForfPercent.Text,
Me.TBox_ForfImp.Text) & vbCrLf
Summary &= "...Here Some text..." & TBox_TotPhases.Text & ")" & vbCrLf
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}", "", New String(CChar("_"), C2Wdt)) & vbCrLf
Summary &= String.Format("{0," & C1Wdt.ToString & "} {1," & C2Wdt.ToString & "}",
"...Here Some text...",
Me.TBox_TotAll.Text) & vbCrLf
Me.Hide()
Me.ShowInTaskbar = False
Frm_Summary.Show()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Here the code of the back button:
Private Sub Btn_CloseNBack_Click(sender As Object, e As EventArgs) Handles Btn_CloseNBack.Click
Frm_Base.ShowInTaskbar = True
Frm_Base.Show()
Me.Close()
End Sub
I don't see any connetion between the codes (but it seems I'm wrong) please show me what I'm missing.

May I suggest another approach for your problem ?
If i were you, i would think about using a drawn datagridview, put datatable content into it before hand.
Then in the show/hide button, just switch the visibility of that datagridview along with recalculating the form size.
As for the currency column, instead of making another table, you can just populate the datatable into your datagridview, then set the format of that column into currency:
DGV_Tbl.Columns("CurrencyColumn").DefaultCellStyle.Format = "c"
More information about datagridview column formatting can be found here

Related

Only column text appears when csv file is imported to datagridview vb.net

This is my first vb.net program, so please bear with me if my question is very mediocre.
Before mentioning my problem, the mechanism of my accounting program is that if the user would input data into the datagridview and export it, so that when he restarts the program, he can import the exported data.
I have imported this .csv file to my datagridview
The problem is that when I have imported it, only column texts would appear like this.
This is the export code that I have used to create the .csv file.
Private Sub ExportToExcelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportToExcelToolStripMenuItem.Click
Dim msg1 = "Export Successful"
Dim title = "Excel Export"
MsgBox(msg1, , title)
Try
Dim iexport1 As String = ""
Dim eexport1 As String = ""
For Each C As DataGridViewColumn In Income.Columns
iexport1 &= """" & C.HeaderText & ""","
Next
iexport1 = iexport1.Substring(0, iexport1.Length - 1)
iexport1 &= Environment.NewLine
For Each R As DataGridViewRow In Income.Rows
For Each C As DataGridViewCell In R.Cells
If Not C.Value Is Nothing Then
iexport1 &= """" & C.Value.ToString & ""","
Else
iexport1 &= """" & "" & ""","
End If
Next
iexport1 = iexport1.Substring(0, iexport1.Length - 1)
iexport1 &= Environment.NewLine
Next
For Each C As DataGridViewColumn In Expense.Columns
eexport1 &= """" & C.HeaderText & ""","
Next
eexport1 = eexport1.Substring(0, eexport1.Length - 1)
eexport1 &= Environment.NewLine
For Each R As DataGridViewRow In Expense.Rows
For Each C As DataGridViewCell In R.Cells
If Not C.Value Is Nothing Then
eexport1 &= """" & C.Value.ToString & ""","
Else
eexport1 &= """" & "" & ""","
End If
Next
eexport1 = eexport1.Substring(0, eexport1.Length - 1)
eexport1 &= Environment.NewLine
Next
Dim tw As IO.TextWriter = New IO.StreamWriter(path:="C:\Users\S2009516\Desktop\JanuaryIncome.CSV")
tw.Write(iexport1)
tw.Close()
Dim tw2 As IO.TextWriter = New IO.StreamWriter(path:="C:\Users\S2009516\Desktop\JanuaryExpense.CSV")
tw2.Write(eexport1)
tw2.Close()
Catch ex As Exception
End Try
End Sub
And this is the one I have used for importing csv file.
Private Sub ImportFromExcelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ImportFromExcelToolStripMenuItem.Click
Dim expenseload1 As String = "C:\Users\S2009516\Desktop\JanuaryExpense.CSV"
Dim incomeload1 As String = "C:\Users\S2009516\Desktop\JanuaryIncome.CSV"
Try
Dim expensereader1 As New StreamReader(expenseload1, Encoding.Default)
Dim incomereader1 As New StreamReader(incomeload1, Encoding.Default)
Dim eline As String = ""
Dim iline As String = ""
Do
eline = expensereader1.ReadLine
iline = incomereader1.ReadLine
If eline Is Nothing Then Exit Do
If iline Is Nothing Then Exit Do
Dim words() As String = eline.Split(",")
Dim words2() As String = iline.Split(",")
Income.Rows.Add("")
Expense.Rows.Add("")
For ix As Integer = 0 To 3
Me.Income.Rows(Income.Rows.Count - 1).Cells(ix).Value = words2(ix)
Me.Expense.Rows(Income.Rows.Count - 1).Cells(ix).Value = words(ix)
Next
Loop
expensereader1.Close()
incomereader1.Close()
Catch ex As Exception
End Try
End Sub
I have no clue on why this is happening... I have followed all the steps in the tutorial video.. Please save my soul... I have been stuck with this for 2 days already.

How to check whether a string value is present in a DataGridView

Im working on a code that has a TextBox, A Button and a Datagrid view.
I want to Display "Data Not Exist" when a value in TextBox is not present on the DataGridView when i press the button.
This is my code so far
If DataGridView1.Rows.Contains(TextBox1.Text) = False Then
MessageBox.Show("Data Not Exist!")
End If
You need to loop through all rows and columns
Dim isFound As Boolean = False
For Each row As GridViewRow In DataGridView1.Rows
for i As Integer = 0 to DataGridView1.Columns.Count -1
If row.Cells[i].Text = TextBox1.text Then
isFound = True
exit for
End If
Next
Next
If (isFound) Then
MessageBox.Show("Data Exists!")
Else
MessageBox.Show("Data Not Exists!")
EndIf
You can do it easily with either using a LINQ or a ForLoop
This code will search all matches it will find across the DataGridView and will prompt in which Row and Column it sees the match.
With a ForLoop, you need to run a loop for Column and for the Row.
Private Sub SearchUsingForLoop()
Dim resultString As String = Nothing
For x = 0 To DataGridView1.ColumnCount - 1
For y = 0 To DataGridView1.RowCount - 1
If DataGridView1.Item(x, y).Value.ToString.ToUpper = txtSearch.Text.ToUpper Then
resultString &= " - Column " & x + 1 & " Row " & y + 1 & vbCrLf
End If
Next
Next
If resultString <> Nothing Then
resultString = txtSearch.Text & " found in : " & vbCrLf & resultString
Else
resultString = "Data does not exist."
End If
MsgBox(resultString)
End Sub
Do remember that index of DatagridViewRow and DatagridViewColumn starts with 0.
Another way of doing this is by LINQ:
Private Sub SearchUsingLINQ()
Dim resultSet = From dgRow As DataGridViewRow In Me.DataGridView1.Rows, _
dgCell As DataGridViewCell In dgRow.Cells _
Where dgCell.Value.ToString.ToUpper = txtSearch.Text.ToUpper _
Select dgCell
Dim resultString As String = Nothing
If resultSet.Count > 0 Then
resultString = txtSearch.Text & " found in :" & vbCrLf
For Each dgCells In resultSet
resultString &= " - Column " & dgCells.ColumnIndex + 1 & " Row " & dgCells.RowIndex + 1 & vbCrLf
Next
End If
If resultString <> Nothing Then
MsgBox(resultString)
Else
MsgBox("Data does not exist.")
End If
End Sub
Feel free to use any of those. But I suggest you to study iterating a DataGridView first.

Displaying Data from SQL to vb.net listbox

I'm trying to display all the golfer's information from TGolfers into a ListBox. But when I run my code it only displays one golfer's information.
Public Class frmGolfers
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim strSelect As String = ""
Dim strName As String = ""
Dim cmdSelect As OleDb.OleDbCommand ' this will be used for our Select statement
Dim drSourceTable As OleDb.OleDbDataReader ' this will be where our data is retrieved to
Dim dt As DataTable = New DataTable ' this is the table we will load from our reader
' open the database
If OpenDatabaseConnectionSQLServer() = False Then
' No, warn the user ...
MessageBox.Show(Me, "Database connection error." & vbNewLine & _
"The application will now close.",
Me.Text + " Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
' and close the form/application
Me.Close()
End If
' Build the select statement using PK from name selected
strSelect = "SELECT * FROM TGolfers "
' Retrieve all the records
cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
drSourceTable = cmdSelect.ExecuteReader
' load the data table from the reader
dt.Load(drSourceTable)
' populate the text boxes with the data
lbxDisplay.Items.Add(dt.Rows(0).Item(1).ToString & "," & " " & dt.Rows(0).Item(2).ToString &
ControlChars.CrLf & " " & dt.Rows(0).Item(3).ToString & " " & dt.Rows(0).Item(4).ToString & " " & dt.Rows(0).Item(5).ToString & " " & dt.Rows(0).Item(6).ToString)
' close the database connection
CloseDatabaseConnection()
End Sub
End Class
After loading the DataTable start a loop
......
' load the data table from the reader
dt.Load(drSourceTable)
' populate the text boxes with the data
For Each row in dt.Rows
Dim text = row(1).ToString & ", " & _
row(2).ToString & ControlChars.CrLf & " " & _
row(3).ToString & " " & _
row(4).ToString & " " & _
row(5).ToString & " " & _
row(6).ToString
lbxDisplay.Items.Add(text)
Next
......
In this way you loop over each row present in the dt.Rows collection, extract the information needed, build a string and add the string to the ListBox Items collection.
Notice that I have followed your indexing starting from Index = 1 for each column in the DataRow but keep in mind that in NET the array indexing start at index 0 not 1, so the first column of your datarow is at index 0. Change the index in code accordingly to your need. (This means that if you have 6 columns in that table then the indexing should go from 0 to 5, using the index 6 in this scenario gives an Index out of Range exception)

Change text in a TextBox using a Loop

I need to change some lines in a TextBox using a For loop. The problem is that when I run the following code I get an IndexOfOfRangeException.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For counter As Integer = 0 To TextBox1.Lines.Length = -1
TextBox1.Text = "some text" & "(" & """" & TextBox1.Lines(counter) & """" & ")" & vbCrLf
Next
End Sub
First
For counter As Integer = 0 To TextBox1.Lines.Length = -1
should be
For counter As Integer = 0 To TextBox1.Lines.Length -1
Otherwise it evaluates the part after To as a boolean.
Second
TextBox1.Text = "some text" & "(" & """" & TextBox1.Lines(counter) & """" & ")" & vbCrLf
you are setting the complete text of the textbox to this new string instead of just changing one line.
The easiest way to do this task would be to copy the lines of the textbox to a string array, change the strings in that array and copy it back.
Dim tempArray as String()
tempArray = TextBox1.Lines
For counter = 0 to tempArray.Length -1
tempArray(counter) = "some text" & "(" & """" & tempArray(counter) & """" & ")" & vbCrLf
Next
TextBox1.Lines = tempArray

Selecting ListBox in MS Access

I need a small help dear people. I have a form where I have listbox and with selecting listbox I populate some fields and subform in the form.
What I`m trying to achieve is that the subform should not be with 0 records.
So I have a control where is checking if the recordset is 0 and is pop up a message but exit sub is not helping and user can stil get to the next record.
Private Sub lstRev_BeforeUpdate(Cancel As Integer)
Dim DataConn10 As New ADODB.Recordset
Dim Comm10 As String
Set Conn = CurrentProject.Connection
Comm10 = " SELECT tblLIVE.SID " & _
" FROM tblLIVE " & _
" WHERE tblLIVE.CID = " & Me.txtCID & " And tblLIVE.PID =
" & Me.txtPIDRev & " And tblLIVE.MNumber = '" & Me.txtSMNum & "'"
DataConn10.Open Comm10, Conn, adOpenKeyset, adLockOptimistic
If DataConn10.RecordCount = 0 And Not IsNull(Me.txtMIDRev) Then
Dim x As Integer
x = MsgBox("Are sure that you want to leave the form without adding Line in subform. If you press yes Rev will be deleted. If you press No please enter Line", vbYesNo)
If x = vbYes Then
MsgBox "Delete"
Else
MsgBox "EnterSOV"
'Here I need something to tell him to stay in the same record :(
DataConn10.Close
Exit Sub
End If
End If
End Sub
You can add Cancel=True to prevent the before update method from finishing. Just add this before your Exit Sub.