Adding New Row and inserting data to row - vb.net

I have an existing datatable with two columns predefined.
Example:
Column 1 = Name
Column 2 = Surname
No rows are currently in my data table.
Datatable.Rows.Add - Will add a row to my collection
Now I want to write to the current row added and column 1 (Name) with text "Bob" then write to current row and column 2 (Surname) with text "smith"
I can't seem to figure out how to do this. Any help is greatly appreciated.

You can add a DataRow with the specified information :
Dim dtTable As New DataTable
'create a new data row.
Dim drNewRow As DataRow = dtTable.NewRow
'specify the information of the new row.
drNewRow("Name") = "Bob"
drNewRow("Surname") = "Smith"
'add the new row with specified information to the table.
dtTable.Rows.Add(drNewRow)

Related

DataTable And Two Row Sorting

So the boss comes to me and says "I want the value of each agent and the project on one line and the average of all the other agents on the next line so I can easily see if they are above or below average."
the table looks like this:
dt.Columns.Add("AGENT", GetType(String))
dt.Columns.Add("PROJECT", GetType(String))
dt.Columns.Add("Sales", GetType(Integer))
dt.Columns.Add("Declines", GetType(Integer))
dt.Columns.Add("Margin", GetType(Integer))
Ok its all good. One row in the datatable is the agent and project. The next row is the average of all the other agents and project like so:
row 1:
John Smith,
ProjectName,
(other column values)
row 2:
John Smith,
ProjectName & " AVERAGE/TOTAL",
(other column values)
The project name is removed in the SSRS report on the AVERAGE/TOTAL line because of space constraints on the piece of paper it is printed on.
I do the sorting by our standard way of sorting a datatable.
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Return dt1
But now the boss has a new requirement later on. He wants to be able to sort by other columns in the table but keep the two rows the (agent/project and the agent/project AVERAGE/TOTAL) together. So in essence he wants to be able to sort not by one row but the two rows together but the sort value could be "AGENT,Margin". Obviously to keep the two rows together I have to find a way to sort the Project value too.
So I am stumped and would appreciate any thoughts you might have. C# ideas are welcome as well.LINQ is fine but it is going to have to become a datatable.
so you create two tables. One with the row values one one with the average values. Loop through the row values and then do another loop inside that loop to match project names. Its a hack but it worked.
If SortValue = "Default" Then
dt1.Merge(dt)
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Else
Dim dataView As New DataView(dt)
dataView.Sort = SortValue
dt = dataView.ToTable
Dim dtCopy As New DataTable
dtCopy = dt.Clone
For Each row As DataRow In dt.Rows
dtCopy.ImportRow(row)
For i = 0 To dt1.Rows.Count - 1
If dt1.Rows(i).Item("PROJECT").ToString.Replace(" AVERAGE/TOTAL", "") = row.Item("PROJECT") And dt1.Rows(i).Item("AGENT") = row.Item("AGENT") Then
dtCopy.Rows.Add(dt1.Rows(i).Item("AGENT"), dt1.Rows(i).Item("PROJECT"), dt1.Rows(i).Item("SALES"), dt1.Rows(i).Item("Declines"), dt1.Rows(i).Item("Margin"))
End If
Next
Next
dt1 = dtCopy
End If

How to search in and edit Table by using DataGridView

I have a SQL database with a table "Employees" in it (with large number of rows). By using DataGridView, I want to search for specific "Employee's Name" and change it's "Job". How can I achieve that. I'm using VB.net. Please Help Me.
Not sure if this will help but write a loop that goes through all the values if it finds a match its true if not it is false ,if found the item can be displayed in a textbox and edited
if not a message is displayed saying "no match found"
the editing part can be done using a procedure that will update the value in your grid with what is entered
i can supply code for this if need be but i am unsure if this is what you wish
and there is most likely a better way of doing it
you can loop through your grid, and check if the data you wish to edit exists using the For loop:
Supposing you are using a textbox as the input, and you use a label to hold the employee ID:
Dim EmpIDColumn as Integer = 'array number of your EmpID Column
Dim EmpNameColumn as Integer = 'The array number of the column where your EmpName is
Dim JobColumn as Integer = 'The array number of your job column
For each dr as Datagridviewrow in Datagridview1.Rows
If dr.cells(EmpNameColumn).value = TxtSearchBox.text Then
txtEmpJob.text = dr.cells(JobColumn).value
lblEmpID.Text = dr.cells(EmpIDColumn).value
End if
Next
Okay, so you've searched the record successfully. Next step (after editing the job, and even other details like the name) would be to update the record in the grid. Remember you set the lblEmpID's text to empID in the column? Use it to find the
Record you wish to change in the grid using the same technique above!
Dim EmpIDColumn as Integer = 'array number of your EmpID Column
Dim EmpNameColumn as Integer = 'The array number of the column where your EmpName is
Dim JobColumn as Integer = 'The array number of your job column
For each dr as Datagridviewrow in Datagridview1.Rows
If dr.cells(EmpIDColumn).value = lblEmpID.text Then
dr.cells(JobColumn).value = txtEmpJob.text
dr.cells(EmpNameColumn).value = txtEmpName.text
'then, type in here your SQL Query Update!
End if
Next

VB.Net Fill tables in dataset by data variables

Hi I need to fill a datatable(in data set) row by row by providing the the data by variables in VB.Net. The variables assigns its value by loop.. so the datatable row should be filled row by row until the loop ends. There are three columns in the table. so the table should fill with different kind of datatype variables. pls help.. I need the piece of code...
Sample code to do it:
Inserting a New Record into an Untyped Dataset
Dim newEmployeeRow As DataRow = DtEmployee.Tables("Employee").NewRow()
newEmployeeRow ("EmpNo") = value
newEmployeeRow ("EmpName") = value
newEmployeeRow ("Commission") = value
DtEmployee.Tables("Employee").Rows.Add(newEmployeeRow )
Inserting a New Record into a Typed Dataset
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)

Editing current data in a data table

I am using following data to add new data to my table
Me.TblResultTableAdapter.Fill(Me.DbResultDataSet.tblResult)
Dim drNewRowEditSummary As DataRow
drNewRowEditSummary = DbResultDataSet.tblResult.NewRow
drNewRowEditSummary.Item(1) = "Test"
DbResultDataSet.tblResult.Rows.Add(drNewRowEditSummary)
TblResultTableAdapter.Update(DbResultDataSet.tblResult)
How can I edit my data using similar method as above?
Thanks
Furqan
DataRowCollection has a Find-Method that returns the DataRow with the given primary key(s). So you should define a pk column, then you get the row with:
Dim drResult As DataRow = DbResultDataSet.tblResult.Rows.Find(1) 'if your pk is 1'
drResult.Item(1) = "Test2"
TblResultTableAdapter.Update(DbResultDataSet.tblResult)
Btw, the same way if you want to delete this row: drResult.Delete()

How can I update a row in a DataTable in VB.NET?

I have the following code:
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count Step 1
strVerse = blHelper.Highlight(dtResult.Rows(i).ToString, s)
' syntax error here
dtResult.Rows(i) = strVerse
Next
I want to add a strVerse to the current row.
What am I doing wrong?
The problem you're running into is that you're trying to replace an entire row object. That is not allowed by the DataTable API. Instead you have to update the values in the columns of a row object. Or add a new row to the collection.
To update the column of a particular row you can access it by name or index. For instance you could write the following code to update the column "Foo" to be the value strVerse
dtResult.Rows(i)("Foo") = strVerse
You can access columns by index, by name and some other ways:
dtResult.Rows(i)("columnName") = strVerse
You should probably make sure your DataTable has some columns first...
Dim myRow() As Data.DataRow
myRow = dt.Select("MyColumnName = 'SomeColumnTitle'")
myRow(0)("SomeOtherColumnTitle") = strValue
Code above instantiates a DataRow. Where "dt" is a DataTable, you get a row by selecting any column (I know, sounds backwards). Then you can then set the value of whatever row you want (I chose the first row, or "myRow(0)"), for whatever column you want.