有時候會想要一行一行地顯現gridview 即按 新增 , 則增加一列
例如在檔案上傳時,現只出現一列,使用者可以按 新增上傳欄位來上傳多個檔案
於是研究寫了這樣一個gridview
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
namespace Naonet
{
[ToolboxData("<{0}:IncrementalGridView runat=server>{0}:IncrementalGridView>")]
public partial class IncrementalGridView : GridView
{
string _addRowText = "新增...";
[DefaultValue("新增...")]
public virtual string AddRowText
{
get { return _addRowText; }
set { _addRowText = value; }
}
Literal _litScript = new Literal();
HyperLink _addRowLink = new HyperLink();
public HyperLink AddRowLink
{
get { return _addRowLink; }
}
int _totalRowCount = 0;
///
/// 總列數, 0 為不設定
///
[DefaultValue(0)]
public virtual int TotalRowCount
{
get
{
return _totalRowCount;
}
set
{
if (value <= 0) _totalRowCount = 0;
else _totalRowCount = value;
}
}
///
/// 初始列數
///
int _initialRowCount = 1;
[DefaultValue(1)]
public int InitialRowCount
{
get
{
if (_initialRowCount > TotalRowCount && TotalRowCount != 0)
return TotalRowCount;
else return _initialRowCount;
}
set
{
if (value <= 0) _initialRowCount = 1;
else _initialRowCount = value;
}
}
public IncrementalGridView()
: base()
{
this.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(IncrementalGridView_RowDataBound);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
GridView gv = (GridView)this;
if (!gv.Page.IsPostBack)
{
if (DataSource == null && !IsBoundUsingDataSourceID)
{
if (TotalRowCount > 0)
DataSource = new object[TotalRowCount];
else DataSource = new object[1];
DataBind();
}
}
if (this.FooterRow != null)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
row.Cells.Clear();
TableCell cell = new TableCell();
cell.ColumnSpan = Columns.Count;
cell.Controls.Add(_addRowLink);
cell.Controls.Add(_litScript);
cell.Width = Unit.Percentage(100);
row.Cells.Add(cell);
TableRowCollection rows = (TableRowCollection)FooterRow.Parent.GetType().GetProperty("Rows").GetValue(FooterRow.Parent, new object[] { });
rows.Add(row);
}
/*
* 設定為顯示增加連結 且
*
* */
_addRowLink.NavigateUrl = string.Format("javascript:addGridViewRow{0}()", gv.ClientID);
_addRowLink.Text = AddRowText;
_litScript.Text = string.Format(@"
", gv.ClientID,
_addRowLink.ClientID);
if (Rows.Count > 1 &&
(
(
TotalRowCount > 0 && (
(TotalRowCount >= Rows.Count && Rows.Count > InitialRowCount) ||
(Rows.Count > TotalRowCount && TotalRowCount > InitialRowCount)
)
) ||
(
TotalRowCount == 0 && Rows.Count > InitialRowCount
)
)
)
{
}
else
{
_addRowLink.Visible = false;
}
}
void IncrementalGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = sender as GridView;
if (e.Row.RowIndex + 1 > InitialRowCount)
e.Row.Style[HtmlTextWriterStyle.Display] = "none";
else e.Row.Style[HtmlTextWriterStyle.Display] = "block";
if (TotalRowCount != 0 && e.Row.RowIndex + 1 > TotalRowCount)
e.Row.Visible = false;
}
}
}
}
本文作者:佚名 来源:http://city.udn.com/57239/2291682