Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

vb.net 2008 modify to use Datagridview

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
aarontham
Asked:
aarontham
  • 5
  • 4
1 Solution
 
JackOfPHCommented:
Set the datasource of the datagridview to the datable

Datagridview1.Datasource = rsNAme


0
 
aaronthamAuthor Commented:
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
 
JackOfPHCommented:
'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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
aaronthamAuthor Commented:
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
 
JackOfPHCommented:
Your grid is bounded...

Please remove this...

Datagridview1.Datasource = rsNAme
0
 
JackOfPHCommented:
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
 
aaronthamAuthor Commented:
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
 
JackOfPHCommented:
Dim custRows() As DataRow = rsName.Select("CusName like  '%" & (TextBox38.Text) & "%'")
 
            Datagridview1.Refresh
0
 
aaronthamAuthor Commented:
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
 
CodeCruiserCommented:
Try this,
rsName.DefaultView.RowFilter = "CustName Like '%" & TextBox38.Text & "%'"
DataGridView1.DataSource = rsName.DefaultView
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now