Solved

Filtering data in datagrid

Posted on 2006-06-25
12
217 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

636 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