在这种情况下我应该使用什么行过滤器或选择查询

时间:2019-04-04 17:30:52

标签: c# .net winforms datatable rowfilter

我正在编写一个程序来过滤csharp中给定数据表中的数据,该程序可以根据它们的类型以及用户想要分类为新闻,娱乐等的内容来过滤不同的网络通道,但是当用户第一次单击其他通道时却出现异常频道,而不是先显示所有频道。

名为“ channel_id”的列已属于此数据表

设计图片:enter image description here

我尝试通过在数据表的默认视图中应用行过滤器来使用此功能。

这是具有所有功能的完整代码

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vp_practice
{
    public partial class Form2 : Form
    {
        DataTable dttvchannel = new DataTable();
        public Form2()
        {
            InitializeComponent();
        }

        private void filldata()
        {
            //Adding columns
            dttvchannel.Columns.Add("channel_id", typeof(int));
            dttvchannel.Columns.Add("channel_name");
            dttvchannel.Columns.Add("Type");
            //Adding Rows
            dttvchannel.Rows.Add(1, "Bol tv", "News");
            dttvchannel.Rows.Add(1, "geo news", "News");
            dttvchannel.Rows.Add(1, "Hum tv", "entertainment");
            dttvchannel.Rows.Add(1, "Ary new", "News");
            dttvchannel.Rows.Add(1, "catoon network", "catoons");
            dttvchannel.Rows.Add(1, "Masala tv", "non tv ");
            dttvchannel.Rows.Add(1, "Ary entertainment", "entertainment ");
            dttvchannel.Rows.Add(1, "dunya tv", "empter");
        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            filldata();
            listBox1.DataSource = dttvchannel;
            listBox1.DisplayMember = "channel_name";
            label2.Text = dttvchannel.DefaultView.Count.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dttvchannel.DefaultView.RowFilter ="Type Like '%News'" ;
            label2.Text = dttvchannel.DefaultView.Count.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dttvchannel.DefaultView.RowFilter = "Type like '%entertainment'";
            label2.Text = dttvchannel.DefaultView.Count.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dttvchannel.DefaultView.RowFilter = "Type Not in('News','entertainment')";
        }
    }
}

如果用户单击其他按钮而不是显示所有程序,则所有程序均应相应显示频道

0 个答案:

没有答案