Vlad Alexei

Vlad Alexei

  • NA
  • 10
  • 9.4k

How do I put specific rows together in datagridview?

Jul 9 2019 8:59 PM
Hi,

I created a DataGridView control to list down a couple of stuff. However, I want to put specific rows together (NOT putting them all inside a row) based on a condition from the textBox. For example, when I type "Fruit" in the textBox. All the rows that contain a type of fruit will automatically line up on one another, starting at the first row like this:

 Number  Food
 1  Pear
 2  Banana
 3  Orange
 4  Mango

The same goes for the rest of rows that contain a type of meat when the word "Meat" is typed in the textBox.

Thank you!
 
Here's the code:
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" )
{
}
}

Answers (1)