Solved

Filtering data in datagrid

Posted on 2006-06-25
12
210 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

20 Experts available now in Live!

Get 1:1 Help Now