472,374 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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

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
1 2587
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
4
by: Jim Heavey | last post by:
Hello, I am starting to learn how to use the Datagrid and I have a couple of questions. My datagrid as a checkbox in it. It looks like the following in the datagrid... <asp:TemplateColumn...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
2
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.