當DataGrid同時具有分頁和排序功能時應注意在重新綁定數據源時
MyDataGrid
CurrentPageIndex=
; 下面給實現以上功能的原碼
也就不多綴了aspx中包含有DataGrid和控制其數據源變化的dropdownlist
DataGrid代碼
<asp:datagrid id=
MyDataGrid
runat=
server
BorderColor=
#CCCCCC
Font
Size=
%
HorizontalAlign=
Center
AutoGenerateColumns=
False
OnDeleteCommand=
MyDataGrid_Delete
OnSortCommand=
Sort_Grid
OnPageIndexChanged=
MyDataGrid_PageIndexChanged
DataKeyField=
ACC_NO
PagerStyle
Position=
Bottom
PagerStyle
HorizontalAlign=
Center
PagerStyle
Mode=
NextPrev
PageSize=
AllowSorting=
True
AllowPaging=
True
CellPadding=
Width=
%
>
<AlternatingItemStyle BackColor=
#E
E
E
></AlternatingItemStyle>
<HeaderStyle Font
Bold=
True
Wrap=
False
ForeColor=
White
BackColor=
#
></HeaderStyle>
<Columns>
<asp:ButtonColumn Text=
口
CommandName=
Delete
></asp:ButtonColumn>
<asp:BoundColumn DataField=
NO
SortExpression=
NO
ReadOnly=
True
HeaderText=
序號
></asp:BoundColumn>
<asp:BoundColumn DataField=
ID
SortExpression=
ID
HeaderText=
ID
></asp:BoundColumn>
<asp:BoundColumn DataField=
NAME
SortExpression=
NAME
HeaderText=
名稱
></asp:BoundColumn>
<asp:BoundColumn DataField=
C_NAME
SortExpression=
C_NAME
HeaderText=
各科名稱
></asp:BoundColumn>
<asp:BoundColumn DataField=
FLG
SortExpression=
FLG
HeaderText=
項目
></asp:BoundColumn>
</Columns>
<PagerStyle NextPageText=
下
件
PrevPageText=
返回
HorizontalAlign=
Center
></PagerStyle>
</asp:datagrid>
dropdownlist代碼
<asp:dropdownlist id=
ddlWk
Runat=
server
AutoPostBack=
True
Enabled=
False
>
<asp:ListItem Value=
>東京</asp:ListItem>
<asp:ListItem Value=
>九州</asp:ListItem>
<asp:ListItem Value=
>北海道</asp:ListItem>
<asp:ListItem Value=
>四國</asp:ListItem>
</asp:dropdownlist>
aspx
cs文件代碼核心如下
private void Page_Load(object sender
System
EventArgs e)
{
if(!IsPostBack)
{
Session[
WP
] =
;
ddlWk_getS();
BindGrid();
}
}
private void ddlWk_getS()
{
switch (Session[
WP
]
ToString())
{
case
:ddlWk
SelectedIndex=
;
break;
case
:ddlWk
SelectedIndex=
;
break;
case
:ddlWk
SelectedIndex=
;
break;
case
:ddlWk
SelectedIndex=
;
break;
default:ddlWk
SelectedIndex=
;
break;
}
}
protected void BindGrid()
{
MyDataGrid
DataSource=GetData()
Tables[
vCO
]
DefaultView;
MyDataGrid
DataBind();
//COUNT
Text=MyDataGrid
Columns
Count
ToString();
}
/// <summary>
/// 返回Data
/// </summary>
///<returns></returns>
private DataSet GetData()
{
string strConn=(String) ((NameValueCollection) Context
GetConfig(
system
web/database
))[
strConn
];
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(
sp_C
conn);
cmd
CommandType=CommandType
StoredProcedure;
cmd
Parameters
Add(
@place
SqlDbType
VarChar
);
cmd
Parameters[
@place
]
Value=Session[
WP
]
ToString();
conn
Open();
SqlDataAdapter da = new SqlDataAdapter();
da
SelectCommand=cmd;
DataSet ds=new DataSet();
da
Fill(ds
vCO
);
Count
Text=
ヒット
+ds
Tables[
vCO
]
Rows
Count
ToString()+
件
;
return ds;
}
}
/// <summary>
///從DataSet中除一
/// </summary>
/// <param name=
sender
></param>
/// <param name=
E
></param>
protected void MyDataGrid_Delete(Object sender
DataGridCommandEventArgs E)
{
String strID=MyDataGrid
DataKeys[(int)E
Item
ItemIndex]
ToString();
//刪除操作
}
/// <summary>
/// 分頁操作
/// </summary>
/// <param name=
sender
></param>
/// <param name=
e
></param>
protected void MyDataGrid_PageIndexChanged(object source
DataGridPageChangedEventArgs e)
{
MyDataGrid
CurrentPageIndex=e
NewPageIndex;
BindGrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name=
sender
></param>
/// <param name=
e
></param>
protected void Sort_Grid(object sender
DataGridSortCommandEventArgs e)
{
DataView dv= new DataView(GetData()
Tables[
vCO
]);
dv
Sort= e
SortExpression
ToString();
MyDataGrid
DataSource=dv;
MyDataGrid
DataBind();
}
#region Web override protected void OnInit(EventArgs e)
{
// //
InitializeComponent();
base
OnInit(e);
}
/// <summary> /// </summary>
private void InitializeComponent()
{
this
ddlWk
SelectedIndexChanged += new System
EventHandler(this
ddlWk_SelectedIndexChanged);
this
Load += new System
EventHandler(this
Page_Load);
}
#endregion
private void ddlWk_SelectedIndexChanged(object sender
System
EventArgs e)
{
Session[
WP
]=ddlWk
SelectedValue;
MyDataGrid
CurrentPageIndex=
;//沒有這一句
當該頁碼超出其他數據源的范圍時會出錯
BindGrid();
Response
Write(
<script language=
javascript
>nuframe
location
reload();</script>
);
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21675.html