Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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!

What I have tried:

C#
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" )
            {

            }
        }
Posted
Updated 10-Jul-19 17:32pm
v2
Comments
BillWoodruff 9-Jul-19 23:25pm    
So far, you haven't taken any steps toward sorting, changing the view. There's nothing in your Table that tells you which Items are 'Fruit.

What you need is to create a collection<type>, which would contain all of the food items, along with an additional property for food type. This collection could be populated with a database at sometime.
C#
public class Food {
  public int FoodID { get; set; }
  public string FoodTyoe { get; set; }
  public string FoodName { get; set; }

  public Food() {}
  public Food(int foodID, string foodType, string foodName) {
    FoodID = foodID;
    FoodType = foodType;
    FoodName = foodName;
  }

  public Collection<Food> LoadAllFood() {
    Collection<Food> foods = new Collection<Food>();

    foods.Add(new Food(1, "Meat", "BBQ"));
    foods.Add(new Food(2, "Fruit", "Pear"));
    // and so on with other items

    return foods;
  }
}
So now you have all your foods. Now you need to filter that down by the FoodType. This is easily achieved using a lambda expression in your Button method:
C#
private void Button1_Click(object sender, EventArgs e) {
  table.Rows.Clear();

  // get all the foods
  Collection<Food> AllFoods = new Food().LoadAllFoods();

  // filtered
  Collection<Food> SomeFoods = AllFoods.Where(f => f.Type == textBox1.Text);

  // populate your table
  foreach(Food item in SomeFoods) {
     table.Rows.Add(item.FoodID, item.FoodName);
  }

  // refresh your DGV
  dataGridView1.Refresh();
}

Now this is hastily written and could be a lot more compact and efficient. I thought this would give you a start on how to use Object Orientated Programming.
 
Share this answer
 
Comments
whateves 9-Jul-19 23:57pm    
Thank you for your suggestion. But there's something wrong with The lambda expression at f.Type
MadMyche 10-Jul-19 0:20am    
Probably needs to be set as an IEnumerable
var SomeFoods = ((IEnumerable)AllFoods).Where(....
You can do the research on this, I'm going to bed
BillWoodruff 10-Jul-19 12:24pm    
I had hoped to get the OP involved here, rather than writing code for him.
BillWoodruff 10-Jul-19 23:20pm    
my vote of #3: "hastily written" yes, and code that would not compile. if you are going to use (the lesser known) Collection<T>, you would expect to see some use of its protected method overrides. using Collection<T> will require you import the 'System.Collections.ObjectModel NameSpace.
whateves 10-Jul-19 23:23pm    
what? No you don't have to. I figured it out using Dataview.RowFilter. It was actually easier to understand.
As MadMyche points out, you have to incorporate the category data into your code.

Adding another Column with category data is the logical choice.

Assuming you've added a Column named 'Category as the third Column, you can sort the DataTable:
string cat = textBox1.Text;

table = table.AsEnumerable()
    .OrderByDescending(r => r.Field<string>(2).StringCaseIndependentEq(cat))
    .CopyToDataTable();

dataGridView1.DataSource = table;
dataGridView1.Refresh();
This code makes use of a simple case-independent string comparison Extension Method:
public static class StringExtensions
{
    // see: [<a href="http://cc.davelozinski.com/c-sharp/fastest-way-to-compare-strings" target="_blank" title="New Window">^</a>] 
    public static bool StringCaseIndependentEq(this string s1, string s2)
    {
        return String.Compare(s1, s2, StringComparison.OrdinalIgnoreCase) == 0;
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900