Solved

Filtering data in datagrid

Posted on 2006-06-25
12
211 Views
Last Modified: 2010-04-23
I have the following code but the datagrid still showing all table data.

        strQuery = "Select * From Clients"

        Dim da As New SqlClient.SqlDataAdapter(strQuery, sConnString)
        Dim dt As New DataTable
        da.Fill(dt)

        frmClient.dgResult.DataSource = da

Thanks
0
Comment
Question by:yireh
  • 6
  • 6
12 Comments
 
LVL 1

Author Comment

by:yireh
ID: 16978610
Sorry, the correct used code is;

        strQuery = "Select * From Clients Where ID = 1"

        Dim da As New SqlClient.SqlDataAdapter(strQuery, sConnString)
        Dim dt As New DataTable
        da.Fill(dt)

        frmClient.dgResult.DataSource = da
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16978922
Try

        frmClient.dgResult.DataSource = dt

That is, the datasource should be the datatable, not the dataadapter.

Roger
0
 
LVL 1

Author Comment

by:yireh
ID: 16979814
Sancler, the code you send not work. Continue showing al table data.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16980228
What type of field is ID in your database's table Clients?

Roger
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16980232
I mean datatype

Roger
0
 
LVL 1

Author Comment

by:yireh
ID: 16980243
The DataType is Int.

n debug mode the DataTable variable has only one record but the DataGrid continue showing all records.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Expert Comment

by:Sancler
ID: 16980297
In that case try this

        frmClient.dgResult.DataSource = Nothing '<<< NEW LINE
        frmClient.dgResult.DataSource = da

Roger
0
 
LVL 1

Author Comment

by:yireh
ID: 16980343
Nothing Do
0
 
LVL 1

Author Comment

by:yireh
ID: 16980389
I mean, the problem is with the original binding setting.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16980420
Then it looks very much like there is something going on that the code you have posted so far does not reveal.  The code you have posted so far shows that you are declaring dt as a new datatable IMMEDIATELY BEFORE you fill it with the selective query.

        Dim dt As New DataTable
        da.Fill(dt)

But the symptoms you are describing would most likely occur if you had already filled the datatable with all the data, and then called .Fill again, on the same datatable, with the selective query.  The .Fill command does not empty a datatable before it brings over the records specified in any selective query.  It refreshes those rows identified by the selective query and leaves any others as they were before.

If my diagnosis is correct, then you can choose among (at least) three solutions.

1)   Clear the existing datatable before you fill it with the selective query

     dt.Clear()
     da.Fill(dt)

2)   In fact, as the code you have shown suggests you are doing, create and fill a NEW datatable with the results of your selective query and re-bind the datagrid to that new datatable.

3)   If you already have all the data in a datatable then, rather than revisiting the database to get the record you want, use a dataview, set its .RowFilter to "ID = 1" and bind your datagrid to the dataview rather than direct to the datatable.

Roger
0
 
LVL 1

Author Comment

by:yireh
ID: 16980530
I'm begin again. Please, send me a code sample to bind table to DataGrid.

I need to create a DataGrid with 3 columns (SSN, CLIENT_NAME, CLIENT_PHONE) from a Client table. I have a connection working.

If I not have any code, what is the code I need?
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 16982375
Here's some code - fully commented

        'query string, as at present
        Dim strQuery As String = "Select * From Clients Where ID = 1"
        'dataadapter, as at present, except ref is to connection, not sConnString, which looks like a ConnectionString rather than a Connection
        Dim da As New SQLDataAdapter(cmdText, connection)
        'datatable, as at present
        Dim dt As New DataTable
        'fill datatable, as at present
        da.Fill(dt)

        'NEW CODE - TO RESTRICT DISPLAY JUST TO COLUMNS YOU WANT
        'Create new table style
        Dim ts As New DataGridTableStyle
        'Add column for SSN
        Dim cs1 As DataGridColumnStyle = New DataGridTextBoxColumn
        cs1.HeaderText = "SSN" 'will be displayed at head of column
        cs1.MappingName = "SSN" 'name of column in datatable
        ts.GridColumnStyles.Add(cs1)
        'Add column for CLIENT_NAME
        Dim cs2 As DataGridColumnStyle = New DataGridTextBoxColumn
        cs2.HeaderText = "CLIENT_NAME" 'you might want to make this more 'user friendly' e.g. "Client Name"
        cs2.MappingName = "CLIENT_NAME"
        ts.GridColumnStyles.Add(cs2)
        Dim cs3 As DataGridColumnStyle = New DataGridTextBoxColumn
        cs3.HeaderText = "CLIENT_PHONE" 'you might want to make this more 'user friendly' e.g. "Phone Number"
        cs3.MappingName = "CLIENT_PHONE"
        ts.GridColumnStyles.Add(cs3)
        'Clear existing table style - not strictly necessary in this case but good practice
        dgResult.TableStyles.Clear()
        'Add the new style, showing just the columns you want
        dgResult.TableStyles.Add(ts)

        'Bind datagrid, similar to present but no reference to frmClient - see comment below
        dgResult.DataSource = dt

The above code should work, assuming that your connection is OK, provided it is placed in the same form as dgResult.  However, your previous code should also have worked (although it would have shown all columns in your datatable) under the same conditions.  Which makes me wonder if the problem was in your reference to

        frmClient.dgResult

It is, of course, possible to reference a form's datagrid from outside that form but only if the reference to the form is correctly made to an instance of that form.  What was "frmClient" in that line referring to?

Still, try the above, and see if that does what you want.

Roger
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

12 Experts available now in Live!

Get 1:1 Help Now