Solved

Need help with DataGridView object

Posted on 2007-11-27
6
169 Views
Last Modified: 2013-11-26
I need a little assistance with DataGrids. I can bind the grid and have it show data no problem, but the table it is bound to I  don't want to show all the data, just data based on the  current order number (which is a column in the table). Can anyone assist me on this issue?
0
Comment
Question by:rolltide0
6 Comments
 
LVL 18

Accepted Solution

by:
vbturbo earned 250 total points
ID: 20357529
try this

Private myView As DataView

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
                With myView
                    .RowFilter = "Convert("OrderNumberColumn", 'System.String')  Like '%" & txtsearch.Text & "%'"
                    .RowStateFilter = DataViewRowState.CurrentRows
                End With

                dgv.DataSource = myView
end sub

vbturbo
0
 
LVL 9

Expert Comment

by:JonMny
ID: 20357597
    Dim s As New BindingSource()
       s.DataSource = lst
        Me.DataGridView1.DataSource = s
        s.Filter = "Ordernumber = 2"
0
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 250 total points
ID: 20358612
Hi, I hope you don't mind me chiming in...

This is similar to vbturbo's, however, it does not use the Convert method, or the LIKE operator, which
is an important difference, because this example will only return exact matches, which is what I think
you are looking for:

First of all, when you set the DataSource for your DataGridView, set it to the table's DataView, and
not the actual table itself, for example:

        'Create a table as an example:
        Dim table As New DataTable()
        table.Columns.Add("CurrentOrderNumber")
        table.Columns("CurrentOrderNumber").DataType = GetType(Int32)
        table.Columns.Add("Item")

        For i As Int16 = 1 To 10
            table.Rows.Add(i, "Item " & i)
            table.Rows.Add(i, "Item " & i)
        Next


        'Set the DataSource to the DataView of the table
        Me.DataGridView1.DataSource = table.DefaultView


'Then, when you want to perform filtering based off of a order number, you can do it like this, and it
will only return exact matches.

        'You can set this to whatever number your are looking for
        Dim OrderNumber As Int32 = 5

        Dim view As DataView = Me.DataGridView1.DataSource
        view.RowFilter = "CurrentOrderNumber=" & OrderNumber

        Me.DataGridView1.DataSource = view


One thing to think about:  The DataView will perform far better with fewer records in it, than a lot,
(for example - 5000+).  So filtering the DataView works great with fewer than say, 5000 records.  So
if you are thinking about having lots of records in the DataGridView, and then letting the user filter them,
you may want to think about filtering them in the Database they are coming from (like SQL, or Access,
etc.).  That way, if you have 25,000 orders in your database, don't retrieve all 25,000, and then let
the user filter the ones they want, but rather let the user select the ones they want (enter an order
number), then filter them with SQL when you retrieve them from the database.




0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 18

Expert Comment

by:vbturbo
ID: 20360344
VBRocks has provided you with a very good approach when handling a great amount of rows.
i agree with him.

Though you should let the database do what it is best to , handle perfomance heavy jobs.
This is what it is designed for among others.

My point of view is that when having a fair amount of rows and perhaps long order numbers
maybee 5-15 digits or even longer then there is a big advantice in filtering when typing.

so whatever choise you make , i think it comes down to user interaction,taste and performance < 1000 rows.

vbturbo

 
0
 

Author Comment

by:rolltide0
ID: 20443409
All three of you all gave great suggestions. Due to finals I haven't had the opportunity to try any of them. Vbturbo, can you explain your solution with a little more detail? The convert method is something I am not familiar. So if you have time, a line by line explanation would be very helpful.
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20450672
Hi

When filtering on column data which is (type of) integer then you can only use the "=" equal operator
as suggested by VBRocks.
:Display's only a match in the filter when it exacly match's your filter argument
 

By using the convert tostring method on the column (integer) you can now apply the LIKE operator on the
column filter.
:Display's all records which match each time you type a argument in your txtbox.

Try have a look on table column expressions
http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

vbturbo

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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:…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

26 Experts available now in Live!

Get 1:1 Help Now