Solved

Need help with DataGridView object

Posted on 2007-11-27
6
173 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
Independent Software Vendors: 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!

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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