How to add dropdownlist into Gridview in asp.net

2019-08-28 02:28发布

问题:

I've a grid control, in that I want to add dropdownlist box. The pupose of the dropdown is to show the page in in the grid, If I select 1 the gridview page1 or 2 means 2nd page in the grid etc, that drop down should not view while I'm going to take print out

My current code:

protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e) 
{ 
    GridViewRow pagerRow = gvDetails.BottomPagerRow; 
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); 
    gvDetails.PageIndex = pageList.SelectedIndex; 
}

回答1:

to add a drop down to a grid cell you should use ItemTemplates and especially the edit mode ItemTemplate, see here for a full example: Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control

I am anyway not sure about your usage of it, if you need to move from one page to another you should use the Footer/Pager control or you can have a drop down outside the GridView where you select the page then you change the Current PageIndex of the Grid and call DataBind again.



回答2:

You'd need to use the PagerTemplate as described here: GridView.PagerTemplate Property

To make your dropdown navigate to a specific page, pass the integer number of that page as the CommandArgument on postback.

the example on that page describes how to use a DropDownList.



回答3:

You are probably better off using Paging - see Paging in a GridView Web Server Control



回答4:

Not showing the dropdown on a printed page can be done via a print specific style sheet. This is a reasonable description of what you need to know: http://webdesign.about.com/cs/css/a/aa042103a.htm

The other part, try a customer paging template, see here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate.aspx