查询datagridview c# [英] query datagridview c#

查看:95
本文介绍了查询datagridview c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究c#中的一个项目,其中我有一个datagridview,它是通过导入excel文件的方式填充的,现在一旦datagridview填充了数据,就可以从gridview查询数据了如果数据不是来自数据库,应该怎么做?





我是编程新手,很难想象出事。请帮助,谢谢

解决方案



查看此

 DataTable dt3 =  new  DataTable(); 
dt3 =(DataTable)dataGridView1.DataSource;
DataRow [] result = dt3.Select( EmployeeId = 100);



最好的问候

M.Mitwalli


如果你的数据源是DataTable那么你可以像Mohamed Mitwali建议的那样去做。



同样如果您的数据源是DataSet,那么您可以这样做:

< pre lang =c#> DataSet ds =(DataSet)dataGridView.DataSource;
DataTeble dt = ds.Tables [dataGridView.DataMember];

DataRow [] resultSet = dt.Select( 你的查询在这里);





如果您的数据源是List或(或IList< t>或任何其他实现IEnumerable< t>的集合)然后你可以提取它并做简单的Linq查询:

 IEnumerable< someobject> rows = IEnumerable< someobject> dataGridView.DataSource; 

var results = rows.Where(r = > r.SomeProperty == SomeValue)。ToList(); < / someobject > < / someobject > ;





你也可以循环并遍历所有行并检查它们的值;我只会在我的dataGridView中有无界列并且我想使用这些值进行过滤时使用这种方法。

  foreach  var  row  in  dataGridView.Rows)
{
if ((row.Cells [columnIndex_Or_Name] .Value as SomeType)== SomeValue)
{
// 做点什么
}
}


hi, I am working on a project in c# wherein I have a datagridview and it is populated by the means of importing an excel file, now once the datagridview is populated with the data is it possible to query data from the gridview even if the data is not from a database and how should it be done?


Im newby in programming and having a hard time to figure out things. Please help, thanks

解决方案

Hi ,
Check this

DataTable dt3 = new DataTable();
dt3 = (DataTable)dataGridView1.DataSource;
DataRow[] result  = dt3.Select("EmployeeId = 100");


Best Regards
M.Mitwalli


If your data source is DataTable then you can do it as Mohamed Mitwali has suggested.

Similarly if your data source is DataSet then you can do it like this:

DataSet ds = (DataSet)dataGridView.DataSource;
DataTeble dt = ds.Tables[dataGridView.DataMember];

DataRow[] resultSet = dt.Select("Your query goese here");



If your data source is List or (or IList<t> or any other collection which implements IEnumerable<t>) then you can extract it and do easy Linq query:

IEnumerable<someobject> rows = IEnumerable<someobject>dataGridView.DataSource;

var results = rows.Where(r => r.SomeProperty == SomeValue).ToList();</someobject></someobject>



You can also make a loop and go through all rows and check their values; I would use this approach only when I have unbounded columns in my dataGridView and I want to use those values for filtering.

foreach(var row in dataGridView.Rows)
{ 
    if((row.Cells[columnIndex_Or_Name].Value as SomeType) == SomeValue)
    {
        //Do something
    }
}


这篇关于查询datagridview c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆