Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
DamithSL 7-Jan-15 21:49pm    
update the question with the code which explain how you bind data to datagridview and also how which event or action you going to execute datagridview query and which data you going to use in query etc..
Suvendu Shekhar Giri 8-Jan-15 0:34am    
Yes, you can. Import excel sheet data to datatable and then bind the GridView with datatable as datasource. Now, you can do actions like filter, sort etc. on that datatable and bind the gridview from the modified datasource.

Hi ,
Check this
C#
DataTable dt3 = new DataTable();
dt3 = (DataTable)dataGridView1.DataSource;
DataRow[] result  = dt3.Select("EmployeeId = 100");

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Oshtri Deka 8-Jan-15 3:54am    
That will work if data is binded in a form of a table.
What if he has binded data in a form of an IList?
Mohamed Mitwalli 8-Jan-15 4:46am    
Check this
BindingList<customer> cust= ( BindingList<customer>)dataGridView1.DataSource;

var result = from x in customerList
where
x.First.Contains( "First1")
select x;

dataGridView2.DataSource = result.ToList();
private class Customer
{
private String _first;

public String First
{
get { return _first; }
set { _first = value; }
}
private String _last;

public String Last
{
get { return _last; }
set { _last = value; }
}

public Customer(String first, String last)
{
_first = first;
_last = last;
}
}
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:
C#
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:
C#
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.
C#
foreach(var row in dataGridView.Rows)
{ 
    if((row.Cells[columnIndex_Or_Name].Value as SomeType) == SomeValue)
    {
        //Do something
    }
}
 
Share this answer
 
Comments
Mohamed Mitwalli 8-Jan-15 5:04am    
+5
Oshtri Deka 8-Jan-15 5:09am    
Thank you.
Mohamed Mitwalli 8-Jan-15 5:22am    
your welcome Oshtri

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