Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView filter with BindingSource?

$
0
0

Hi,

Been searching the web for a couple days, trying various examples but can't seem to get this to work.  I have a form with a DataGridView.  I pass the form a reference to my data access layer class, which contains a method that returns a dataset that contains a datatable called Patients.

I'm using a BindingSource as the DataSource of the DataGridView and some TextBox objects on the form.  The intent being that when the user clicks on a record in the DataGridView, the details appear in the TextBoxes.  This part is working fine.

I also have another TextBox that I'm trying to use to filter the DataGridView and this is the part I can't get to work.  It compiles without error but it just doesn't work, nothing seems to happen to the data displayed.

Here's my code:

public partial class frmPatientSelection : Form
{
        public DataTable dtPatients { get; set; }
        public DataSet dsPatients { get; set; }
        DeliveryMgrDALDisLayer _dal = null;
        BindingSource bs = new BindingSource();

        public frmPatientSelection()
        {
            InitializeComponent();
        }

        public frmPatientSelection(DeliveryMgrDALDisLayer dal)
        {
            InitializeComponent();
            _dal = dal;
        }

        private void frmPatientSelection_Load(object sender, EventArgs e)
        {
            // Get the active patient data.
            dsPatients = _dal.GetActivePatients();
            
            // Set the binding source.
            bs.DataSource = dsPatients;

            // Set the DataGridView data source.
            dgvPatients.DataSource = bs;
            dgvPatients.DataMember = "Patients";

            // Setup text box data sourcse.
            txtPatientName.DataBindings.Add("Text", bs, "Patients.Patient");
            txtDestination.DataBindings.Add("Text", bs, "Patients.Primary Addr");
        }

        private void txtFind_TextChanged(object sender, EventArgs e)
        {
            bs.Filter = string.Format("[patient id] LIKE '{0}'", txtFind.Text);
        }
    }
}

All of the examples I've found online are basically this approach, but it simply doesn't work.

I did have some success without the BindingSource, using a DataTable as the DataSource for the DataGridView, but all of the examples I found for binding the TextBoxes to the DataGridView used the BindingSource.

Any thoughts as to what I'm missing?

Thanks in advance!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>