Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Filtering data in datagrid

Posted on 2006-06-25
12
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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