使用BindingSource的DataGridView过滤器? [英] DataGridView filter with BindingSource?

查看:59
本文介绍了使用BindingSource的DataGridView过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在网上搜索了几天,尝试了各种各样的例子,但似乎无法让它发挥作用。 我有一个带有DataGridView的表单。 我将表单传递给我的数据访问层类,该类包含一个返回数据集
的方法,该数据集包含一个名为Patients的数据表。


我正在使用BindingSource作为DataGridView的DataSource和表单上的一些TextBox对象。 目的是当用户单击DataGridView中的记录时,详细信息将显示在TextBox中。 这部分工作正常。


我还有另一个TextBox,我试图用来过滤DataGridView,这是我无法工作的部分。 它编译没有错误,但它只是不起作用,似乎没有任何事情发生在显示的数据。


这是我的代码:

 public partial class frmPatientSelection:Form 
{
public DataTable dtPatients {get;组;
public DataSet dsPatients {get;组;
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)
{
//获取有效的患者数据。
dsPatients = _dal.GetActivePatients();

//设置绑定源。
bs.DataSource = dsPatients;

//设置DataGridView数据源。
dgvPatients.DataSource = bs;
dgvPatients.DataMember =" Patients" ;;

//设置文本框数据源码。
txtPatientName.DataBindings.Add(" Text",bs," Patientss.Patient");
txtDestination.DataBindings.Add(" Text",bs," Patientss.Primary Addr");
}

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

我在网上找到的所有例子基本上这种方法,但它根本不起作用。


我没有使用BindingSource取得了一些成功,使用DataTable作为DataGridView的DataSource,但是我找到的所有例子用于将TextBoxes绑定到使用BindingSource的DataGridView。


关于我缺少什么的想法?


提前致谢!


解决方案

您好,


对于字符串字段,通常是您想要使用条件,例如以下是包含,也可以做起,也可以结束。我有一些用于字符串过滤的语言扩展名

这里

 bs.Filter = string.Format(" [patient id] LIKE'%{0}%'" ,txtFind.Text); 


上述内容不适用于数字字段。



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!

解决方案

Hello,

For string fields usually you want to use conditions e.g. the following are contains, can do starts with and ends with also. I have some language extension for string filtering here.

bs.Filter = string.Format("[patient id] LIKE '%{0}%'", txtFind.Text);

The above does not work with numeric fields.


这篇关于使用BindingSource的DataGridView过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆