By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

Filter Data in the DataGrid system in 3 layers

tsanalista
P: 11
Staff good day.
I have a problem when that will filter the data in my datagrid.
I am using a system with 3 Layers (BLL, DAL, WIN). Whenever I type something in the textbox generates this error message
/ / Error
* Unable to cast object of type 'System.Collections.Generic.List `1 [BLL.BLL.Students.BLLStudents ]' to type 'System.Data.DataView'.


Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Data
  3. Imports System.Text
  4. Imports System.Text.RegularExpressions
  5. Imports BLL.BLL.Course
  6. Imports BLL.BLL.Students
  7.  
  8. Public Class Form1
  9. #Region "Private Varibales"
  10.     Private _criar As Boolean = False
  11.     Private _editar As Boolean = False
  12.     Private _carregar As Boolean = True
  13.     Private _objc As BLLCourse = Nothing
  14.     Private _objs As BLLStudents = Nothing
  15.     Private _dset As DataSet = Nothing
  16.     Private _dlinha As DataRow = Nothing
  17.     Private _retvalor As Boolean = False
  18.     Private _cursoid As Integer = 0
  19.  
  20.  
  21.  
  22. #End Region
  23.  
  24.  
  25.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  26.         Try
  27.             _objs = New BLLStudents()
  28.  
  29.             _objs.AllStudents()
  30.             DataGridView1.DataSource = _objs.ListaEstudantes
  31.  
  32.         Catch ex As Exception
  33.             MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
  34.             Me.Close()
  35.         Finally
  36.             _objs = Nothing
  37.         End Try
  38.  
  39.     End Sub
  40.  
  41.  
  42. //Textbox
  43.  
  44. Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
  45.  
  46.         Try
  47.  
  48.             Dim txtstring As String = Me.txtsearch.Text
  49.  
  50.             Dim dv As DataView = Me.DataGridView1.DataSource
  51.  
  52.             dv.RowFilter = "StudentsName LIKE '%" + txtstring + "%'"
  53.  
  54.             Me.DataGridView1.DataSource = dv
  55.  
  56.         Catch ex As Exception
  57.             Throw ex
  58.         End Try
  59.  
  60.     End Sub
  61.  
  62.  
  63.  
  64.  
  65.  

Here BLL Students
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. Imports System
  5. Imports System.Data
  6.  
  7. Imports DAL.DAL.Students
  8.  
  9. Namespace BLL.Students
  10.     Public Class BLLStudents
  11.  
  12.  
  13. #Region "Variables"
  14.  
  15.         Private _obj As DALStudents = Nothing
  16.         Private _retvalor As Boolean = False
  17.         Private _dset As DataSet = Nothing
  18.         Private _dlinha As DataRow = Nothing
  19.         Private _idvalor As Object = Nothing
  20.  
  21.         Private _StudentsID As Integer
  22.         Private _courseid As Integer
  23.         Private _namestudents As String
  24.         Private _addressstudents As String
  25.         Private _phonestudents As String
  26.         Private _agestudents As Integer
  27.         Private _paidstudents As Boolean
  28.         Private _liststudents As List(Of BLLStudents)
  29.  
  30. #End Region
  31.  
  32.  
  33.      Public Sub AllStudents()
  34.  
  35.             _dset = Nothing
  36.             Try
  37.                 _obj = New DALStudents()
  38.                 _dset = _obj.AllStudents()
  39.  
  40.                 Dim objs As BLLStudents = Nothing
  41.                 _liststudents = New List(Of BLLStudents)
  42.  
  43.                 For index As Integer = 0 To _dset.Tables(0).Rows.Count - 1
  44.                     objs = New BLLStudents
  45.                     objs.StudentsID = _dset.Tables(0).Rows(index)(0).ToString()
  46.                     objs.CursoID = _dset.Tables(0).Rows(index)(1).ToString()
  47.                     objs.EstudanteNome = _dset.Tables(0).Rows(index)(2).ToString()
  48.                     objs.EstudanteEndereco = _dset.Tables(0).Rows(index)(3).ToString()
  49.                     objs.Telefone = _dset.Tables(0).Rows(index)(4).ToString()
  50.                     _liststudents.Add(objs)
  51.                     objs = Nothing
  52.                 Next
  53.  
  54.             Catch ex As Exception
  55.                 Throw ex
  56.             Finally
  57.                 _obj = Nothing
  58.             End Try
  59.  
  60.         End Sub
  61.  
  62.  
I'll leave here a sample of Crud if you want to test:

https://skydrive.live.com/redir?resi...JEWCcmZXmKsI50
Nov 20 '12 #1
Share this Question
Share on Google+
11 Replies


Rabbit
Expert Mod 10K+
P: 12,382
You can't assign a list object to a dataview object.
Nov 20 '12 #2

tsanalista
P: 11
so in this case how can I do this kind of research in my Datagrid?
Nov 20 '12 #3

tsanalista
P: 11
Could you explain how to obtain the same result in this example here?

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
  2.  
  3.         Try
  4.  
  5.             Dim txtstring As String = Me.txtsearch.Text
  6.  
  7.             Dim dv As DataView = Me.DataGridView1.DataSource
  8.  
  9.             dv.RowFilter = "StudentsName LIKE '%" + txtstring + "%'"
  10.  
  11.             Me.DataGridView1.DataSource = dv
  12.  
  13.         Catch ex As Exception
  14.             Throw ex
  15.         End Try
  16.  
  17.     End Sub
  18.  
Nov 20 '12 #4

Rabbit
Expert Mod 10K+
P: 12,382
Here's Microsoft's documentation on how to bind a DataView to a DataViewGrid.
http://msdn.microsoft.com/en-us/library/bb669070.aspx
Nov 20 '12 #5

tsanalista
P: 11
Okay, thank you. But not exactly fills in the DataGrid I want. I want to make queries according what I type in the textbox.
Just like that I'm returning a List Of, I can not do this kind of query.
Nov 20 '12 #6

tsanalista
P: 11
I want to take this research using BLL I created ... understand?
If I do it this way there is going to work. But would the design pattern that I am organizing.
Nov 20 '12 #7

Rabbit
Expert Mod 10K+
P: 12,382
I have no idea what you said in post #7.

That's just to bind a dataview to the dataviewgrid. You will still need to do your filtering after it.
Nov 20 '12 #8

tsanalista
P: 11
Sorry, my English is weak, because I'm from Brazil.
About the project, how do I list the data using my BLL class that returns a List of Students?

Using this model below ..

Expand|Select|Wrap|Line Numbers
  1.  Public Sub AllStudents()
  2.  
  3.             _dset = Nothing
  4.             Try
  5.                 _obj = New DALStudents()
  6.                 _dset = _obj.AllStudents()
  7.  
  8.                 Dim objs As BLLStudents = Nothing
  9.                 _liststudents = New List(Of BLLStudents)
  10.  
  11.                 For index As Integer = 0 To _dset.Tables(0).Rows.Count - 1
  12.                     objs = New BLLStudents
  13.                     objs.StudentsID = _dset.Tables(0).Rows(index)(0).ToString()
  14.                     objs.CursoID = _dset.Tables(0).Rows(index)(1).ToString()
  15.                     objs.EstudanteNome = _dset.Tables(0).Rows(index)(2).ToString()
  16.                     objs.EstudanteEndereco = _dset.Tables(0).Rows(index)(3).ToString()
  17.                     objs.Telefone = _dset.Tables(0).Rows(index)(4).ToString()
  18.                     _liststudents.Add(objs)
  19.                     objs = Nothing
  20.                 Next
  21.  
  22.             Catch ex As Exception
  23.                 Throw ex
  24.             Finally
  25.                 _obj = Nothing
  26.             End Try
  27.  
  28.         End Sub
  29.  
Nov 20 '12 #9

Rabbit
Expert Mod 10K+
P: 12,382
Well, from your first post, it sounds like you're already listing the data. And it sounded like you were having problems filtering the data, not listing it. If you're actually having problems listing it, that's a completely different problem.
Nov 20 '12 #10

tsanalista
P: 11
Exactly, in the first post, I can solve the problem. Now this would filter the data in the DataGrid using this model Crud as quoted above. But always give this error:

* Unable to cast object of type 'System.Collections.Generic.List `1 [BLL.BLL.Students.BLLStudents]' to type 'System.Data.DataView'.
Nov 21 '12 #11

Rabbit
Expert Mod 10K+
P: 12,382
You're confusing me. You just said you were having problems listing the data but now you say you're not having problems listing the data. So now we're back to the problem originally stated in the first post.

And the solution to that is in post #5 in the link to Microsoft's documentation on binding a dataview to a dataviewgrid. You have to correctly assign it before you can filter on it.
Nov 21 '12 #12

Post your reply

Sign in to post your reply or Sign up for a free account.