I use the GridViewControl. Here it is the code:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZzimaAuctionBLL;
using ZzimaAuctionCommon;
using System.Data;
namespace ZzimaAuctionUI.Controls.UserControls
{
public partial class GridViewControl : System.Web.UI.UserControl
{
#region Fields
#endregion
#region Properties
public event EventHandler CheckBoxCheckedChanged;
protected virtual void OnCheckBoxCheckedChanged()
{
if (CheckBoxCheckedChanged != null)
{
CheckBoxCheckedChanged(this, EventArgs.Empty);
}
}
public bool EnableCheckBoxColumn
{
get
{
return gvMain.Columns[0].Visible;
}
set
{
gvMain.Columns[0].Visible = value;
}
}
public Type ListClassType
{
get { return (Type)ViewState["ListClassName"]; }
set { ViewState["ListClassName"] = value; }
}
public string LoadMethodName
{
get { return ViewState["LoadMethodName"].ToString(); }
set { ViewState["LoadMethodName"] = value; }
}
public string GetRowsCountMethodName
{
get { return ViewState["GetRowsCountMethodName"].ToString(); }
set { ViewState["GetRowsCountMethodName"] = value; }
}
public int IDColumnIndex
{
get
{
if (ViewState["IDColumnIndex"] == null)
{
return -1;
}
return (int)ViewState["IDColumnIndex"];
}
set { ViewState["IDColumnIndex"] = value; }
}
public List<int> SelectedRowIDs
{
get
{
List<int> result = new List<int>();
var rows = gvMain.Rows;
for (int i = 0; i < rows.Count; ++i)
{
bool isChecked = ((from Control control in rows[i].Cells[0].Controls
where control.ID == "chbxSelect"
select control).Single() as CheckBox).Checked;
if (isChecked)
{
result.Add((int)gvMain.DataKeys[i].Value);
}
}
return result;
}
}
public int PageIndex
{
get
{
if (ViewState["PageIndex"] == null)
{
return 0;
}
return (int)ViewState["PageIndex"];
}
protected set
{
if (value >= 0)
{
ViewState["PageIndex"] = value;
}
}
}
public int PageSize
{
get
{
return gvMain.PageSize;
}
set
{
gvMain.PageSize = value;
}
}
public int PagesCount
{
get
{
if (ViewState["PagesCount"] == null)
{
return 0;
}
return (int)ViewState["PagesCount"];
}
protected set
{
if (value >= 0)
{
ViewState["PagesCount"] = value;
}
}
}
public int RecordsCount
{
get
{
if (ViewState["RecordsCount"] == null)
{
return 0;
}
return (int)ViewState["RecordsCount"];
}
protected set
{
if (value >= 0)
{
ViewState["RecordsCount"] = value;
}
}
}
public string CurrentSortExpression
{
get
{
if (ViewState["CurrentSortExpression"] == null)
{
return string.Empty;
}
return (string)ViewState["CurrentSortExpression"];
}
set
{
ViewState["CurrentSortExpression"] = value;
}
}
public SortDirection CurrentSortDirection
{
get
{
if (ViewState["CurrentSortDirection"] == null)
{
return SortDirection.Ascending;
}
return (SortDirection)ViewState["CurrentSortDirection"];
}
set
{
ViewState["CurrentSortDirection"] = value;
}
}
public HorizontalAlign HorizontalAlign
{
get
{
return gvMain.HorizontalAlign;
}
set
{
gvMain.HorizontalAlign = value;
}
}
public Unit Width
{
get
{
return Unit.Parse(tblMain.Style[HtmlTextWriterStyle.Width]);
}
set
{
tblMain.Style[HtmlTextWriterStyle.Width] = value.ToString();
}
}
#endregion
#region Methods
public override void DataBind()
{
BindGridView(PageIndex, CurrentSortExpression, CurrentSortDirection);
UpdatePagerInfo();
base.DataBind();
}
private void BindGridView(int startRowIndex, string sortExpression, SortDirection sortDirection)
{
object listObject = Activator.CreateInstance(ListClassType);
gvMain.DataSource = ListClassType.InvokeMember(
LoadMethodName, BindingFlags.InvokeMethod, null, listObject,
new object[] {
new DataOutputBO {
StartIndex = startRowIndex * PageSize,
PageSize = gvMain.PageSize,
SortExpression = CurrentSortExpression,
SortDirection = CurrentSortDirection}});
GetTotalRowCount(listObject);
gvMain.DataBind();
CurrentSortExpression = sortExpression;
CurrentSortDirection = sortDirection;
listObject = null;
}
private void TuneNavigationButtons()
{
lnkFirstPage.Enabled = lnkPrevPage.Enabled = (PageIndex > 0);
lnkLastPage.Enabled = lnkNextPage.Enabled = (PageIndex != PagesCount - 1);
}
private void UpdateSortIndicator(GridViewRow headerRow)
{
for (int i = 0; i < headerRow.Cells.Count; ++i)
{
if (gvMain.Columns[i].SortExpression == CurrentSortExpression && CurrentSortExpression != string.Empty)
{
Image sortIndicator = new Image();
sortIndicator.ImageUrl =
(CurrentSortDirection == SortDirection.Ascending)
? ("~/Design/Images/icon-sort1.gif")
: ("~/Design/Images/icon-sort2.gif");
sortIndicator.Style.Add("style", "vertical-align:middle;float:right");
headerRow.Cells[i].Controls.Add(sortIndicator);
}
}
}
private void UpdatePagerInfo()
{
lblRecordsNum.Text = RecordsCount.ToString();
lblPageInfo.Text = (PageIndex + 1).ToString();
lblMaxPages.Text = PagesCount.ToString();
}
protected void GetTotalRowCount(object listObject)
{
RecordsCount = (int)ListClassType.InvokeMember(
GetRowsCountMethodName, BindingFlags.InvokeMethod, null, listObject,
new object[] { });
PagesCount = RecordsCount / gvMain.PageSize + (RecordsCount % gvMain.PageSize > 0 ? 1 : 0);
}
protected void GetTotalRowCount()
{
object listObject = Activator.CreateInstance(ListClassType);
GetTotalRowCount(listObject);
listObject = null;
}
public void AddBoundIDField(string dataField, string sortExpression)
{
BoundField boundField = new BoundField();
if (dataField != string.Empty)
{
boundField.DataField = dataField;
}
if (sortExpression != string.Empty)
{
boundField.SortExpression = sortExpression;
}
boundField.Visible = false;
gvMain.Columns.Add(boundField);
gvMain.DataKeyNames = new string[] { dataField };
IDColumnIndex = gvMain.Columns.IndexOf(boundField);
}
public void AddBoundField(string dataField, string headerText, string sortExpression, Unit columnWidth)
{
BoundField boundField = new BoundField();
if (dataField != string.Empty)
{
boundField.DataField = dataField;
}
if (headerText != string.Empty)
{
boundField.HeaderText = headerText;
}
if (sortExpression != string.Empty)
{
boundField.SortExpression = sortExpression;
}
boundField.HeaderStyle.Width = columnWidth;
gvMain.Columns.Add(boundField);
}
public void AddBoundField(string dataField, string headerText, string sortExpression)
{
AddBoundField(dataField, headerText, sortExpression, Unit.Empty);
}
#endregion
#region Event Handlers
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
CheckBoxCheckedChanged = (EventHandler)Session["CheckBoxCheckedChanged"];
}
else
{
TuneNavigationButtons();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
Session["CheckBoxCheckedChanged"] = CheckBoxCheckedChanged;
}
protected void chbxSelect_OnCheckedChanged(object sender, EventArgs e)
{
OnCheckBoxCheckedChanged();
}
protected void Navigate(object sender, EventArgs e)
{
var button = (sender as IButtonControl);
switch (button.CommandArgument)
{
case "First":
PageIndex = 0;
break;
case "Last":
PageIndex = PagesCount - 1;
break;
case "Next":
if (PageIndex < PagesCount - 1)
{
PageIndex++;
}
break;
case "Prev":
if (PageIndex > 0)
{
PageIndex--;
}
break;
}
TuneNavigationButtons();
DataBind();
}
protected void gvMain_Sorting(object sender, GridViewSortEventArgs e)
{
if (CurrentSortExpression == e.SortExpression)
{
if (CurrentSortDirection == SortDirection.Ascending)
{
CurrentSortDirection = SortDirection.Descending;
}
else
{
CurrentSortDirection = SortDirection.Ascending;
}
}
else
{
CurrentSortExpression = e.SortExpression;
CurrentSortDirection = SortDirection.Ascending;
}
DataBind();
}
protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
UpdateSortIndicator(e.Row);
}
}
#endregion
}
}
In method to bind gridview I can pass just the name of listclass.
private void BindGridView(int startRowIndex, string sortExpression, SortDirection sortDirection)
{
object listObject = Activator.CreateInstance(ListClassType);
gvMain.DataSource = ListClassType.InvokeMember(
LoadMethodName, BindingFlags.InvokeMethod, null, listObject,
new object[] {
new DataOutputBO {
StartIndex = startRowIndex * PageSize,
PageSize = gvMain.PageSize,
SortExpression = CurrentSortExpression,
SortDirection = CurrentSortDirection}});
GetTotalRowCount(listObject);
gvMain.DataBind();
CurrentSortExpression = sortExpression;
CurrentSortDirection = sortDirection;
listObject = null;
}
How to rewrite or extend this method for I can bind my control with some object of listclass. Now there is a class f.e. TagBOList wich has a method
public List<tagbo> GetData()
.
But now I need to use GetData methods with some parameters or bind my control with concrete object of listclass. Which solution is more effective and How to solve this problem?