Solved

vb.net 2008 modify to use Datagridview

Posted on 2009-04-15
11
263 Views
Last Modified: 2013-11-26
i attach code, this code query Datatable and display it into Listview. pls help to modify it to do same query from Datatable but display result inot DataGridView
Public rsName As New DataTable
 
If rsName.Rows.Count > 0 Then
 
            Me.Cursor = Cursors.AppStarting
            Dim custRows() As DataRow = rsName.Select("CusName like  '%" & (TextBox38.Text) & "%'")
 
            Dim itX As ListViewItem
            Dim itm As ListViewItem.ListViewSubItem
 
            ListView30.Items.Clear()
 
            For Each row As DataRow In custRows
                itX = ListView30.Items.Add(row.Item("CusName").ToString().Trim())
                itm = New ListViewItem.ListViewSubItem(itX, row.Item("CusNameID").ToString().Trim())
                itX.SubItems.Add(itm)
            Next
 
            custRows = Nothing
            Me.Cursor = Cursors.Default
        End If

Open in new window

0
Comment
Question by:aarontham
  • 5
  • 4
11 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24146065
Set the datasource of the datagridview to the datable

Datagridview1.Datasource = rsNAme


0
 

Author Comment

by:aarontham
ID: 24146164
if just set Datagridview1.Datasource = rsNAme. it don't work.
rsName is datatable and the code will quiry raName the display result into Datagridview
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24153809
'Okey before doing this create two column namely colCusName and colCusNameID.


Public rsName As New DataTable
 
If rsName.Rows.Count > 0 Then
 
            Me.Cursor = Cursors.AppStarting
            Dim custRows() As DataRow = rsName.Select("CusName like  '%" & (TextBox38.Text) & "%'")
 
            Datagridview1.rows.clear
 
            For Each row As DataRow In custRows
            
            Dim rowIndex as integer = Datagridview1.rows.add
            
            Datagridview1(me.colCusName.index,rowindex).value = row.Item("CusName").ToString().Trim()
            Datagridview1(me.colCusNameID.index,rowindex).value = row.Item("CusNameID").ToString().Trim()
               
            Next
 
            custRows = Nothing
            Me.Cursor = Cursors.Default
 End If
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:aarontham
ID: 24154251
Hi JackOfPH,

i already create two column namely colCusName and colCusNameID.
I have error in below line

 Dim rowIndex as integer = Datagridview1.rows.add
below is the error
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.


I have error in below line too.
me.colCusName.index
me.colCusNameID.index

it say
'index' is not a member of 'String'
"colCusNameID" is not a member of "windowsApplication1.form1"
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24154488
Your grid is bounded...

Please remove this...

Datagridview1.Datasource = rsNAme
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24154501
Did you have any variable named colCusName , colCusNameID aside from the name columnname?

For example did you declare something like this?

Dim colCusName as string or colCusNameID?


If so please change it to other name...


Also, If you can post your code here, please do, so I can check it out...

Jack
0
 

Author Comment

by:aarontham
ID: 24156259
hi i cannot remove Datagridview1.Datasource = rsNAme.
the Datagridview1 will populate with data query from DB and store on rsName Datatable.

then i need the code to do query from this rsName then display again the result into the same Datagridview1.

Pls advice
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24163975
Dim custRows() As DataRow = rsName.Select("CusName like  '%" & (TextBox38.Text) & "%'")
 
            Datagridview1.Refresh
0
 

Author Comment

by:aarontham
ID: 24164657
Don't work and nothing happen.

pls find attach is the code. I have a Datagridview call DGVName

i do this below code in other sub
DGVName.Datasource = rsNAme
Private Sub TextBox38_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox38.TextChanged
        If rsName.Rows.Count > 0 Then
 
 
 
        Me.Cursor = Cursors.AppStarting
        Dim custRows() As DataRow = rsName.Select("Name like  '%" & (TextBox38.Text) & "%'")
 
        DGVName.Refresh()
 
        custRows = Nothing
        Me.Cursor = Cursors.Default
       End If
    End Sub

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24195221
Try this,
rsName.DefaultView.RowFilter = "CustName Like '%" & TextBox38.Text & "%'"
DataGridView1.DataSource = rsName.DefaultView
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 34
VB.net Move a class from Solution Items to a project 2 22
Footer for each row on Gridview 2 21
vb.net winforms sizing/resolution? 4 31
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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