日期:2014-05-18  浏览次数:20591 次

2005里用gridview如何实现收缩、展开(带+ -号的那种)效果?
如题,之前看过孟子兄的文章,但我需要的是更复杂的效果,请给点例子,谢谢!

点击   +   号展开,点击   -   号收缩。

------解决方案--------------------
//控件主行显示按钮、子行不显示
protected void RecoveriesGridView_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
object PaymentSubCategoryId = recoveriesGridView.DataKeys[e.Row.DataItemIndex][ "PaymentSubCategoryId "];
if (!string.IsNullOrEmpty(WebConvert.ToString(PaymentSubCategoryId) )) {
((ImageButton)e.Row.FindControl( "hiddenImageButton ")).Visible = false;
Literal literal = new Literal();
literal.Text = WebConvert.ToString( "      ");
e.Row.Cells[0].Controls.AddAt(0, literal);
} else {
((ImageButton)e.Row.FindControl( "hiddenImageButton ")).CommandArgument = WebConvert.ToString(e.Row.DataItemIndex);
}
}
}

//收缩、展开按钮事件

protected void RecoveriesGridView_RowCommand(object sender, CommandEventArgs e) {
if (e.CommandName == "HiddenRows ") {
int rowId = WebConvert.ToInt32(e.CommandArgument);
object currentPaymentCategoryId=recoveriesGridView.DataKeys[rowId][ "PaymentCategoryId "];
for (int i = rowId + 1; i < recoveriesGridView.Rows.Count; i++) {
if (recoveriesGridView.DataKeys[i][ "PaymentCategoryId "].Equals(currentPaymentCategoryId)) {
recoveriesGridView.Rows[i].Visible = recoveriesGridView.Rows[i].Visible == false ? true : false;
}
}
((ImageButton)recoveriesGridView.Rows[rowId].FindControl( "hiddenImageButton ")).ImageUrl = ((ImageButton)recoveriesGridView.Rows[rowId].FindControl( "hiddenImageButton ")).ImageUrl == "~/Image/Collapse.jpg " ? "~/Image/Expand.jpg " : "~/Image/Collapse.jpg ";
}
}