I am currently unsure on the best way of going about getting a filter to work right on a datagridview that has its datasource set to a list of objects.
So given an object of:
public class DepositAccountBill
{
#region Properties
public int AccountBillID { get; set; }
public int AccountID { get; set; }
public string AccountNumber { get; set; }
public string ControlNumber { get; set; }
public DateTime BillDate { get; set; }
public decimal DepositAmount { get; set; }
}
I have a datagridview table that looks roughly like this:
Account Number | Control Number | Bill Date | Deposit Amount
123456 | AJA1234367 | 5/21/2018 | 12.99
123456 | PSA1234367 | 5/21/2018 | 5.77
567332 | HBA1234367 | 5/21/2018 | 1255.99
769843 | AJA1234367 | 5/21/2018 | 12.99
So when clicking on a cell. Lets say the first cell on the first column. If I click a button that says filter I need to display the datagridview table with only rows that have the same Account number. In this case, it would be rows 1 and 2. In order for me to do this, I have to access the object that the datagridview table is populated with DepositAccountBill. So what I need to do is look and at the column I'm looking at has the selected cell's value.
So in my method I have tried thus far with no results:
var collection = (List<DepositAccountBill>)dataGridView1.DataSource;
var filterList = collection.Where ( q => (collection.Select(r => GetPropValue(r, dataGridView1.Columns[clickedCell.ColumnIndex].DataPropertyName))) == (clickedCell.Value);
dataGridView1.DataSource = filterList.ToList();
public object GetPropValue(object obj, string propName)
{
return obj.GetType().GetProperty(propName).GetValue(obj, null);
}
I don't know is SELECT is the right LINQ method to use here or if this is even possible. I want to use WHERE because it only grabs the list of objects that match the condition. Something like this: var filterList = collection.Where(r => r.AccountNumber == clickedCell.Value);
Only problem is the r.AccountNumber
is dependant on the data property of the selected column. The program does not know what the data property is based on a click event on the selected cell. This is why I think reflection might be necessary.
Aucun commentaire:
Enregistrer un commentaire