473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Filtering a DataGridView in bound mode using C#

8
Hello,

Quick Overview:
I am creating a Windows application using C# and have run across a problem. I’m trying to use a DataGridView in “Bound” mode, but filter the view.

I have a database table full of employees called Employees. I wish to display the First Name, Last Name, Employee Id and Social Security Number (SSN) of all employees who have illegitimate social security numbers. Due to the SSN validity check required, I cannot simply filter my sql statement to only pull in what I wish to display to the user. I must pull in all data, filter it (using an isValidSSN method) and then display only invalid SSN employees.

Code snippet:
Expand|Select|Wrap|Line Numbers
  1. private void GetData(string strConnection, string strSelectCommand)
  2. {
  3. //…
  4.   // 1. Instantiate the connection  
  5.   conn = new SqlConnection(strConnection);
  6.  
  7.   // 2. Create a dataset to hold and manipulate the results
  8.   dsEmps = new DataSet();
  9.  
  10.   // 3. Create a sql data adapter 
  11.   daEmps = new SqlDataAdapter(strSelectCommand, conn);
  12.  
  13.   // 4. Automatically create the insert, update, and delete statements
  14.   SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daEmps);
  15.  
  16.   // 5. Fill the DataSet  
  17.   daEmps.Fill(dsEmps, tableName);
  18.  
  19.   // 6. Set the datamember
  20.   dgEmps.DataMember = tableName;
  21.  
  22.   // 7. Fill the datagrid
  23.   dgEmps.DataSource = dsEmps.DefaultViewManager;
  24.  
  25.   // 8. Loop through each SSN in the dataset and check ssn validity.  
  26.   int iSSNIndex = 4;
  27.   int iCounter = 0;
  28.  
  29.   //8a. You can’t hide a row that is selected, so deselect all rows 
  30.   dgEmps.CurrentCell = null;
  31.  
  32.   //8b. If valid SSN, hide.  If not, show.
  33.   using (DataTableReader dtRdr = dsEmps.CreateDataReader())
  34.   {
  35.     do
  36.     {
  37.     while (dtRdr.Read())
  38.     {
  39.       //Hide row if SSN is OK.
  40.       if (isValidSSN(dtRdr.GetValue(iSSNIndex).ToString()))
  41.       {
  42.       dgEmps.Rows[iCounter].Visible = false;
  43.       }
  44.       else
  45.       {
  46.       //Read Only fields
  47.       dgEmps.Rows[iCounter].Cells["Company"].ReadOnly = true;
  48.       dgEmps.Rows[iCounter].Cells["Employee Id"].ReadOnly = true;
  49.       dgEmps.Rows[iCounter].Cells["Firstname"].ReadOnly = true;
  50.       dgEmps.Rows[iCounter].Cells["Lastname"].ReadOnly = true;
  51.       //SSN is the only editible column
  52.       }
  53.       iCounter++;
  54.     }
  55.     iSSNIndex += iSSNIndex;
  56.  
  57.     }
  58.     while (dtRdr.NextResult());
  59.   }
  60.  
  61.   }
  62. //…
  63. }
  64.  
Dec 20 '07 #1
2 7150
Rosco
8
(Sorry, the original problem didn't get posted)
Problem:
When the data loads into the datagridview, everything displays fine. But, when I sort a column, all the data I pulled from the database gets re-displayed. (So instead of showing 10 rows of employees with bad SSN's, I get 30,000 rows of employees - both the bad and good SSN's). I could reapply the isValidSSN condition to re-hide the data, but this is not efficient. (I also don't wish to simply remove the sort option of the columns).

Because the user is allowed to fix the SSN field in the DataGridView, I wish to add a "Refresh" button - removing all the fixed SSN's. When I attempted this, again all data was shown.

I'm thinking I'm better pulling in the data and in the isValidSSN populating an array of bad employees with bad SSN's and attaching this to a datagridview. Then, when changes are made or a refresh button is used, only the array (or dataset or something) will be run through the isValid method.
Dec 24 '07 #2
Plater
7,872 Expert 4TB
DataSets can have multiple tables.
Obtain your DataTable, then run THAT (and not your gridview object) through your isValidSSN() function.
You can then either choose to any number of the following:
1) Remove all VALID entries from the DataTable, leaving only your invalid SSN entries
2) Copy/Move all INVALID entries to another DataTable, giving you a subset(or seperate set, based on your choice of copy/move)
3) Generate another column for the isValidSSN results, which would be used to determine what to show/hide.

You could also use the various "row changed" (rowdirty?) properties to delay write-backs to the database. This would allow you to wait until a direct user input (save changes?) is called before writing back the data to the database, then re-pulling it and running it through isValidSSN() again.

You should also seriously consider putting part/all of isValidSSN() into the database query itself, it will be much faster.
Dec 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: milk-jam | last post by:
I'm trying to set my datagridview so that the first row will be left blank and to use it as a filtering filed for the datagridview. Until now I was using 2 datagridview the upper one with a header...
2
by: David Veeneman | last post by:
How can I set a bound DataGridView control to use a dataset table's column captions, instead of column names? I'm working with a DataGridView control, which I have bound to a table in a dataset....
2
by: inpuarg | last post by:
Is it possible that - or is there any workarround for adding a new unbound row to a datagridview at bound mode ? Theese are not working. And i don 't want to add a row to dataset then rebind -...
6
by: Satya | last post by:
Hi, I am using a DataGridView to display a large amount of data and so need to use filters for displaying certain data. For this I want to provide a textbox for each column of the DataGridView so...
4
by: Chris Botha | last post by:
VS2005 and I have a DataTable displayed in a DataGridView that needs custom sorting, with the DataSource of the grid set to the DataTable and the SortCompare event just won't fire. Then I found...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
8
by: Brian Pelton | last post by:
This is on .Net 2.0 in a WinForms application. I have a DataGridView that is bound to a BindingSource. The DataGridView has 3 columns. The first two are "normal" text columns and the last is a...
3
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
0
by: RKT | last post by:
I have a DataGridView bound to an MS Access table. This is a single- user application. When the User is adding or editing a row, the User may click on a Control elsewhere. That Control has context...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.