gridview
-
hai all, i have a sqlstatemnt which returns 10 rows. I am fillig this data in gridview using datareader.Nothing new. But i wanted to fill the data on some condition,so that the rows which are fulfilling that condition can only come in the gridview. out of 10 rows i need 2,5,7,9,10 rows data.Infact i want to skip the data on some condition. I s possible with datareader? if yes how to do?If not how to do? Please anybody have any idea is helpful for me. Thanks in advance.
kissy
I dont know why do you need to use DataReader. Of course you can do that, just need a counter while you read each datarow from the datareader. Otherwise, I would suggest to get a DataTable from the sqlStatement and use LINQ or by any means to filter out those rows.
datatable.AsEnumerable().Where(datarow=>{
// your logic
});Something like this . :thumbsup:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->**
-
hai all, i have a sqlstatemnt which returns 10 rows. I am fillig this data in gridview using datareader.Nothing new. But i wanted to fill the data on some condition,so that the rows which are fulfilling that condition can only come in the gridview. out of 10 rows i need 2,5,7,9,10 rows data.Infact i want to skip the data on some condition. I s possible with datareader? if yes how to do?If not how to do? Please anybody have any idea is helpful for me. Thanks in advance.
kissy
If your datasource is a collection object, then you can use linq to filter the data source and bind the result with the GridView. Else, you can filter your data in RowDataBound event,
if(condition)
e.Row.Visible = false;hope this will help.
Anurag Gandhi.
http://www.gandhisoft.com
Life is a computer program and every one is the programmer of his own life. -
If your datasource is a collection object, then you can use linq to filter the data source and bind the result with the GridView. Else, you can filter your data in RowDataBound event,
if(condition)
e.Row.Visible = false;hope this will help.
Anurag Gandhi.
http://www.gandhisoft.com
Life is a computer program and every one is the programmer of his own life. -
I have given the following code,but still it is displaying all the rows. If e.Row.Cells(2).Text <> "" Then If ((e.Row.Cells(2).Text = e.Row.Cells(2).Text) + 1) Then e.Row.Visible = False End If End If
kissy
try to debug your code you will find the exact problem.
Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0
-
hai all, i have a sqlstatemnt which returns 10 rows. I am fillig this data in gridview using datareader.Nothing new. But i wanted to fill the data on some condition,so that the rows which are fulfilling that condition can only come in the gridview. out of 10 rows i need 2,5,7,9,10 rows data.Infact i want to skip the data on some condition. I s possible with datareader? if yes how to do?If not how to do? Please anybody have any idea is helpful for me. Thanks in advance.
kissy
Normally you would build that condition into your SQL query statement so that the query only returns those rows you want included in the gridview. - Dave
-
I have given the following code,but still it is displaying all the rows. If e.Row.Cells(2).Text <> "" Then If ((e.Row.Cells(2).Text = e.Row.Cells(2).Text) + 1) Then e.Row.Visible = False End If End If
kissy
Kissy16 wrote:
If ((e.Row.Cells(2).Text = e.Row.Cells(2).Text) + 1) Then
Check this line !! :rolleyes:
Cheers ! Abhijit Jana | MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
hai all, i have a sqlstatemnt which returns 10 rows. I am fillig this data in gridview using datareader.Nothing new. But i wanted to fill the data on some condition,so that the rows which are fulfilling that condition can only come in the gridview. out of 10 rows i need 2,5,7,9,10 rows data.Infact i want to skip the data on some condition. I s possible with datareader? if yes how to do?If not how to do? Please anybody have any idea is helpful for me. Thanks in advance.
kissy
-
you nedd to create tp class one will contain the object of other claoss
ClientBasePlanData
ClientBasePlanList : List<ClientBasePlanData>
create the object of ClientBasePlanData fill it and add it into ClientBasePlanList
and later bind it
-
Normally you would build that condition into your SQL query statement so that the query only returns those rows you want included in the gridview. - Dave
-
I can't give any condition in my sqlquery so that only desired records can come. From the display only i have to do,can u give something with gridview which can fulfill my requirement ?
kissy
Instead of binding the SQL command with the gridview, store the results of the SQL query that match your criteria in a dataset. Then bind the dataset with the gridview. I think this is what Abhishek was suggesting as well. It would look something like:
// Create a dataset that represents the data you are retrieving from the database
DataSet myDataSet = new DataSet();
DataTable myDataTable = myDataTable.Tables.Add();
myDataTable.Columns.Add("ID", typeof(int));
myDataTable.Columns.Add("Foo", typeof(string));
myDataTable.Columns.Add("Bar", typeof(string));// Execute your sql reader as normal
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
// instead of binding the reader, add the records you want to the dataset
if (dr["myCriteria"].ToString() == "this")
{
myDataTable.Rows.Add(
(int)dr["ID"],
dr["Foo"].ToString(),
dr["Bar"].ToString()
);
}
}
dr.Close();
connection.Close();// Now bind the dataset you created to your GridView
myGridView.DataSource = myDataSet;
myGridView.BindData();This could be done more elegantly by using
myDataTable.Load(dr)
then removing the rows that do not meet your criteria via a LINQ statement. But it should give you an idea of what you are trying to accomplish with a DataSet and get you started... - Dave