473,327 Members | 1,936 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,327 software developers and data experts.

Help with best way to create a filtering record selector in a database application

I'm working on a contact management application, and need a hand with one
aspect...

Here's what I want to create:
------------------------------------
A form split into two parts. There is a datagrid on the left side that lists
names and perhaps a couple of other key fields. The user can click on a
record in the datagrid, which should automatically pull up details on that
record in the various text boxes and other controls on the right side of the
form.

The datagrid on the left side of the form should also be filterable. A
filter is entered from a textbox above the datagrid. For example,
if the user types "An" in the textbox, the datagrid only shows names
that start with "An" (e.g. "Anderson", "Anthony", "Anzar").

So I want 2 features:
1) a datagrid that is used as a record selector, and
2) a datagrid that may be filtered.
Here's my problem
------------------------------------
I can get either feature #1 or feature #2 to work, but not both at the same
time.

To get feature #1 to work, I bind the datagrid to my "contact" table,
and then I bind my various textboxes and other controls to individual fields
within the "contact" table. And then like magic,
when I select a record in the datagrid on the left side of the form,
that record is pulled up in the controls on the rigth side of the form.
I can also get feature #1 to work by putting my DataSet into a
DataViewManager,
and then binding my datagrid and my other controls to that.

To get feature #2 to work, I create a DataView object based on my "contact"
table, and then bind the datagrid to that DataView object. When I change the
RowFilter property, the grid changes just as I want. But when I select a
record in the datagrid, nothing happens on the right side of the form.
My own experiments
------------------------------------
I've tried experimenting with performing filters on the "contact" table's
DefaultView, but changing the RowFilter property doesn't seem to do
anything.

I've tried using a DataViewManager object for filtering without success (I
don't really
understand them completely).
Other notes
------------------------------------
1) I don't want all the fields from "contacts" appearing in the datagrid, so
I use a DataGridTableStyle to customize how stuff appears in the datagrid. I
don't think this effects anything adversely.

2) The datagrid, along with a couple of other controls, is tucked into a
User Control, which I've called Navigator. I don't think this effects
anything adversely either.

3) I'm using SharpDevelop, not Visual Studio (yet), so I do databinding
programmatically, not through the form designer.
Request for help
------------------------------------
I'm very experienced with C but new to C# and .Net (and I really like it). I
could really use advise on the best way to accomplish this task, but I also
wish to solicit advice on proper coding style and better program structure.
My code (without indents) from my Navigator user control, version 1
----------------------------------------------------------------------
// In this version, the filtering works, but the DataGrid stops acting as a
// record selector.
// grdPerson is the name of my DataGrid.
// My user control is called Navigator.
// The following code is used to set up the databinding.
DataView dataView;
public void SetDataBinding(DataView dataSource)
{
dataView = dataSource;
DoBinding();
}
public void SetDataBinding(DataTable dataSource, string sortByField)
{
dataView = new DataView(dataSource);
dataView.AllowDelete = false;
dataView.AllowNew = false;
dataView.Sort = sortByField + "ASC";
DoBinding();
}
void DoBinding()
{
const int NameColumnWidth = 130;
grdPerson.DataSource = dataView;
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataView.Table.TableName;
tableStyle.ReadOnly = true;
tableStyle.RowHeadersVisible = false;
DataGridTextBoxColumn col = new DataGridTextBoxColumn();
col.HeaderText = "Name";
col.MappingName = "ListBy";
col.Width = NameColumnWidth;
tableStyle.GridColumnStyles.Add(col);
grdPerson.TableStyles.Add(tableStyle);
currencyManager = (CurrencyManager) grdPerson.BindingContext[dataView];
currencyManager.PositionChanged += new System.EventHandler(PositionChanged);
}
// textFind is TextBox where user types in the filter.
// User types "A", grid filters to people named "A*".
// ApplyTextFilter() is called when the TextChanged event is fired.
void ApplyTextFilter()
{
string findText;
string filter;
findText = txtFind.Text;
if (findText == "")
filter = "";
else
filter = "ListBy Like '" + findText + "*'";
dataView.RowFilter = filter;
// Highlight first record in grid
if (dataView.Count > 0)
grdPerson.Select(0);
}
//
// Here is an excerpt of code from my MainForm.
// nvgPerson is an instance of my Navigator user control.
// db is an instance of a class that handles connected to my database.
// nvgPerson is bound to a DataView based on a table from db.DataSet.
// Other controls are bound directly to fields in a table in db.DataSet.
//
DataView dataView = new DataView(db.DataSet.Tables["person"]);
nvgPerson.SetDataBinding(dataView);
txtFirst.DataBindings.Add("Text", db.DataSet, "person.FirstName");
txtMiddle.DataBindings.Add("Text", db.DataSet, "person.MiddleName");
txtLast.DataBindings.Add("Text", db.DataSet, "person.LastName");
cbxSuffix.DataBindings.Add("Text", db.DataSet, "person.Suffix");
chkSpouse.DataBindings.Add("Checked", db.DataSet, "person.HasSpouse");
//
// etc
//

My code (without indents) from my Navigator user control, version 2
----------------------------------------------------------------------
// In this version, the DataGrid works as a record selector,
// but I can't get it to filter.
ViewManager viewManager;
public void SetDataBinding(DataViewManager viewManager, string table)
{
const int NameColumnWidth = 130;
grdPerson.DataSource = viewManager;
grdPerson.DataMember = table;
this.viewManager = viewManager;
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = table;
tableStyle.ReadOnly = true;
tableStyle.RowHeadersVisible = false;
DataGridTextBoxColumn col = new DataGridTextBoxColumn();
col.HeaderText = "Name";
col.MappingName = "ListBy";
col.Width = NameColumnWidth;
tableStyle.GridColumnStyles.Add(col);
grdPerson.TableStyles.Add(tableStyle);
currencyManager = (CurrencyManager) grdPerson.BindingContext[viewManager,
table];
}
void ApplyTextFilter()
{
string findText;
string filter;
findText = txtFind.Text;
if (findText == "")
filter = "";
else
filter = "ListBy Like '" + findText + "*'";
viewManager.DataViewSettings["person"].RowFilter = filter;
}
//
// Here is an excerpt of code from my MainForm.
// nvgPerson is an instance of my Navigator user control.
// db is an instance of a class that handles connected to my database.
//
DataViewManager viewManager = new DataViewManager();
viewManager.DataSet = db.DataSet;
viewManager.DataViewSettings["person"].Sort = "ListBy ASC";
nvgPerson.SetDataBinding(viewManager, "person");
txtFirst.DataBindings.Add("Text", viewManager, "person.FirstName");
txtMiddle.DataBindings.Add("Text", viewManager, "person.MiddleName");
txtLast.DataBindings.Add("Text", viewManager, "person.LastName");
cbxSuffix.DataBindings.Add("Text", viewManager, "person.Suffix");
chkSpouse.DataBindings.Add("Checked", viewManager, "person.HasSpouse");
//
// etc
//

-----------------------------------
Thanks!

Patrick
Nov 16 '05 #1
0 3606

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
7
by: Megan | last post by:
Hi everybody- I inherited a database that somehow uses a bound combo box as a record selector. Let me give you some background. The form is based on data from 2 tables. The first table, Person,...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
17
by: Liam.M | last post by:
Hey guys, Forgive me if my question my be alittle silly, but I would very much appreciate and assistance that could be given! My situation is as follows: I have created a Button, and set...
17
by: Timothy.Rybak | last post by:
Hello all, This is my first attempt at an application, so kid gloves are appreciated. I need to make a very simple form that only has a few elements. One is TraceCode - a text field that is...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
5
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
1
by: access baby | last post by:
Hi Below mention is the reply from Salad on my query i created a crosstab query and form not based on any table of qurey but this doesnt work . I somehow have missed something actually i have too...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.