This is a simplifed verson of my code
Dim dt As New DataTable()`
dt.Columns.Add(New DataColumn("Year 0 ")
dt.Columns.Add(New DataColumn("Year 1")
dt.Columns.Add(New DataColumn("Year 2 ")
dt.Columns.Add(New DataColumn("Year 3")
dt.Columns.Add(New DataColumn("Year 4 ")
dt.Columns.Add(New DataColumn("Year 5")
dt.Columns.Add(New DataColumn("Total")
dt.Rows.Add("Net Economic Benefit")
Which creates a table like this (ignore the other rows)
My question is, is there a way for me to add to the row "Net Economic Benefit" ?
The code should basically do what the following code does
dt.Rows.Add("Net Economic Benefit", "1", "2", "3")
Except I was to create a loop to add the "1", "2", "3" so I cannot use that code as it will create multiple rows.
Code might look something like:
dt.Rows.Add("Net Economic Benefit")
dt.Rows("Net Economic Benefit").AddRow("1")
Basically it refers to the already added row "Net Economic Benefit" and then it adds the "1", "2", and "3"
Once you have an existing DataRow, however you may have created it, you set the fields by indexing the row, either by column name or ordinal:
Dim row As DataRow
'...
row("SomeColumn") = someValue
row(index) = someOtherValue
Related
I'm trying to generate a dummy data to test my Reporting Application. So far I have something this,
Dim list As List(Of Object)
Dim random As System.Random = New System.Random()
For i As Integer = 1 To 100
Dim obj As New Object
If random.Next(0, 3) > 0 Then
obj.Country = "China"
Else
obj.Country = "Japan"
End If
...
list.add(obj)
Next
This working nicely. However I want to be able to random more Country, say 5 (China, Japan, South Korea, North Korea, Taiwan) with probability 0.4 : 0.3 : 0.1 : 0.1 : 0.1 respectively.
I know I can just extend the If statement above, but I really want avoid messy code since the purpose is only to generate dummy data.
While the current above code may can be simplified using Ternary Operator, how can I do that if I need to random between 5 values? Is there any other choice other than If - Else statement?
Urm, the simplest solution I can think so far is:
Dim list As List(Of Object)
Dim random As System.Random = New System.Random()
Dim Countries() As String = {"China", "China", "China", "China", _
"Japan", "Japan", "Japan", _
"South Korea", _
"North Korea", _
"Taiwan"}
For i As Integer = 1 To 100
Dim obj As New Object
obj.Country = Countries(random.Next(0, 10))
...
list.add(obj)
Next
This question already has answers here:
How can I delete an item from an array in VB.NET?
(12 answers)
Closed 8 years ago.
I have below variable.
Dim strValues As String() with values "1", "2", "3", "4", "5", "6", "7"
I want to delete "4" from StrValues based on the string index without knowing the string value in vb.net. How can I do this?
I'd use Linq for simplicity:
Dim strValues = {"1", "2", "3", "4", "5", "6", "7"}
strValues = strValues.Where(Function(s) s <> "4").ToArray 'Removes all instances
Better yet, use a list:
Dim strValues = {"1", "2", "3", "4", "5", "6", "7"}.ToList
strValues.Remove("4") 'Removes first instance of "4" only
If you want to do it by value at index you could do something like the following (though these will remove all instances of the value):
Dim index = 3
strValues = strValues.Where(Function(s) s <> strValues(index)).ToArray
or
strValues = strValues.Except({strValues(index)}).ToArray
To remove or skip a given index (single instance only), you could do something like this:
Dim index = 3
strValues = strValues.Take(index).Concat(strValues.Skip(index + 1)).ToArray
Dim strValues As String() = New String() {"1", "2", "3", "4"}
Dim strList As List(Of String) = strValues.ToList()
strList.Remove("4")
strValues = strList.ToArray()
Depending on what you mean by "delete", you could do a couple of things. First, if you're for a dynamically sized array, you may want to look into the ArrayList or List(of T). This class allows you to add and delete objects to an array while resizing for you if necessary. However, if you literally want to delete it from your array, you can set the value to 0 or Nothing or shift all the values in the array after it down one index.
I have data that is stored in a 2-Dimensional List that I want to print to the console window where it is all lined up properly.
Example:
Dim aList As New List Of(List Of String))
aList = AfunctionThatFetchesData
aList
{column 1} {column 2} {column 3}
This is some data 0 3
Some more 1 3
One more 2 3
Check out the documentation for Console.WriteLine, where you will see that it uses the composite formatting feature, which support alignment parameters. So, you can align things using, e.g.
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{column 1}", "{column 2}", "{column 3}")
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "This is some data", 0, 3)
which results in:
{column 1} {column 2} {column 3}
This is some data 0 3
Adjusting the spacing and alignments in the format string will get you what you want.
If you want the user to be able to manually enter data into a table:
Console.Write("Enter Data For Column 1: ")
Dim Data1 As String = Console.ReadLine
Console.Write("Enter Data For Column 2: ")
Dim Data2 As String = Console.ReadLine
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{Data Type}", "{Column 1}", "{Column 2}")
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "Data Entered:", Data1, Data2)
Console.WriteLine("ENTER To Exit: ")
Console.ReadLine()
It should look like this (Click Me).
hi i am trying to get index of row through its value . i tried this code but its not working is there any way where i can get indexof row by its value.
Dim dgr As New DataGridViewRow()
dgr.CreateCells(DataGridView1, New Object() {"name", "DFD", "SDFDF"})
DataGridView1.Rows.Add(dgr)
txtNameIndex.Text = DataGridView1.Rows.IndexOf(dgr)
i want to add value and remove value by check box when the check box is checked then the value should be added in datagridviewd and when same check box is unchecked the value should be removed from datagridview
If chkbox.Checked = True Then
row = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Add(row)
Else
Value = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Remove(Value)
End If
This is how you get an index:
Dim i as Integer = DataGridView1.Rows.Add(dgr)
Or
Dim i as Integer = dgr.Index
For your checkbox code I recommend the top one since you aren't creating an object for the DataGridViewRow.
hi this is my code to get datable from query but i need to check for some values like if there is 0 in any column then replace it with N.A.
Dim op As New cad
Dim dt As DataTable = op.Fetchbooks().Tables(0)
Dim sb As New StringBuilder()
Dim rp As System.Data.DataRow
If dt.Rows.Count > 0 Then
ListView1.DataSource = dt
ListView1.DataBind()
DropDownList1.DataSource = dt
DropDownList1.DataTextField = "Description"
DropDownList1.DataValueField = "Description"
DropDownList1.DataBind()
End if
so is there any way to check some values and edit in datable before binding ???
First, if possible i would modify your sql-query in Fetchbooks instead to replace 0 with N.A.. I assume you are querying the database.
However, if you want to do it in memory:
For Each row As DataRow In dt.Rows
For Each col As DataColumn In dt.Columns
If row.IsNull(col) OrElse row(col).ToString = "0" Then
row(col) = "N.A."
End If
Next
Next
Sure: you can iterate through the table, making whatever value replacements are appropriate (assuming that your replacement values are type-compatible with what came from the query).
Something like:
For Each row as DataRow in dt.Rows
if row("columnname") = "left" then
row("columnname") = "right"
Next
Then bind it to the grid, and you should see your updated values.
Once you have the DataTable object you can manipulate it as you need:
For Each rp In dt.Select("COLUMN_A = '0'")
rp("COLUMN_A") = "N.A."
Next
Note that this assumes that COLUMN_A is defined as a string type, not a numeric.
The challenge will be if you intend on saving this data back to it's source and you don't want the original 0 value to be saved instead of N.A.. You could add dt.AcceptChanges immediately after the above loop so that it will appear as the Fetchbooks query had these values all along.