Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb.net vs2010 Insert data into daragridview

Posted on 2011-04-23
12
Medium Priority
?
613 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

0
Comment
Question by:altimofejevs
  • 8
  • 4
12 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35454258
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

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35454275
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

0
 

Author Comment

by:altimofejevs
ID: 35454304
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:nepaluz
ID: 35454385
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

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35454389
the second code suggestion above should read

StatusTable.Columns.AddRange({ ....

Open in new window

0
 

Author Comment

by:altimofejevs
ID: 35454557
and how to display that datatable?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35454677
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
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 total points
ID: 35454698
So the entire code should be:
1. Declare your table in the form:
Public WithEvents StatusTable As New DataTable

Open in new window


2. Then add the following to your form load event:
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")}}) 
Me.DataGridView1.DataSource = StatusTable

Open in new window


3. Finally, include thin in your testing routine:
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

 
0
 

Author Comment

by:altimofejevs
ID: 35454740
Got an error when launching application Error
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35455813
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 .....
0
 

Author Comment

by:altimofejevs
ID: 35455871
Yea I got that one :)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35455959
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

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question