c# - DataGridVIew populated with anonymous type, how to filter? -
i've populated datagridview linq query returns anonymous type.
question: chance filter datagridview data source anonymous?
// setting datagridview data source rawdocumentsdatagridview.datasource = rawtopics .selectmany(t => t.documents) .select(d => new { documentid = d.id, rilevante = d.isrelevant, topicid = d.topic.id // foreign key }).tolist(); // make not visibile, waiting master change rawdocumentsdatagridview.visible = false; // when master selection changed... void rawtopicsdatagridview_selectionchanged(object sender, system.eventargs e) { if (rawtopicsdatagridview.currentrow == null) return; // selected topic id int tid = (int) rawtopicsdatagridview.currentrow.cells["topicid"].value; // filter rawdocumentsdatagridview based on topic id // warning: pseudo code var olddatasource = (list<anonymoustype>)rawdocumentsdatagridview.datasource; rawdocumentsdatagridview.datasource = olddatasource .where(d => d.topicid == tid); }
if keep doing (paraphrasing) "datasource = datasource.where(...)" going filtering inside filtered data repeatedly; in case would:
a: store list in field re-use, and
b: not anonymous type
class documentrow { public int documentid {get;set;} public bool rilevante {get;set;} public int topicid {get;set;} } ... list<documentrow> alldata; ... alldata = rawtopics.selectmany(t => t.documents) .select(d => new documentrow { documentid = d.id, rilevante = d.isrelevant, topicid = d.topic.id // foreign key }).tolist(); ... rawdocumentsdatagridview.datasource = alldata .where(d => d.topicid == tid).tolist();
Comments
Post a Comment