tags:

views:

165

answers:

5

suppose i am having a database table with 20 records and of that i want to display only 10 records in the dataviewgrid control, how can i achieve this?

+1  A: 

Select only the 10 records you want.

In SQL use the TOP clause:

SELECT TOP 10 * FROM myTable
Oded
actually, i have a DataSet object consisting of the entire table and from this dataset object i want to take out any 10 records, depending on a certain condition and put them into the dataviewgrid.
Pratik Gandhi
You can't do that directly on the DataSet/DataTable.
Oded
A: 

use DataTable.Select

usage:

dataSet1.Tables[0].Select("Id>5");

or, better, DataView with a RowFilter, example here

you can set tha DataGridView.DataSource to this DataView

Catalin DICU
+1  A: 

You can write a query like this:

SELECT * FROM (
  SELECT TOP 10 * FROM (
    SELECT TOP 20 * FROM MyTable ORDER BY MyID ASC
  ) AS NewTbl ORDER BY MyID DESC
) AS NewTbl2 ORDER BY MyID  ASC

This selects records 11-20. If you want to select records 6-15 just change 20 to 15.
20 is the "last record to select" and 10 is the number of records before and up to 20.

Edit (After your comment about having all rows in a DataSet):

var newDS = new DataSet();
newDS.Tables.Add(oldDSWithAllRows.Tables["YourTableName"].Clone());

foreach (DataRow myDataRow in oldDSWithAllRows.Tables["YourTableName"].Rows)
{
  if (/* Your criteria */)
  {
    newDS.Tables["YourTableName"].ImportRow(myDataRow);
  }
}

myDataGridView.DataSource = newDS;
Sani Huttunen
can you please translate the above code in C#? i am not familiar with vb.net
Pratik Gandhi
the above code is C#...
Sani Huttunen
i am a newbie and hence not familiar with statements like foreach (DataRow myDataRow in oldDSWithAllRows.Rows) so if possible can u plz explain in words what that statement does so that i can try to do it in my own way.thx in advance.
Pratik Gandhi
The foreach loop takes all rows in your current ´DataSet´ and loops through them one by one. Much like a for loop but without an indexer.
Sani Huttunen
if i am not wrong, what u r trying to suggest is that i create a new table in my dataset and then whichever rows i want from my old table (in the same dataset), i copy those rows to the new table and then fill the datagridview with this new table.
Pratik Gandhi
That is exactly what I suggest.
Sani Huttunen
well, i tried doing that but i am getting the error "This row already belongs to another table". ne solns for dis????
Pratik Gandhi
Sorry, my bad. You can use DataTable.ImportRow. Changed the code to reflect this.
Sani Huttunen
thx a lot, got the answer
Pratik Gandhi
A: 

If you're using the latest version of C# you could filter your source with LINQ:

// change Skip to 10 to page through
var filtered = mydatasource.Skip(0).Take(10); 

This assumes you've returned your SQL data as an IEnumerable<T>

Chris S
i am using visual studio 2005, so i guess, won't be able to implement there.
Pratik Gandhi
A: 

Suppose we have the following table

DataTable dt = new DataTable();
int counter = 1;
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 1; i <= 20; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i;
dr["Name"] = string.Format("Name{0}", i);
dt.Rows.Add(dr);
}

You can bind the grid this way:

this.GridView1.DataSource = dt.AsEnumerable().Take(10);
this.GridView1.DataBind();

but: this can work if you did the following: -Add two template fields to the gridview -Add the following function to the code behind page:

protected object GetColumnValue(object Row,string columnName)
        {
            DataRow dr = Row as DataRow;
            return dr[columnName];
        }

Edit the DataBindings of the fields of the gridview to bind this way:

GetColumnValue(Container.DataItem,"ID") //for the first Field
GetColumnValue(Container.DataItem,"Name") //for the second field
SubPortal