Solved

Filtering data in datagrid

Posted on 2006-06-25
12
213 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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