Solved

vb.net 2008 modify to use Datagridview

Posted on 2009-04-15
11
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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