Solved

vb.net 2008 modify to use Datagridview

Posted on 2009-04-15
11
261 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now