0

I'm writing a WPF application connected to a local Access database. In one of the application screens, one table data (named Service) is shown in individual textboxes, like a form, and the user can navigate through records, create new ones, delete, edit or search. Everything is done on the same table.

After a intensive research on how to navigate through records displayed in textboxes, I ended up using a DataSet and a CollectionView.

public partial class Entries : Window
{
    AgendaDataSet agendaDataSet = new AgendaDataSet();
    AgendaDataSetTableAdapters.ServiceTableAdapter serviceAdapter = new AgendaDataSetTableAdapters.ServiceTableAdapter();
    CollectionView workSheetView;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {                         
        this.serviceAdapter.FillByDateAsc(agendaDataSet.Service);                
        this.DataContext = agendaDataSet.Service;
        this.workSheetView = (CollectionView)CollectionViewSource.GetDefaultView(agendaDataSet.Service);
        this.workSheetView.MoveCurrentToLast();                                  
    }

I got record navigation working using the CollectionView methods MoveCurrentToFirst(), MoveCurrentToNext(), etc. I also can create new records, edit and delete.

This is the method I use to create a new record:

    private void btnNovo_Click(object sender, RoutedEventArgs e)
    {            
        dynamic row = this.agendaDataSet.Service.NewMainRow();            
        this.agendaDataSet.Service.AddMainRow(row);
        this.workSheetView.MoveCurrentToLast();
    }

My problem is with record searching. I have a button that, when the user presses it, it asks for the PatientName he is searching. Then, the data about that Patient must appear on the various textboxes, ready to be consulted, edited or deleted.

Through the CollectionView, I only found the method GetItemAt() that gets a record based on it's row index. Since I am working with an Access database, I can't use the predicate ROW_NUMBER. And I don't think this approach would be the best.

So, how can I get an item based on it's ID, or PatientName, or any other field, and pass it as a row to the CollectionView?

RodWall
  • 149
  • 1
  • 9

1 Answers1

0

Probably you don't need to get an item based on its ID or PatientName property. Suppose that the user looks for "Andrew" as PatientName. Your code finds that the second row of your DataTable (called "Service") is the one the user is looking for.

You can use a simple static method to look for a DataRowView, something like this:

private static DataRowView FindDataRowView(DataView dataView, DataRow dataRow)
{
    foreach (DataRowView dataRowView in dataView)
    {
        if (dataRowView.Row == dataRow)
        {
            return dataRowView;
        }
    }

    return null;
}

and then you can select the object in your CollectionView:

collectionView.MoveCurrentTo(FindDataRowView(agendaDataSet.Service.DefaultView,
    agendaDataSet.Service.Rows[2]));

Of course you can find the real DataRow index by using a foreach cycle or the Select method of DataTable.

Il Vic
  • 5,576
  • 4
  • 26
  • 37