We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

vb.net vs2010 Insert data into daragridview

Medium Priority
621 Views
Last Modified: 2013-11-26
Hi there, I need help with inserting data into the datagridview.

Column 1 will be URL and column 2 will be status.


Protected Function ProcessLinks(ByVal txt As String) As String
        Me.Cursor = Cursors.WaitCursor
        Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
        Dim mactches As MatchCollection = regx.Matches(txt)
        For Each match As Match In mactches
            txt = match.Value
            If txt.Contains("sometext.com") Then
                Dim resp As String = SendRequest("performtest.php?u=" & txt)
                If resp = "AVAILABLE" Then
                   'insert into datagridview txt = url status = available.
                ElseIf resp = "UNAVAILABLE" Then
'same as above, but status unavailable
                Else
'same as above but status = error
                End If
            End If
        Next
        Return txt
        Me.Cursor = Cursors.Default
    End Function

Open in new window

Comment
Watch Question

Commented:
you can have your datagrid bound to a table and add a new row when you get your atatus / test

e.g with table StatusTable having 2 columns (url and status), you can have:

Dim xRow As DataRow
xRow = StatusTable.NewRow()
If resp = "AVAILABLE" Then
  'insert into datagridview txt = url status = available.
   xRow(0) = txt
   xRow(1) = "Available"
ElseIf resp = "UNAVAILABLE" Then
'same as above, but status unavailable
   xRow(0) = txt
   xRow(1) = "Unavailable"
Else
'same as above but status = error
   xRow(0) = txt
   xRow(1) = "Error"
End If
StatusTable.Rows.Add(xRow)

Open in new window

Commented:
Indeed, you can use the shorthand VS2010 syntax of:

Dim xRow As DataRow
xRow = StatusTable.NewRow()
xRow(0) = txt
xRow(1) = If(resp = "AVAILABLE", "AVAILABLE", If(resp = "UNAVAILABLE", "UNAVAILABLE", "Error"))
StatusTable.Rows.Add(xRow) 

Open in new window

Author

Commented:
Error      1      'NewRow' is not a member of 'System.Windows.Forms.DataGridView'.      D:\Developer\Visual Studio Projects\E3MCWP_LinkChecker\E3MCWP_LinkChecker\Form1.vb      41      24      E3MCWP_LinkChecker

Commented:
that was an example  WITH a table named StatusTable.

Add this to the top of your form1

Public WithEvents StatusTable As New DataTable

Open in new window


and this to your form load

xTable.Columns.AddRange({New DataColumn With {.AllowDBNull = True, .ColumnName = "Category Headers", .DataType = System.Type.GetType("System.String")}, New DataColumn With {.AllowDBNull = True, .ColumnName = "Category Headers", .DataType = System.Type.GetType("System.String")}})

Open in new window


then add the code I gave before into your routine, ie

Dim xRow As DataRow
xRow = StatusTable.NewRow()
xRow(0) = txt
xRow(1) = If(resp = "AVAILABLE", "AVAILABLE", If(resp = "UNAVAILABLE", "UNAVAILABLE", "Error"))
StatusTable.Rows.Add(xRow)

Open in new window

Commented:
the second code suggestion above should read

StatusTable.Columns.AddRange({ ....

Open in new window

Author

Commented:
and how to display that datatable?

Commented:
just add the table as the datasource of the datagridview after the line

StatusTable.Columns.AddRange({ .... 

Open in new window


add

Me.DataGridView1.DataSource = StatusTable

Open in new window


assuming your datagridview is called DataGridView1
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Got an error when launching application Error

Commented:
Ibelieve you managed to solve this since you have awarded points, if not, you need to change the line:

StatusTable.Columns.AddRange({New DataColumn With {.AllowDBNull = True, .ColumnName = "Category Headers", .DataType = System.Type.GetType("System.String")}, New DataColumn With {.AllowDBNull = True, .ColumnName = "Category Headers", .DataType = System.Type.GetType("System.String")}})

to

StatusTable.Columns.AddRange({New DataColumn With {.AllowDBNull = True, .ColumnName = "URL", .DataType = System.Type.GetType("System.String")}, New DataColumn With {.AllowDBNull = True, .ColumnName = "Status", .DataType = System.Type.GetType("System.String")}})

simple enough to spot, you'd think .....

Author

Commented:
Yea I got that one :)

Commented:
And having looked at your code again, you could further reduce the number of lines in your routine as such

Dim xRow As DataRow
xRow = StatusTable.NewRow()
xRow.ItemArray = {txt, resp}
StatusTable.Rows.Add(xRow)

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.