使用vb.net中的datetimepicker组合框进行搜索 [英] make a search using combo box with datetimepicker in vb.net

查看:126
本文介绍了使用vb.net中的datetimepicker组合框进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您希望我的帮助如何使用组合框和两个datetimepicker搜索我的数据网格值,


其中 组合框依赖于datetimepicker。 ,例如:


NAME:ALI1,LEO,LION,ALI2


AND


出生日期:


ALI1 = 2015年5月10日


LEO = 20-JUN-2001


LION = 3-JAN-2000


ALI2 = 2018年5月22日


现在我想只选择名称 ali ,在datetimepicker中我选择
2014年至2019年 ,搜索按钮显示结果。请告诉我连接组合框和datetimepicker的解决方案和代码!

解决方案

你好,


如果ComboBox有ALI1,LEO,LION,ALI2你不能使用ComboBox在ali上做类似的条件,因为ali不存在,只有ali1和ali2。


你是什么可以做的是使用TextBox和两个DateTimePickers来对DataGridView执行过滤。下面的示例从DataTable加载DataGridView,您可以从List< T>加载DataGridView。也。虽然你可以在没有DataSource
的情况下加载但是这样做是非常不明智的,但如果你想要将DataGridView行转换为DataGridViewRow集合并从行中查询。


这里是过滤器之前和过滤器之后的两个屏幕截图。如果您清空文本框并在执行搜索后按搜索,则会删除过滤器。




加载模拟数据的类。

公共类操作
{
public DataTable GetDataTable()
{
var dt = new DataTable();
dt.Columns.Add(new DataColumn(){ColumnName =" Name",DataType = typeof(string)});
dt.Columns.Add(new DataColumn(){ColumnName =" BirthDate",DataType = typeof(DateTime)});

dt.Rows.Add(" ALI1",new DateTime(2015,5,10));
dt.Rows.Add("LEO",new DateTime(2001,6,20));
dt.Rows.Add(" LION&qu​​ot;,new DateTime(2000,1,3));
dt.Rows.Add(" ALI2",new DateTime(2019,5,22));

返回dt;
}
}

表格代码


1个TextBox,两个DateTimePicker,一个DataGridView,一个按钮( ComboBox在那里,但什么也没做。)

公共部分类Form1:表格
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender,EventArgs e)
{
var ops = new Operations();
var dt = ops.GetDataTable();

comboBox1.DataSource = dt.AsEnumerable()
.Select(person => person.Field< string>(" Name"))。ToList();

dataGridView1.DataSource = dt;
}

private void filterButton_Click(object sender,EventArgs e)
{
var dt =(DataTable)dataGridView1.DataSource;
if(string.IsNullOrWhiteSpace(nameTextBox.Text))
{
dt.DefaultView.RowFilter ="" ;;
}
其他
{
dt.DefaultView.RowFilter =


"名称如'%{nameTextBox.Text}%'AND " +


" BirthDate> =#{dateTimePicker1.Value}#AND BirthDate< =#{dateTimePicker2.Value}#" ;;
}

}
}



hi every one i want your help how can i search my datagrid value using combo box and two datetimepicker,

where the value ofcombo box depend with datetimepicker. for example:

NAME:ALI1,LEO,LION,ALI2

AND

DATE OF BIRTH:

ALI1=10-MAY-2015

LEO=20-JUN-2001

LION=3-JAN-2000

ALI2=22-MAY-2019

NOW i want to select only name ali and in datetimepicker i select 2014 to 2019 and the search button show me the result. please tell me the solution and code to connect combo box with datetimepicker!

解决方案

Hello,

If the ComboBox has ALI1, LEO, LION, ALI2 you can't use the ComboBox to do a like condition on ali as ali does not exists, only ali1 and ali2.

What you can do is use a TextBox and two DateTimePickers to perform a filter on your DataGridView. The following example loads a DataGridView from a DataTable, you could load the DataGridView from a List<T> also. Although you could load without a DataSource it's very unwise to do this but if you wanted to you need to cast the DataGridView rows to DataGridViewRow collection and query from the rows.

Here is two screenshots, before filter and after filter. If you empty the text box and press search after performing a search the filter is removed.

Class to load mocked data.

public class Operations
{
    public DataTable GetDataTable()
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn() {ColumnName = "Name", DataType = typeof(string)});
        dt.Columns.Add(new DataColumn() { ColumnName = "BirthDate", DataType = typeof(DateTime)});

        dt.Rows.Add("ALI1", new DateTime(2015,5,10));
        dt.Rows.Add("LEO", new DateTime(2001, 6, 20));
        dt.Rows.Add("LION", new DateTime(2000, 1, 3));
        dt.Rows.Add("ALI2", new DateTime(2019, 5, 22));

        return dt;
    }
}

Form code

1 TextBox, two DateTimePicker, one DataGridView, one button (the ComboBox is there but does nothing).

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var ops = new Operations();
        var dt = ops.GetDataTable();

        comboBox1.DataSource = dt.AsEnumerable()
            .Select(person => person.Field<string>("Name")).ToList();

        dataGridView1.DataSource = dt;
    }

    private void filterButton_Click(object sender, EventArgs e)
    {
        var dt = (DataTable) dataGridView1.DataSource;
        if (string.IsNullOrWhiteSpace(nameTextBox.Text))
        {
            dt.DefaultView.RowFilter = "";
        }
        else
        {
            dt.DefaultView.RowFilter =


"Name like '%{nameTextBox.Text}%' AND " +


"BirthDate >= #{dateTimePicker1.Value}# AND BirthDate <= #{dateTimePicker2.Value}#"; } } }


这篇关于使用vb.net中的datetimepicker组合框进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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