Add multiple values to specified column in datagridview

Dwhite

Member
Joined
Nov 10, 2020
Messages
5
Programming Experience
1-3
Good evening all,

I am looking to find a way to add values to a single column in a datagridview column. Basically i would like to read the the values in one column and input the ip address in another column. please see parcial code below.. please let me know if you are able to assist.

Try
For Each DRow As DataRow In TestbaseDataSet.TestDB.Rows

Dim con As New SqlConnection
Dim cmd As New SqlCommand


Dim hname As IPHostEntry = Dns.GetHostByName(DRow.Item("Hostname").ToString())
Dim ip As IPAddress() = hname.AddressList
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
You ask about a DataGridView but then your code makes no mention of it and refers only to a DataTable. Are you actually saying that you want to process the rows of a DataTable, getting the host name from one column and setting the corresponding IP address in another? If so, does the IP address column already exist in the DataTable? If not, did you intend to use an unbound column in the grid?
 

Dwhite

Member
Joined
Nov 10, 2020
Messages
5
Programming Experience
1-3
You ask about a DataGridView but then your code makes no mention of it and refers only to a DataTable. Are you actually saying that you want to process the rows of a DataTable, getting the host name from one column and setting the corresponding IP address in another? If so, does the IP address column already exist in the DataTable? If not, did you intend to use an unbound column in the grid?
my apologies. yes i would like to process the rows in one column(hostname) and set the corresponding IP address in another. the IPAddress column is apart of the datatable. Please see below



'adds the IPaddress to the ipaddress column

DataGridView1.Rows(0).Cells(2).Value = ip(0).ToString
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
If you want to display data in a DataGridView from a database then you ought to be populating a DataTable first and then binding that to the grid via a BindingSource. The changes should be made to the DataTable before binding:
VB.NET:
For Each row In table.AsEnumerable()
    Dim hostName = row.Field(Of String)("HostName")
    Dim ipAddress = Dns.GetHostEntry(hostName).AddressList.First()

    row("IPAddress") = ipAddress.ToString()
Next
 

Dwhite

Member
Joined
Nov 10, 2020
Messages
5
Programming Experience
1-3
If you want to display data in a DataGridView from a database then you ought to be populating a DataTable first and then binding that to the grid via a BindingSource. The changes should be made to the DataTable before binding:
VB.NET:
For Each row In table.AsEnumerable()
    Dim hostName = row.Field(Of String)("HostName")
    Dim ipAddress = Dns.GetHostEntry(hostName).AddressList.First()

    row("IPAddress") = ipAddress.ToString()
Next
Thank you so much. This was a big help. I'm assuming if i want other changes in columns it would be he same thing?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
I'm assuming if i want other changes in columns it would be he same thing?
The row variable gives you access to the current DataRow so you can do whatever you want with and to that row.
 

Dwhite

Member
Joined
Nov 10, 2020
Messages
5
Programming Experience
1-3
The row variable gives you access to the current DataRow so you can do whatever you want with and to that row.
Thanks for all your help. 1 last question, I'm looking to scan 1 column for a particular value and then go to a different column ( same row) and run a try catch statement , could i still use the "for each row" statement?. For example if i have 2 columns (A,B) and i want to scan column B for a room number (lets say room 2) then get the value in the same row but in Column A and run a try catch statement with the value in column A . Or is there a more efficient way to do this?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
A For Each loop over the rows of the table allows you to perform an action for each row in the table. Do you want to perform an action for each row in the table? If so then a For Each loop can be used and is almost certainly the best way to do so. What the action is is irrelevant to the loop itself.
 
Last edited:

Dwhite

Member
Joined
Nov 10, 2020
Messages
5
Programming Experience
1-3
A For Each loop over the rows of the table allows you to perform an action for each row in the table. Do you want to perform an action for each row in the table? If so then a For Each loop can be used and is almost certainly the best way to do so. What the action is is irrelevant to the loop itself.
Yes I would like to perform an action for each row. Something like For each row containing “room 2” , take the value in cell(1) and do this try catch statement.
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
I've already shown you how you can perform multiple operations on the row inside the loop so I don't really understand what you're issue is. In my code example, row is a variable like any other, so you can use it like any other variable.
 
Top Bottom