GridView and View Top 5/View All

C

Cindy Lee

I want to have a button to view top 5 and toggle to view all on my gridview.

Can this be done using paging? I don't want to show page 1, 2, 3. Just the
top 5 or all.

Or am I going to have to postback and change my query? If I do that I will
also lose my current sorting. Any suggestions?
 
G

Guest

your could setup ur gridview so that it has an empty pagerTemplate -> so that
the page numbers are not shown.

then set the event handler for your "top 5" button to enable paging on the
gridview and set the pagesize to 5.

and have ur event handler for 'all' to disable paging.

so you shouldnt have to requery your data (it should remain in the
gridview's viewstate)

good luck
 
C

Cindy Lee

When I click the button it works and I can see all the rows, but I don't get
the right sort order:

This is on the page load, so the sorting


DataTable statetable = StateDataSet.Tables[0];
GridView2.DataSource = statetable;
GridView2.DataBind();

Here's by button click function

protected void Button1_Click(object sender, EventArgs e)
{
GridView2.AllowPaging = false;
GridView2.DataBind();
Button1.Visible = false;
Button2.Visible = true;
}
 
Q

Quoc Linh

When I click the button it works and I can see all the rows, but I don't get
the right sort order:

This is on the page load, so the sorting

DataTable statetable = StateDataSet.Tables[0];
GridView2.DataSource = statetable;
GridView2.DataBind();

Here's by button click function

protected void Button1_Click(object sender, EventArgs e)
{
GridView2.AllowPaging = false;
GridView2.DataBind();
Button1.Visible = false;
Button2.Visible = true;
}

Cindy,

Bind to a view to sort your grid items:

StateDataSet.Tables[0].DefaultView.Sort = "FieldNameHere ASC"; //or
DESC
GridView2.DataSource = StateDataSet.Tables[0].DefaultView;
GridView2.DataBind();

Now, to display top 5, you can either do a postback, filter your view
and rebind, or try some javascript to hide the rest
of the grid items:

<script language="javascript" type="text/javascript">
function filterGrid ()
{
var myForm = document.forms["FormNameHere"];
var table = document.getElementById("GridNameHere");
if (table == null)
return;

//hide the rest of grid item except for the top 5 (grid index
starts at 0)
for (var r = 5; r < table.rows.length; r++)
{
table.rows[r].style.display = 'none';
}
}
</script>

HTH,
Quoc Linh
 
G

Guest

I generally do my sorting in a dataview

DataTable statetable = StateDataSet.Tables[0];
DataView dv = new DataView(statetable);
dv.sort = "name of field to sort by";
GridView2.DataSource = dv;
GridView2.DataBind();
 
C

Cindy Lee

Well I want to use the gridview sorting so I don't have to worry about all
the different parameters.
I also don't want to have to re-query everything.

The sorting works on my gridview just fine with my sorting functions. But
it's just when I click the button, it doesn't keep the old settings. Is
there a way to take the current gridview and make that the new datasource or
remember the last sort?



Quoc Linh said:
When I click the button it works and I can see all the rows, but I don't get
the right sort order:

This is on the page load, so the sorting

DataTable statetable = StateDataSet.Tables[0];
GridView2.DataSource = statetable;
GridView2.DataBind();

Here's by button click function

protected void Button1_Click(object sender, EventArgs e)
{
GridView2.AllowPaging = false;
GridView2.DataBind();
Button1.Visible = false;
Button2.Visible = true;
}

Cindy,

Bind to a view to sort your grid items:

StateDataSet.Tables[0].DefaultView.Sort = "FieldNameHere ASC"; //or
DESC
GridView2.DataSource = StateDataSet.Tables[0].DefaultView;
GridView2.DataBind();

Now, to display top 5, you can either do a postback, filter your view
and rebind, or try some javascript to hide the rest
of the grid items:

<script language="javascript" type="text/javascript">
function filterGrid ()
{
var myForm = document.forms["FormNameHere"];
var table = document.getElementById("GridNameHere");
if (table == null)
return;

//hide the rest of grid item except for the top 5 (grid index
starts at 0)
for (var r = 5; r < table.rows.length; r++)
{
table.rows[r].style.display = 'none';
}
}
</script>

HTH,
Quoc Linh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top