关于c#:如何在datagridview中将特定的行放在一起?

How do I put specific rows together in datagridview?

我创建了一个DataGridView控件来列出一些内容。但是,我想根据textBox中的条件将特定的行放在一起(不要将所有行放到一行中)。

谢谢!

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
DataTable table = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            // populate dgv from datatable

            // add columns
            table.Columns.Add("Number", typeof(int));
            table.Columns.Add("Food", typeof(string));

            // add rows
            table.Rows.Add(1,"BBQ");
            table.Rows.Add(2,"Pear");
            table.Rows.Add(3,"Eggs");
            table.Rows.Add(4,"Banana");
            table.Rows.Add(5,"Noodle");
            table.Rows.Add(6,"Orange");
            table.Rows.Add(7,"Mango");
            table.Rows.Add(8,"Beef");

            dataGridView1.DataSource = table;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text =="Fruit" )
            {

            }
        }

例如,当我在文本框中键入"水果"时。从第一行开始,所有包含一种水果的行将自动彼此对齐,如下所示:

  • 香蕉
  • 橙子
  • 芒果
  • 在文本框中键入单词" Meat"时,其余包含肉类的行也是如此。


    您可以使用DataView.RowFilter属性来过滤行。但是您需要基于一些可以过滤的列。

    在您的情况下,您没有任何包含"水果,肉类"的食品,因此我再添加一种食品。 (如果您不想显示此列,我们可以将其隐藏)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    public partial class Form1 : Form
        {
            DataTable table = new DataTable();
            public Form1()
            {
                InitializeComponent();
                table.Columns.Add("Number", typeof(int));
                table.Columns.Add("Food", typeof(string));
                table.Columns.Add("FoodType", typeof(string));

                // add rows
                table.Rows.Add(1,"BBQ","Meat");
                table.Rows.Add(2,"Pear","Fruit");
                table.Rows.Add(3,"Eggs","Eggs");
                table.Rows.Add(4,"Banana","Fruit");
                table.Rows.Add(5,"Noodle","Veg");
                table.Rows.Add(6,"Orange","Fruit");
                table.Rows.Add(7,"Mango","Fruit");
                table.Rows.Add(8,"Beef","Meat");

                dataGridView1.DataSource = table;
               //hide the foodtype column
                this.dataGridView1.Columns["FoodType"].Visible = false;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text !="")
                {
                    DataView dv = new DataView(table,"FoodType= '" + textBox1.Text +"'","FoodType Desc", DataViewRowState.CurrentRows);

                    dataGridView1.DataSource = dv;
                }
                else
                    dataGridView1.DataSource = table;
            }
        }

    值是基于文本框值的过滤器。如果再次想要所有值,则仅输入空白文本框,然后单击按钮。