Solved

vb.net 2008 modify to use Datagridview

Posted on 2009-04-15
11
266 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
Industry Leaders: 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!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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:…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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