adding characters into a datagrid in vb.net - vb.net

I'm new to vb.net. I would like to know how i could i/p a string via textbox and display its individual characters into a datagrid view in diff rows?

Are you only using a single column in the datagridview? if so you could do this easily by stepping through each character in the string and adding a row containing that character... see the code example below
InputString = TextBox1.Text
For a = 0 To InputString.Length - 1
Datagridview1.Rows.Add(InputString(a))
Next
Cant see why you'd want to do this though - If this isn't what you're trying to accomplish please give us more details

Related

how to add certain number of blank lines into multiline textbox in vb .net?

Normally, a multi-line textbox does not contain any lines.
But I want to add a certain number of blank lines to the text box.
I have no idea how I can do this.
The Lines property of a TextBox is an array containing the lines of text. Just assign to it a String array of the appropriate length:
TextBox1.Lines = New String(lineCount - 1) {}
I haven't tested but I wouldn't expect that you would even have to set the elements, i.e. an array full of Nothing will do the job.

convert text to single type

Dim fontsize As Single = CSng(SynopsisTSCmbFontSize.Text)
rtbSynopsis.Font = New Font(SynopsisTSCmbFonts.Text, fontsize)
to change the fontsize to the value selected in a combo box, the value has to be of the Single type.
the combobox is populated with numbers entered at the design mode, ranging from 7-78. I know that these are entered as strings.
the error is :
I have tried a number of things to convert the text (which are numbers, no letters) from the combobox to single to no avail. try parse did not work, trimming did not work, first convert to INT or DBL, then to SNG did not work.
What is the correct syntax here?
I would have thought that it was pretty standard stuff to change the fontsize.
I found a solution : instead of populating the combobox at design time, I populated it at runtime where I had full control over the type.
Dim i As Single
For i = 5 To 70
SynopsisTSCmbFontSize.Items.Add(i)
TreatmentTSCmbFontSize.Items.Add(i)
Next
once the comboboxes are correctly populated, I can run the rest of the code with no errors
thank you all for your time!

Select & Assign Specific Row of a ListBox to a Variable As String

I have tried to find an answer to this but have had no luck. I am using VB.NET and VisualStudio 2019
I have a listbox (lboxsectionnames)that has several different section names listed (as shown in the screenshot). I am trying to specify a specific row, and assign that row to a variable as text.
For this instance I am trying to get the first row text, however, I would like to be able to specify row # in future and get text as well.
Dim firstSectionName
lboxSectionNames.GetItemText(0)
firstSectionName = lboxSectionNames.SelectedIndex.ToString
ListBox Text Example
ListBox.Items is an ObjectCollection and can be accessed with your index like this
lboxSectionNames.Items(0).ToString()
or using SelectedIndex like this:
lboxSectionNames.Items(lboxSectionNames.SelectedIndex).ToString()
although you could also do the above like this:
lboxSectionNames.SelectedItem.ToString()

Format or pad a number column in DataGridView control (DefaultCellStyle.Format)

I have a DatagridView control on my form.
The first column is a Record ID which can be between 3 and 5 digits on the db, but should be formatted on the front end as 5 digits with padded zeros/leading zeros.
So it looks like 901 and I want it to look like 00901
I looked at
MSDN Library: How to: Format Data in the Windows Forms DataGridView Control
and
MSDN Library: How to: Customize Data Formatting in the Windows Forms DataGridView Control
and I tried:
dgvDeletedRecords.Columns(0).DefaultCellStyle.Format = "00000"
in my form load event after the data is loaded into the grid
But it doesn't work.
Where am I going wrong please?
Try this number format "#####00000"
Code for testing I had used:
Me.RichTextBox.Text &= String.Format("{0:#####00000}",123)
Me.RichTextBox.Text &= Environment.NewLine
Me.RichTextBox.Text &= String.Format("{0:#####00000}",12345)
Tested in RichTextBox, but sure a same format will working for DefaultCellStyle.Format
dgvDeletedRecord.Columns(0).DefaultCellStyle.Format = "#####00000"
In format sting "#"-sign tell how mush number will be show(prefix zeros not shown)
Then "0"-sign tell how mush zeros will be shown, and here prefix zeros will be showed
MSDN Number Formats
try the following;
dgvDeletedRecord.Columns(0).DefaultCellStyle.Format = "{0:N4}"

ListBox or Combobox ADODB imported Decimal items invisible in the list

I have encountered this problem several times already and have been able to work around it till now. Also the almighty search engines didn't help me.
The problem is that when I have populated a listbox or combobox from a ADODB recordset all Decimal data elements are not visible in the box, for example with the following (conn is a ADODB connection):
Private Sub GetFilteredRecords()
Dim strSQL As String
Dim arr As Variant
'create the SQL
strSQL = "SELECT * FROM vwStandard_Fee2"
'execute the SQL and fill the rs ( rsFiltered )
Set rsFiltered = conn.Execute(strSQL)
'Apply recordset to the listbox on the form
If Not (rsFiltered.EOF = True And rsFiltered.BOF = True) Then
arr = rsFiltered.GetRows()
With lbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
With cbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
End If
End Sub
Above contains 6 columns of Ids (all show Type = Variant/Decimal), of which the containing values are all not "shown" for some strange reason. Only the String and Date columns are shown normally, the Decimals are there but empty!
Here some snippets:
Now in case of a combo box I can get one column's value shown if their column the BoundColumn when I select that listitem, but only in the value fo the combobox (so still not in the list).
My initial workaround was to convert them into String values before adding to the Listbox/Combobox, in this case however I want to directly link the query result to the Box.List without looking at the details. And thus I am looking for a solution in stead of a work around.
In short: my numerical field items are invisible BY DEFAULT for some strange reason. Workaround was to make the items String values. I am now looking for a solution for this bug/problem instead:
What is causing this?
How to solve it?
So all string data is appearing? And,only numerics don't appear?
Then you may want to convert your numerics to strings and pass it to your list, combo boxes.
Which you have already done I noticed.
Now for any reason if your max number of rows and length of array/recorders row count doesn't match it could also cause an issue. However it seems your setting rows of combobox using recordset row count. Instead of using an array can you try to iterate over the recordset to populate the combobox? yes this is not performance friendly, buy guess what we need it to work without bugs before optimizing. ;-)
Have you bound your combobox to the recordset? Can you confirm if your array is single dimension and it has data to feed to the box?
You may try to populate the listbox using a saved query in the DB to if the issue still persists.
However, list boxes and combo boxes based on SQL statements are slower than
list boxes and combo boxes based on saved queries.
So can you try the following to set rowsource property? Make sure to test on both number,and test columns. As well as on old combo box and new one.
Rowsource->build query->
sqlview copy to rowsource property box->
delete or don't save that above built query since you already have SQL statement.
Just wanted you to try out possibilities to narrow down the issue.
UPDATING ANSWER WITH MOST POSSIBLE ISSUE AND SOLUTIONS
As per my comments, they mainly given assuming you had issues populating listbox/combobox
I forgot to ask something very very important, have you declared
Option Base 1 to make sure to avoid losing one of the array's column
values if you are dumping 2D array...? because you do not have any
explicit declartion for the array you are using to dump data into the
listbox.......... :)
Make sure your Listbox is enabled to show multi column data.
*So you have three choices, *
Option Base 1
ReDim your array and do looping to fill it and dump it into .list.
Since ReDim array need you to anyway loop through, you may just as well
use the recorset iself to add the data.
You seem to have a dimension issue with the array which is not declared but transposed from recordset and then to listbox/combobox. So your undeclared array is not populating multi-columns properly. That could be the reason it works when you declare array proeprly.......
Infact in your comment you have said so,
When I create an array in my code and populate it in my code (entry by
entry) it will show without any problem – K_B 14 mins ago
OK after going through various possible causes it seems to be the case that:
VBA has no Decimal Variant Type of its own.
VBA can handle Decimal from within a variable declared as Variant (thus becoming a Variant/Decimal)
This normally doesn't stop your program from working, but in Controls like Listbox and Combobox the Type Variant/Decimal is not interpretable and thus wont draw that specific entry.
For example populate a listbox called lbHigher with this:
Private Sub ListBoxProblem()
Dim tempArray(2, 2) As Variant
tempArray(0, 0) = "A"
tempArray(0, 1) = 1
tempArray(0, 2) = 1.1
tempArray(1, 0) = "B"
tempArray(1, 1) = CStr(CDec(5.2))
tempArray(1, 2) = 2.3
tempArray(2, 0) = "C"
tempArray(2, 1) = DateSerial(2012, 12, 13)
tempArray(2, 2) = 100
tempArray(3, 0) = "D"
tempArray(3, 1) = -1
tempArray(3, 2) = CDec(5.2)
lbHigher.ColumnCount = 3
lbHigher.List = tempArray
End Sub
Everything works fine except for the CDec(5.2). The CStr(CDec(5.2)) works fine as well as VBA will first have converted the Decimal to String before the Listbox gets to get it.
So either: Dont let the SQL generate any Decimal output OR convert any Decimal output to Single/Double/String/Integer/Long in VBA before handing it to the Listbox.