By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

DataGrid control events like Sort, page, Item not getting fired...

P: n/a
Hello All,

I am working to create a generic datagrid which accepts a
datasource(ListData - This is our own datatype) and depending on the calling
program customizes itself for sorting, paginantion or accepting the add and
remove item events.

What i am observing is that none of the vents are happening... (Sort, page,
or item). I am sure I am missing something basic here... Need help... Thanks
much

Guns
www.goondemand.com
----------------------------------------------------------------------------
--------------------------
<%@ Control Language="C#" %>
<%@ import Namespace="goondemand.framework" %>
<%@ import Namespace="goondemand.framework.types" %>
<%@ import Namespace="goondemand.ui.util" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
// private elements

private String accesskey="";
private String addbuttontext="+";
private String align = "left";
private bool allowsorting=false;
private bool allowpaging=false;
private String classname = "grid-body-default";
private int colspan=0;
private bool displayrownumber=false;
private bool editable=false;
private String evenrowcolor = "White";
private String headertext="";
private bool hideborder=true;
private ListData listdata;
private String nextpagetext="Next";
private String oddrowcolor = "WhiteSmoke";
private int pagesize=10;
private String pagestyle="Numeric";
private String prevpagetext="Prev";
private String removebuttontext = "-";
private bool rowtag = false;
private String rownumberlabel="S_No";
private bool showaddbutton=true;
private bool showremovebutton = true;
private bool showheader=true;
private bool showfooter=false;
private int spacesfromleft = GlobalConfig.SPACESFROMLEFT;
private String tooltip="";
private int width = 0;
private bool visible=true;

// implementation elemnts
private String sortfield="";
private bool ascending=true;
private String selectedsortfield;
private ArrayList hyperlinkcolumns = new ArrayList();
private ArrayList navigatetoURL = new ArrayList();
private ArrayList sortablecolumns = new ArrayList();
private ArrayList invisiblecolumns = new ArrayList();
private int itemtoaddororemove;
private bool itemremove=false;
private bool itemadd = false;
private String associatedURL="";

// the dataview that contains the data for this grid
private DataView dv;

// PROPERTIES
public String ACCESSKEY{
get{
return accesskey;
}

set{
accesskey = value;
localgrid.AccessKey =accesskey;
}
}

public String ADDBUTTONTEXT{
get{
return addbuttontext;
}

set{
addbuttontext = value;
}
}

public String ALIGN{
get{
return align;
}
set{
align = value;
if(align.ToLower() == "left"){
localgrid.PagerStyle.HorizontalAlign=HorizontalAli gn.Left;
}
else{
localgrid.PagerStyle.HorizontalAlign=HorizontalAli gn.Right;
}
}
}

public bool ALLOWPAGING{
get{
return allowpaging;
}

set{
allowpaging = value;
localgrid.AllowPaging = allowpaging;
if(allowpaging){
if(pagesize==0){
pagesize=10;
}
localgrid.PageSize = pagesize;
if(pagestyle.ToUpper() == "NUMERIC"){
localgrid.PagerStyle.Mode = PagerMode.NumericPages;
}
localgrid.PagerStyle.NextPageText = nextpagetext;
localgrid.PagerStyle.PrevPageText = prevpagetext;
}
}
}
public bool ALLOWSORTING{
get{
return allowsorting;
}

set{
allowsorting = value;
localgrid.AllowSorting = allowsorting;
}
}

public String CLASSNAME{
get{
return classname;
}

set{
classname = value;
localgrid.CssClass=classname;
}
}

public int COLSPAN{
get{
return colspan;
}

set{
colspan = value;
}
}
public bool DISPLAYROWNUMBER{
get{
return displayrownumber;
}

set{
displayrownumber = value;
}
}

public bool EDITABLE{
get{
return editable;
}

set{
editable = value;
}
}

public String EVENROWCOLOR{
get{
return evenrowcolor;
}

set{
evenrowcolor = value;
if(evenrowcolor.Length > 0){

localgrid.AlternatingItemStyle.BackColor=System.Dr awing.Color.FromName(evenr
owcolor);
}
}
}

public String HEADERTEXT{
get{
return headertext;
}

set{
headertext = value;
}
}

public bool HIDEBORDER{
get{
return hideborder;
}

set{
hideborder = value;
if (hideborder){
localgrid.BorderStyle = BorderStyle.Solid;
}
else{
localgrid.BorderStyle = BorderStyle.None;
}
}
}

public ListData LISTDATA{
get{
return listdata;
}

set{
listdata = value;
}
}

public String NEXTTEXT{
get{
return nextpagetext;
}

set{
nextpagetext = value;
if (nextpagetext.Length > 0){
localgrid.PagerStyle.NextPageText = nextpagetext;
}
}
}

public String ODDROWCOLOR{
get{
return oddrowcolor;
}

set{
oddrowcolor = value;
if(oddrowcolor.Length > 0){

localgrid.ItemStyle.BackColor=System.Drawing.Color .FromName(oddrowcolor);
}
}
}

public int PAGESIZE{
get{
return pagesize;
}

set{
pagesize = value;
if(pagesize == 0){
localgrid.PageSize = 10;
}
}
}

public String PAGESTYLE{
get{
return pagestyle;
}

set{
pagestyle = value;
if (pagestyle.ToUpper() == "NUMERIC"){
localgrid.PagerStyle.Mode = PagerMode.NumericPages;
}
else{
localgrid.PagerStyle.Mode = PagerMode.NextPrev;
}
}
}

public String PREVTEXT{
get{
return prevpagetext;
}

set{
prevpagetext = value;
if (prevpagetext.Length > 0){
localgrid.PagerStyle.PrevPageText = prevpagetext;
}
}
}

public String REMOVEBUTTONTEXT{
get{
return removebuttontext;
}

set{
removebuttontext = value;
}
}

public bool ROWTAG{
get{
return rowtag;
}

set{
rowtag = value;
}
}

public String ROWNUMBERLABEL{
get{
return rownumberlabel;
}

set{
rownumberlabel = value;
}
}

public bool SHOWADDBUTTON{
get{
return showaddbutton;
}

set{
showaddbutton = value;
}
}

public bool SHOWREMOVEBUTTON{
get{
return showremovebutton;
}

set{
showremovebutton = value;
}
}

public bool SHOWHEADER{
get{
return showheader;
}

set{
showheader = value;
localgrid.ShowHeader = showheader;
}
}

public bool SHOWFOOTER{
get{
return showfooter;
}

set{
showfooter = value;
localgrid.ShowFooter = showfooter;
}
}

public int SPACESFROMLEFT{
get{
return spacesfromleft;
}
set{
spacesfromleft = value;
}
}

public String TOOLTIP{
get{
return tooltip;
}
set{
tooltip = value;
localgrid.ToolTip = tooltip;
}
}

public int WIDTH{
get{
return width;
}

set{
width = value;
if(width >0){
localgrid.Width = width;
}
}
}

public bool VISIBLE{
get{
return visible;
}

set{
visible = value;
}
}
// Implementation Private Methods
protected void CreateDataSource() {

try{
Log.Instance.write(Log.INFO, this.ToString(), "Createdatasource
called");
DataTable dt = new DataTable();
DataRow dr;
RowData row;
int i;
int j;

if(null != listdata){
// first get the header and add the columns to the data table
row = (RowData)listdata.getRowData(0);
for(i=0; i< row.getFieldCount(); i++){
if(displayrownumber){ // then add an extra row header as S No.
if(i == 0){
dt.Columns.Add(new DataColumn(rownumberlabel, typeof(String)));
}
}

switch(System.Type.GetTypeCode(row.getFieldValue(i ).GetType())){

case System.TypeCode.Boolean:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(bool )));
break;

case System.TypeCode.DateTime:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Date Time)));
break;

case System.TypeCode.Decimal:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(bool )));
break;

case System.TypeCode.Int16:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int1 6)));
break;

case System.TypeCode.Int32:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int3 2)));
break;

case System.TypeCode.Int64:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int6 4)));
break;

case System.TypeCode.String:
default:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Stri ng)));
break;
}
}

// then add the data rows to the data table

for(i=1; i<listdata.getCount(); i++){
row = (RowData)listdata.getRowData(i);
dr = dt.NewRow();
for(j=0; j< row.getFieldCount(); j++){
if(displayrownumber){ // then add an extra row header as S No.
if(j == 0){
dr[j] = i;
}
dr[j+1] = row.getFieldValue(j);
}
else{
dr[j] = row.getFieldValue(j);
}
}
dt.Rows.Add(dr);
}
}

dv = new DataView(dt);
}
catch (Exception ex){
Log.Instance.write(Log.EXCEPTION,this.ToString(), "Exception
occured in method createdatasource with message "+ ex.Message);
throw(ex);
}
}

protected bool IsColumnSortable(String columnname){
bool matchfound = false;
int i;
if(sortablecolumns.Count == 0){
return false;
}
for(i=0; i<sortablecolumns.Count; i++){
if(((String)sortablecolumns[i]).ToLower() == columnname.ToLower()){
matchfound = true;
break;
}
}
return matchfound;

}

protected bool IsColumnHyperlink(String colname){
bool matchfound = false;
int i;
if (hyperlinkcolumns.Count == 0){
return false;
}

for(i=0; i<hyperlinkcolumns.Count; i++){
if(((String)hyperlinkcolumns[i]).ToLower() == colname.ToLower()){
matchfound = true;
associatedURL = (String) navigatetoURL[i];
break;
}
}
return matchfound;
}

protected bool IsColumnInvisible(String colname){
bool matchfound = false;
int i;
if (invisiblecolumns.Count == 0){
return false;
}

for(i=0; i<invisiblecolumns.Count; i++){
if(((String)invisiblecolumns[i]).ToLower() == colname.ToLower()){
matchfound = true;
break;
}
}
return matchfound;
}
protected void BindGrid(bool sortit) {
Log.Instance.write(Log.INFO, this.ToString(), "Bind Grid called");
if (sortit){
SortGrid();
}
localgrid.EnableViewState = true;
localgrid.DataSource = dv;
localgrid.DataBind();

}
protected void GenerateGrid(){
int i;
HyperLinkColumn hlink;
BoundColumn bcolumn;
ButtonColumn button;
TemplateColumn tcolumn;
TextBox textbox;

String columnname="";

if(localgrid.Columns.Count >0){
localgrid.Columns.Clear();
}
if(null != listdata){

RowData row = (RowData)listdata.getRowData(0);
for(i=0; i < row.getFieldCount(); i++){
// add the row number column if its ON
if(displayrownumber){
if(i==0){
bcolumn = new BoundColumn();
bcolumn.DataField = rownumberlabel;
bcolumn.HeaderText = rownumberlabel;
bcolumn.ReadOnly = true;
localgrid.Columns.Add(bcolumn);
}
}

columnname = (String)row.getFieldName(i);
Log.Instance.write(Log.INFO, this.ToString(), "Fieldname is :" +
columnname);

if(editable){ // the grid is editable

tcolumn = new TemplateColumn();
tcolumn.HeaderText = (String)row.getFieldLabel(columnname);
tcolumn.ItemTemplate = new DataGridTemplate(ListItemType.Item,
columnname);
tcolumn.EditItemTemplate = new DataGridTemplate(ListItemType.EditItem,
columnname);
localgrid.Columns.Add(tcolumn);
}
else{ // the grid is read only
if(IsColumnHyperlink(columnname)){
hlink = new HyperLinkColumn();
hlink.DataNavigateUrlField = columnname;
hlink.DataNavigateUrlFormatString=associatedURL;
//"detailspage.aspx?id={0}";
hlink.DataTextField=columnname;
hlink.HeaderText = (String)row.getFieldLabel(columnname);
hlink.Visible = !(IsColumnInvisible(columnname));
if(IsColumnSortable(columnname)){
hlink.SortExpression = columnname;
}
hlink.Target="_new";
localgrid.Columns.Add(hlink);
}
else{
bcolumn = new BoundColumn();
bcolumn.DataField = columnname;
bcolumn.HeaderText = (String)row.getFieldLabel(columnname);
if(IsColumnSortable(columnname)){
bcolumn.SortExpression = columnname;
}
bcolumn.ReadOnly = true;
bcolumn.Visible = !(IsColumnInvisible(columnname));
localgrid.Columns.Add(bcolumn);
}
}
}

// now add the ADD and Remove button(s)
if(showaddbutton){
button = new ButtonColumn();
button.Text = addbuttontext;
//button.HeaderText ="Click to Add";
button.CommandName = "localgrid_ADD";
button.ButtonType = ButtonColumnType.PushButton;
localgrid.Columns.Add(button);
}

if(showremovebutton){
button = new ButtonColumn();
button.Text = removebuttontext;
//button.HeaderText ="Click to Remove";
button.CommandName = "localgrid_REMOVE";
button.ButtonType = ButtonColumnType.PushButton;
localgrid.Columns.Add(button);
}
}
Log.Instance.write(Log.INFO, this.ToString(), "total columns in grid = " +
localgrid.Columns.Count);

}

protected void SortGrid(){
if(sortfield == selectedsortfield){
ascending = !(ascending);
}
else{
ascending = true;
}

selectedsortfield = sortfield;

if(ascending){
//dv.Sort = sortfield + " ASC";
dv.Sort = sortfield;
Log.Instance.write(Log.INFO, this.ToString(), "ascending is true and
sortfield is " + sortfield + " selectedSortfield is " + selectedsortfield);
}
else{
//dv.Sort = sortfield + " DESC";
dv.Sort = sortfield;
Log.Instance.write(Log.INFO, this.ToString(), "ascending is false and
sortfield is " + sortfield + " selectedSortfield is " + selectedsortfield);
}
}

// ------------ EVENTS -------------------------------------------------

void Page_Load(Object sender, EventArgs e) {
Log.Instance.write(Log.INFO, this.ToString(), "EVENT:: Page_load for
datagridcontrol called");

if(!Page.IsPostBack){
localgrid.EnableViewState = true;
Log.Instance.write(Log.INFO, this.ToString(), "Call is not a post back");
if (sortfield.Length == 0) {
RowData row = (RowData)listdata.getRowData(0);
sortfield = (String)row.getFieldName(0); // default - the first
field in the listdata
selectedsortfield = sortfield;
}

localgrid.ItemStyle.BackColor=System.Drawing.Color .FromName(oddrowcolor);

localgrid.AlternatingItemStyle.BackColor=System.Dr awing.Color.FromName(evenr
owcolor);

Log.Instance.write(Log.INFO, this.ToString(), "Value of Sort field is:" +
sortfield + " and value of selected sort field is " + selectedsortfield );
localgrid.CssClass= classname;
GenerateGrid();
CreateDataSource();
BindGrid(false);

localgrid.ShowHeader = showheader;
localgrid.ShowFooter = showfooter;
localgrid.ToolTip = tooltip;
}
}

void localgrid_Page(Object sender, DataGridPageChangedEventArgs e) {
try{
Log.Instance.write(Log.INFO, this.ToString(), "localgrid_page event
called");
localgrid.CurrentPageIndex = e.NewPageIndex;
Log.Instance.write(Log.INFO, this.ToString(), "Pageindex on page event is
: " + localgrid.CurrentPageIndex);
BindGrid(false);
}

catch (Exception ex){
Log.Instance.write(Log.EXCEPTION,this.ToString(), "Exception occured in
event localgrid_page with message "+ ex.Message);
throw(ex);
}
}

void localgrid_Sort(Object sender, DataGridSortCommandEventArgs e) {
Log.Instance.write(Log.INFO, this.ToString(), "localgrid_sort called");
sortfield = (string)e.SortExpression;
Log.Instance.write(Log.INFO, this.ToString(), "Sort field in sort event
is:" + sortfield);
BindGrid(true);
}

void localgrid_Item(Object sender, DataGridCommandEventArgs e) {

if (((LinkButton)e.CommandSource).CommandName == "localgrid_ADD") {
itemadd = true;
itemtoaddorremove = e.Item
}

if (((LinkButton)e.CommandSource).CommandName == "localgrid_REMOVE") {

}
}

// --------- PUBLIC METHODS -------------------------------------------
public void MakeColumnAsHyperlink(String columnname, String URL){
if (columnname.Length > 0){
// add the column name to the arraylist of hyperlink columns
hyperlinkcolumns.Add(columnname);
navigatetoURL.Add(URL);
}
}

public void MakeColumnAsSortable(String columnname){
if (columnname.Length > 0){
sortablecolumns.Add(columnname);
}
}

public void MakeColumnInvisible(String columnname){
if (columnname.Length > 0){
invisiblecolumns.Add(columnname);
}
}

public String getBeginTag(){
String tag = "";

tag ="<td ";
if(rowtag){
tag = "<TR><TD ";
}
if(colspan > 1){
tag = tag + " colspan=\"" + colspan + "\"";
}
tag = tag + " >";

if(headertext.Length >0){
tag = tag + "<TABLE width=\"100%\"><TR><TD>" +
Misc.Instance.getSpaces(spacesfromleft) + headertext + "</TD></TR>";
tag = tag + "<TR><TD>";
}

return tag;
}

public String getEndTag(){
String tag = "</td>";
if(headertext.Length > 0){
tag = tag + "</TR></TABLE></TD>";
}
if(rowtag){
tag = tag + "</TR>";
}

return tag;
}

</script>
<%=getBeginTag()%>
<asp:DataGrid id="localgrid" name="localgrid" runat="server"
OnPageIndexChanged="localgrid_Page"
OnSortCommand="localgrid_Sort"
OnItemCommand="localgrid_Item"
AutoGenerateColumns="false" />
<%=getEndTag()%>
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Just keep in mind, then these events occur, is postback... so make sure
your page lifecycle works with your model.

Eg, if you rebuild the grid on each postback, and dont have viewstate
enable, it should break. Otherwise when the postback even occurs, and cant
"re-hydrate" the grid, your event bindings will not be there.

Weston Weems

"Gunjan Garg" <bu*********@hotmail.com> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
Hello All,

I am working to create a generic datagrid which accepts a
datasource(ListData - This is our own datatype) and depending on the
calling
program customizes itself for sorting, paginantion or accepting the add
and
remove item events.

What i am observing is that none of the vents are happening... (Sort,
page,
or item). I am sure I am missing something basic here... Need help...
Thanks
much

Guns
www.goondemand.com
----------------------------------------------------------------------------
--------------------------
<%@ Control Language="C#" %>
<%@ import Namespace="goondemand.framework" %>
<%@ import Namespace="goondemand.framework.types" %>
<%@ import Namespace="goondemand.ui.util" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
// private elements

private String accesskey="";
private String addbuttontext="+";
private String align = "left";
private bool allowsorting=false;
private bool allowpaging=false;
private String classname = "grid-body-default";
private int colspan=0;
private bool displayrownumber=false;
private bool editable=false;
private String evenrowcolor = "White";
private String headertext="";
private bool hideborder=true;
private ListData listdata;
private String nextpagetext="Next";
private String oddrowcolor = "WhiteSmoke";
private int pagesize=10;
private String pagestyle="Numeric";
private String prevpagetext="Prev";
private String removebuttontext = "-";
private bool rowtag = false;
private String rownumberlabel="S_No";
private bool showaddbutton=true;
private bool showremovebutton = true;
private bool showheader=true;
private bool showfooter=false;
private int spacesfromleft = GlobalConfig.SPACESFROMLEFT;
private String tooltip="";
private int width = 0;
private bool visible=true;

// implementation elemnts
private String sortfield="";
private bool ascending=true;
private String selectedsortfield;
private ArrayList hyperlinkcolumns = new ArrayList();
private ArrayList navigatetoURL = new ArrayList();
private ArrayList sortablecolumns = new ArrayList();
private ArrayList invisiblecolumns = new ArrayList();
private int itemtoaddororemove;
private bool itemremove=false;
private bool itemadd = false;
private String associatedURL="";

// the dataview that contains the data for this grid
private DataView dv;

// PROPERTIES
public String ACCESSKEY{
get{
return accesskey;
}

set{
accesskey = value;
localgrid.AccessKey =accesskey;
}
}

public String ADDBUTTONTEXT{
get{
return addbuttontext;
}

set{
addbuttontext = value;
}
}

public String ALIGN{
get{
return align;
}
set{
align = value;
if(align.ToLower() == "left"){
localgrid.PagerStyle.HorizontalAlign=HorizontalAli gn.Left;
}
else{
localgrid.PagerStyle.HorizontalAlign=HorizontalAli gn.Right;
}
}
}

public bool ALLOWPAGING{
get{
return allowpaging;
}

set{
allowpaging = value;
localgrid.AllowPaging = allowpaging;
if(allowpaging){
if(pagesize==0){
pagesize=10;
}
localgrid.PageSize = pagesize;
if(pagestyle.ToUpper() == "NUMERIC"){
localgrid.PagerStyle.Mode = PagerMode.NumericPages;
}
localgrid.PagerStyle.NextPageText = nextpagetext;
localgrid.PagerStyle.PrevPageText = prevpagetext;
}
}
}
public bool ALLOWSORTING{
get{
return allowsorting;
}

set{
allowsorting = value;
localgrid.AllowSorting = allowsorting;
}
}

public String CLASSNAME{
get{
return classname;
}

set{
classname = value;
localgrid.CssClass=classname;
}
}

public int COLSPAN{
get{
return colspan;
}

set{
colspan = value;
}
}
public bool DISPLAYROWNUMBER{
get{
return displayrownumber;
}

set{
displayrownumber = value;
}
}

public bool EDITABLE{
get{
return editable;
}

set{
editable = value;
}
}

public String EVENROWCOLOR{
get{
return evenrowcolor;
}

set{
evenrowcolor = value;
if(evenrowcolor.Length > 0){

localgrid.AlternatingItemStyle.BackColor=System.Dr awing.Color.FromName(evenr
owcolor);
}
}
}

public String HEADERTEXT{
get{
return headertext;
}

set{
headertext = value;
}
}

public bool HIDEBORDER{
get{
return hideborder;
}

set{
hideborder = value;
if (hideborder){
localgrid.BorderStyle = BorderStyle.Solid;
}
else{
localgrid.BorderStyle = BorderStyle.None;
}
}
}

public ListData LISTDATA{
get{
return listdata;
}

set{
listdata = value;
}
}

public String NEXTTEXT{
get{
return nextpagetext;
}

set{
nextpagetext = value;
if (nextpagetext.Length > 0){
localgrid.PagerStyle.NextPageText = nextpagetext;
}
}
}

public String ODDROWCOLOR{
get{
return oddrowcolor;
}

set{
oddrowcolor = value;
if(oddrowcolor.Length > 0){

localgrid.ItemStyle.BackColor=System.Drawing.Color .FromName(oddrowcolor);
}
}
}

public int PAGESIZE{
get{
return pagesize;
}

set{
pagesize = value;
if(pagesize == 0){
localgrid.PageSize = 10;
}
}
}

public String PAGESTYLE{
get{
return pagestyle;
}

set{
pagestyle = value;
if (pagestyle.ToUpper() == "NUMERIC"){
localgrid.PagerStyle.Mode = PagerMode.NumericPages;
}
else{
localgrid.PagerStyle.Mode = PagerMode.NextPrev;
}
}
}

public String PREVTEXT{
get{
return prevpagetext;
}

set{
prevpagetext = value;
if (prevpagetext.Length > 0){
localgrid.PagerStyle.PrevPageText = prevpagetext;
}
}
}

public String REMOVEBUTTONTEXT{
get{
return removebuttontext;
}

set{
removebuttontext = value;
}
}

public bool ROWTAG{
get{
return rowtag;
}

set{
rowtag = value;
}
}

public String ROWNUMBERLABEL{
get{
return rownumberlabel;
}

set{
rownumberlabel = value;
}
}

public bool SHOWADDBUTTON{
get{
return showaddbutton;
}

set{
showaddbutton = value;
}
}

public bool SHOWREMOVEBUTTON{
get{
return showremovebutton;
}

set{
showremovebutton = value;
}
}

public bool SHOWHEADER{
get{
return showheader;
}

set{
showheader = value;
localgrid.ShowHeader = showheader;
}
}

public bool SHOWFOOTER{
get{
return showfooter;
}

set{
showfooter = value;
localgrid.ShowFooter = showfooter;
}
}

public int SPACESFROMLEFT{
get{
return spacesfromleft;
}
set{
spacesfromleft = value;
}
}

public String TOOLTIP{
get{
return tooltip;
}
set{
tooltip = value;
localgrid.ToolTip = tooltip;
}
}

public int WIDTH{
get{
return width;
}

set{
width = value;
if(width >0){
localgrid.Width = width;
}
}
}

public bool VISIBLE{
get{
return visible;
}

set{
visible = value;
}
}
// Implementation Private Methods
protected void CreateDataSource() {

try{
Log.Instance.write(Log.INFO, this.ToString(), "Createdatasource
called");
DataTable dt = new DataTable();
DataRow dr;
RowData row;
int i;
int j;

if(null != listdata){
// first get the header and add the columns to the data table
row = (RowData)listdata.getRowData(0);
for(i=0; i< row.getFieldCount(); i++){
if(displayrownumber){ // then add an extra row header as S No.
if(i == 0){
dt.Columns.Add(new DataColumn(rownumberlabel, typeof(String)));
}
}

switch(System.Type.GetTypeCode(row.getFieldValue(i ).GetType())){

case System.TypeCode.Boolean:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(bool )));
break;

case System.TypeCode.DateTime:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Date Time)));
break;

case System.TypeCode.Decimal:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(bool )));
break;

case System.TypeCode.Int16:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int1 6)));
break;

case System.TypeCode.Int32:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int3 2)));
break;

case System.TypeCode.Int64:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Int6 4)));
break;

case System.TypeCode.String:
default:
dt.Columns.Add(new
DataColumn((String)row.getFieldName(i),typeof(Stri ng)));
break;
}
}

// then add the data rows to the data table

for(i=1; i<listdata.getCount(); i++){
row = (RowData)listdata.getRowData(i);
dr = dt.NewRow();
for(j=0; j< row.getFieldCount(); j++){
if(displayrownumber){ // then add an extra row header as S No.
if(j == 0){
dr[j] = i;
}
dr[j+1] = row.getFieldValue(j);
}
else{
dr[j] = row.getFieldValue(j);
}
}
dt.Rows.Add(dr);
}
}

dv = new DataView(dt);
}
catch (Exception ex){
Log.Instance.write(Log.EXCEPTION,this.ToString(), "Exception
occured in method createdatasource with message "+ ex.Message);
throw(ex);
}
}

protected bool IsColumnSortable(String columnname){
bool matchfound = false;
int i;
if(sortablecolumns.Count == 0){
return false;
}
for(i=0; i<sortablecolumns.Count; i++){
if(((String)sortablecolumns[i]).ToLower() == columnname.ToLower()){
matchfound = true;
break;
}
}
return matchfound;

}

protected bool IsColumnHyperlink(String colname){
bool matchfound = false;
int i;
if (hyperlinkcolumns.Count == 0){
return false;
}

for(i=0; i<hyperlinkcolumns.Count; i++){
if(((String)hyperlinkcolumns[i]).ToLower() == colname.ToLower()){
matchfound = true;
associatedURL = (String) navigatetoURL[i];
break;
}
}
return matchfound;
}

protected bool IsColumnInvisible(String colname){
bool matchfound = false;
int i;
if (invisiblecolumns.Count == 0){
return false;
}

for(i=0; i<invisiblecolumns.Count; i++){
if(((String)invisiblecolumns[i]).ToLower() == colname.ToLower()){
matchfound = true;
break;
}
}
return matchfound;
}
protected void BindGrid(bool sortit) {
Log.Instance.write(Log.INFO, this.ToString(), "Bind Grid called");
if (sortit){
SortGrid();
}
localgrid.EnableViewState = true;
localgrid.DataSource = dv;
localgrid.DataBind();

}
protected void GenerateGrid(){
int i;
HyperLinkColumn hlink;
BoundColumn bcolumn;
ButtonColumn button;
TemplateColumn tcolumn;
TextBox textbox;

String columnname="";

if(localgrid.Columns.Count >0){
localgrid.Columns.Clear();
}
if(null != listdata){

RowData row = (RowData)listdata.getRowData(0);
for(i=0; i < row.getFieldCount(); i++){
// add the row number column if its ON
if(displayrownumber){
if(i==0){
bcolumn = new BoundColumn();
bcolumn.DataField = rownumberlabel;
bcolumn.HeaderText = rownumberlabel;
bcolumn.ReadOnly = true;
localgrid.Columns.Add(bcolumn);
}
}

columnname = (String)row.getFieldName(i);
Log.Instance.write(Log.INFO, this.ToString(), "Fieldname is :" +
columnname);

if(editable){ // the grid is editable

tcolumn = new TemplateColumn();
tcolumn.HeaderText = (String)row.getFieldLabel(columnname);
tcolumn.ItemTemplate = new DataGridTemplate(ListItemType.Item,
columnname);
tcolumn.EditItemTemplate = new DataGridTemplate(ListItemType.EditItem,
columnname);
localgrid.Columns.Add(tcolumn);
}
else{ // the grid is read only
if(IsColumnHyperlink(columnname)){
hlink = new HyperLinkColumn();
hlink.DataNavigateUrlField = columnname;
hlink.DataNavigateUrlFormatString=associatedURL;
//"detailspage.aspx?id={0}";
hlink.DataTextField=columnname;
hlink.HeaderText = (String)row.getFieldLabel(columnname);
hlink.Visible = !(IsColumnInvisible(columnname));
if(IsColumnSortable(columnname)){
hlink.SortExpression = columnname;
}
hlink.Target="_new";
localgrid.Columns.Add(hlink);
}
else{
bcolumn = new BoundColumn();
bcolumn.DataField = columnname;
bcolumn.HeaderText = (String)row.getFieldLabel(columnname);
if(IsColumnSortable(columnname)){
bcolumn.SortExpression = columnname;
}
bcolumn.ReadOnly = true;
bcolumn.Visible = !(IsColumnInvisible(columnname));
localgrid.Columns.Add(bcolumn);
}
}
}

// now add the ADD and Remove button(s)
if(showaddbutton){
button = new ButtonColumn();
button.Text = addbuttontext;
//button.HeaderText ="Click to Add";
button.CommandName = "localgrid_ADD";
button.ButtonType = ButtonColumnType.PushButton;
localgrid.Columns.Add(button);
}

if(showremovebutton){
button = new ButtonColumn();
button.Text = removebuttontext;
//button.HeaderText ="Click to Remove";
button.CommandName = "localgrid_REMOVE";
button.ButtonType = ButtonColumnType.PushButton;
localgrid.Columns.Add(button);
}
}
Log.Instance.write(Log.INFO, this.ToString(), "total columns in grid = "
+
localgrid.Columns.Count);

}

protected void SortGrid(){
if(sortfield == selectedsortfield){
ascending = !(ascending);
}
else{
ascending = true;
}

selectedsortfield = sortfield;

if(ascending){
//dv.Sort = sortfield + " ASC";
dv.Sort = sortfield;
Log.Instance.write(Log.INFO, this.ToString(), "ascending is true and
sortfield is " + sortfield + " selectedSortfield is " +
selectedsortfield);
}
else{
//dv.Sort = sortfield + " DESC";
dv.Sort = sortfield;
Log.Instance.write(Log.INFO, this.ToString(), "ascending is false and
sortfield is " + sortfield + " selectedSortfield is " +
selectedsortfield);
}
}

// ------------ EVENTS -------------------------------------------------

void Page_Load(Object sender, EventArgs e) {
Log.Instance.write(Log.INFO, this.ToString(), "EVENT:: Page_load for
datagridcontrol called");

if(!Page.IsPostBack){
localgrid.EnableViewState = true;
Log.Instance.write(Log.INFO, this.ToString(), "Call is not a post
back");
if (sortfield.Length == 0) {
RowData row = (RowData)listdata.getRowData(0);
sortfield = (String)row.getFieldName(0); // default - the first
field in the listdata
selectedsortfield = sortfield;
}
localgrid.ItemStyle.BackColor=System.Drawing.Color .FromName(oddrowcolor);

localgrid.AlternatingItemStyle.BackColor=System.Dr awing.Color.FromName(evenr
owcolor);

Log.Instance.write(Log.INFO, this.ToString(), "Value of Sort field is:"
+
sortfield + " and value of selected sort field is " + selectedsortfield );
localgrid.CssClass= classname;
GenerateGrid();
CreateDataSource();
BindGrid(false);

localgrid.ShowHeader = showheader;
localgrid.ShowFooter = showfooter;
localgrid.ToolTip = tooltip;
}
}

void localgrid_Page(Object sender, DataGridPageChangedEventArgs e) {
try{
Log.Instance.write(Log.INFO, this.ToString(), "localgrid_page event
called");
localgrid.CurrentPageIndex = e.NewPageIndex;
Log.Instance.write(Log.INFO, this.ToString(), "Pageindex on page event
is
: " + localgrid.CurrentPageIndex);
BindGrid(false);
}

catch (Exception ex){
Log.Instance.write(Log.EXCEPTION,this.ToString(), "Exception occured in
event localgrid_page with message "+ ex.Message);
throw(ex);
}
}

void localgrid_Sort(Object sender, DataGridSortCommandEventArgs e) {
Log.Instance.write(Log.INFO, this.ToString(), "localgrid_sort called");
sortfield = (string)e.SortExpression;
Log.Instance.write(Log.INFO, this.ToString(), "Sort field in sort event
is:" + sortfield);
BindGrid(true);
}

void localgrid_Item(Object sender, DataGridCommandEventArgs e) {

if (((LinkButton)e.CommandSource).CommandName == "localgrid_ADD") {
itemadd = true;
itemtoaddorremove = e.Item
}

if (((LinkButton)e.CommandSource).CommandName == "localgrid_REMOVE") {

}
}

// --------- PUBLIC METHODS -------------------------------------------
public void MakeColumnAsHyperlink(String columnname, String URL){
if (columnname.Length > 0){
// add the column name to the arraylist of hyperlink columns
hyperlinkcolumns.Add(columnname);
navigatetoURL.Add(URL);
}
}

public void MakeColumnAsSortable(String columnname){
if (columnname.Length > 0){
sortablecolumns.Add(columnname);
}
}

public void MakeColumnInvisible(String columnname){
if (columnname.Length > 0){
invisiblecolumns.Add(columnname);
}
}

public String getBeginTag(){
String tag = "";

tag ="<td ";
if(rowtag){
tag = "<TR><TD ";
}
if(colspan > 1){
tag = tag + " colspan=\"" + colspan + "\"";
}
tag = tag + " >";

if(headertext.Length >0){
tag = tag + "<TABLE width=\"100%\"><TR><TD>" +
Misc.Instance.getSpaces(spacesfromleft) + headertext + "</TD></TR>";
tag = tag + "<TR><TD>";
}

return tag;
}

public String getEndTag(){
String tag = "</td>";
if(headertext.Length > 0){
tag = tag + "</TR></TABLE></TD>";
}
if(rowtag){
tag = tag + "</TR>";
}

return tag;
}

</script>
<%=getBeginTag()%>
<asp:DataGrid id="localgrid" name="localgrid" runat="server"
OnPageIndexChanged="localgrid_Page"
OnSortCommand="localgrid_Sort"
OnItemCommand="localgrid_Item"
AutoGenerateColumns="false" />
<%=getEndTag()%>

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.