473,883 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hello All,

I am working to create a generic datagrid which accepts a
datasource(List Data - 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="goon demand.framewor k" %>
<%@ import Namespace="goon demand.framewor k.types" %>
<%@ import Namespace="goon demand.ui.util" %>
<%@ import Namespace="Syst em.Data" %>
<script runat="server">
// private elements

private String accesskey="";
private String addbuttontext=" +";
private String align = "left";
private bool allowsorting=fa lse;
private bool allowpaging=fal se;
private String classname = "grid-body-default";
private int colspan=0;
private bool displayrownumbe r=false;
private bool editable=false;
private String evenrowcolor = "White";
private String headertext="";
private bool hideborder=true ;
private ListData listdata;
private String nextpagetext="N ext";
private String oddrowcolor = "WhiteSmoke ";
private int pagesize=10;
private String pagestyle="Nume ric";
private String prevpagetext="P rev";
private String removebuttontex t = "-";
private bool rowtag = false;
private String rownumberlabel= "S_No";
private bool showaddbutton=t rue;
private bool showremovebutto n = true;
private bool showheader=true ;
private bool showfooter=fals e;
private int spacesfromleft = GlobalConfig.SP ACESFROMLEFT;
private String tooltip="";
private int width = 0;
private bool visible=true;

// implementation elemnts
private String sortfield="";
private bool ascending=true;
private String selectedsortfie ld;
private ArrayList hyperlinkcolumn s = new ArrayList();
private ArrayList navigatetoURL = new ArrayList();
private ArrayList sortablecolumns = new ArrayList();
private ArrayList invisiblecolumn s = new ArrayList();
private int itemtoaddororem ove;
private bool itemremove=fals e;
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.Acces sKey =accesskey;
}
}

public String ADDBUTTONTEXT{
get{
return addbuttontext;
}

set{
addbuttontext = value;
}
}

public String ALIGN{
get{
return align;
}
set{
align = value;
if(align.ToLowe r() == "left"){
localgrid.Pager Style.Horizonta lAlign=Horizont alAlign.Left;
}
else{
localgrid.Pager Style.Horizonta lAlign=Horizont alAlign.Right;
}
}
}

public bool ALLOWPAGING{
get{
return allowpaging;
}

set{
allowpaging = value;
localgrid.Allow Paging = allowpaging;
if(allowpaging) {
if(pagesize==0) {
pagesize=10;
}
localgrid.PageS ize = pagesize;
if(pagestyle.To Upper() == "NUMERIC"){
localgrid.Pager Style.Mode = PagerMode.Numer icPages;
}
localgrid.Pager Style.NextPageT ext = nextpagetext;
localgrid.Pager Style.PrevPageT ext = prevpagetext;
}
}
}
public bool ALLOWSORTING{
get{
return allowsorting;
}

set{
allowsorting = value;
localgrid.Allow Sorting = allowsorting;
}
}

public String CLASSNAME{
get{
return classname;
}

set{
classname = value;
localgrid.CssCl ass=classname;
}
}

public int COLSPAN{
get{
return colspan;
}

set{
colspan = value;
}
}
public bool DISPLAYROWNUMBE R{
get{
return displayrownumbe r;
}

set{
displayrownumbe r = value;
}
}

public bool EDITABLE{
get{
return editable;
}

set{
editable = value;
}
}

public String EVENROWCOLOR{
get{
return evenrowcolor;
}

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

localgrid.Alter natingItemStyle .BackColor=Syst em.Drawing.Colo r.FromName(even r
owcolor);
}
}
}

public String HEADERTEXT{
get{
return headertext;
}

set{
headertext = value;
}
}

public bool HIDEBORDER{
get{
return hideborder;
}

set{
hideborder = value;
if (hideborder){
localgrid.Borde rStyle = BorderStyle.Sol id;
}
else{
localgrid.Borde rStyle = BorderStyle.Non e;
}
}
}

public ListData LISTDATA{
get{
return listdata;
}

set{
listdata = value;
}
}

public String NEXTTEXT{
get{
return nextpagetext;
}

set{
nextpagetext = value;
if (nextpagetext.L ength > 0){
localgrid.Pager Style.NextPageT ext = nextpagetext;
}
}
}

public String ODDROWCOLOR{
get{
return oddrowcolor;
}

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

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

public int PAGESIZE{
get{
return pagesize;
}

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

public String PAGESTYLE{
get{
return pagestyle;
}

set{
pagestyle = value;
if (pagestyle.ToUp per() == "NUMERIC"){
localgrid.Pager Style.Mode = PagerMode.Numer icPages;
}
else{
localgrid.Pager Style.Mode = PagerMode.NextP rev;
}
}
}

public String PREVTEXT{
get{
return prevpagetext;
}

set{
prevpagetext = value;
if (prevpagetext.L ength > 0){
localgrid.Pager Style.PrevPageT ext = prevpagetext;
}
}
}

public String REMOVEBUTTONTEX T{
get{
return removebuttontex t;
}

set{
removebuttontex t = 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 SHOWREMOVEBUTTO N{
get{
return showremovebutto n;
}

set{
showremovebutto n = value;
}
}

public bool SHOWHEADER{
get{
return showheader;
}

set{
showheader = value;
localgrid.ShowH eader = showheader;
}
}

public bool SHOWFOOTER{
get{
return showfooter;
}

set{
showfooter = value;
localgrid.ShowF ooter = showfooter;
}
}

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

public String TOOLTIP{
get{
return tooltip;
}
set{
tooltip = value;
localgrid.ToolT ip = 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 CreateDataSourc e() {

try{
Log.Instance.wr ite(Log.INFO, this.ToString() , "Createdatasour ce
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)listda ta.getRowData(0 );
for(i=0; i< row.getFieldCou nt(); i++){
if(displayrownu mber){ // then add an extra row header as S No.
if(i == 0){
dt.Columns.Add( new DataColumn(rown umberlabel, typeof(String)) );
}
}

switch(System.T ype.GetTypeCode (row.getFieldVa lue(i).GetType( ))){

case System.TypeCode .Boolean:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (bool)));
break;

case System.TypeCode .DateTime:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (DateTime)));
break;

case System.TypeCode .Decimal:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (bool)));
break;

case System.TypeCode .Int16:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int16)));
break;

case System.TypeCode .Int32:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int32)));
break;

case System.TypeCode .Int64:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int64)));
break;

case System.TypeCode .String:
default:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (String)));
break;
}
}

// then add the data rows to the data table

for(i=1; i<listdata.getC ount(); i++){
row = (RowData)listda ta.getRowData(i );
dr = dt.NewRow();
for(j=0; j< row.getFieldCou nt(); j++){
if(displayrownu mber){ // then add an extra row header as S No.
if(j == 0){
dr[j] = i;
}
dr[j+1] = row.getFieldVal ue(j);
}
else{
dr[j] = row.getFieldVal ue(j);
}
}
dt.Rows.Add(dr) ;
}
}

dv = new DataView(dt);
}
catch (Exception ex){
Log.Instance.wr ite(Log.EXCEPTI ON,this.ToStrin g(), "Exception
occured in method createdatasourc e with message "+ ex.Message);
throw(ex);
}
}

protected bool IsColumnSortabl e(String columnname){
bool matchfound = false;
int i;
if(sortablecolu mns.Count == 0){
return false;
}
for(i=0; i<sortablecolum ns.Count; i++){
if(((String)sor tablecolumns[i]).ToLower() == columnname.ToLo wer()){
matchfound = true;
break;
}
}
return matchfound;

}

protected bool IsColumnHyperli nk(String colname){
bool matchfound = false;
int i;
if (hyperlinkcolum ns.Count == 0){
return false;
}

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

protected bool IsColumnInvisib le(String colname){
bool matchfound = false;
int i;
if (invisiblecolum ns.Count == 0){
return false;
}

for(i=0; i<invisiblecolu mns.Count; i++){
if(((String)inv isiblecolumns[i]).ToLower() == colname.ToLower ()){
matchfound = true;
break;
}
}
return matchfound;
}
protected void BindGrid(bool sortit) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "Bind Grid called");
if (sortit){
SortGrid();
}
localgrid.Enabl eViewState = true;
localgrid.DataS ource = dv;
localgrid.DataB ind();

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

String columnname="";

if(localgrid.Co lumns.Count >0){
localgrid.Colum ns.Clear();
}
if(null != listdata){

RowData row = (RowData)listda ta.getRowData(0 );
for(i=0; i < row.getFieldCou nt(); i++){
// add the row number column if its ON
if(displayrownu mber){
if(i==0){
bcolumn = new BoundColumn();
bcolumn.DataFie ld = rownumberlabel;
bcolumn.HeaderT ext = rownumberlabel;
bcolumn.ReadOnl y = true;
localgrid.Colum ns.Add(bcolumn) ;
}
}

columnname = (String)row.get FieldName(i);
Log.Instance.wr ite(Log.INFO, this.ToString() , "Fieldname is :" +
columnname);

if(editable){ // the grid is editable

tcolumn = new TemplateColumn( );
tcolumn.HeaderT ext = (String)row.get FieldLabel(colu mnname);
tcolumn.ItemTem plate = new DataGridTemplat e(ListItemType. Item,
columnname);
tcolumn.EditIte mTemplate = new DataGridTemplat e(ListItemType. EditItem,
columnname);
localgrid.Colum ns.Add(tcolumn) ;
}
else{ // the grid is read only
if(IsColumnHype rlink(columnnam e)){
hlink = new HyperLinkColumn ();
hlink.DataNavig ateUrlField = columnname;
hlink.DataNavig ateUrlFormatStr ing=associatedU RL;
//"detailspage.as px?id={0}";
hlink.DataTextF ield=columnname ;
hlink.HeaderTex t = (String)row.get FieldLabel(colu mnname);
hlink.Visible = !(IsColumnInvis ible(columnname ));
if(IsColumnSort able(columnname )){
hlink.SortExpre ssion = columnname;
}
hlink.Target="_ new";
localgrid.Colum ns.Add(hlink);
}
else{
bcolumn = new BoundColumn();
bcolumn.DataFie ld = columnname;
bcolumn.HeaderT ext = (String)row.get FieldLabel(colu mnname);
if(IsColumnSort able(columnname )){
bcolumn.SortExp ression = columnname;
}
bcolumn.ReadOnl y = true;
bcolumn.Visible = !(IsColumnInvis ible(columnname ));
localgrid.Colum ns.Add(bcolumn) ;
}
}
}

// now add the ADD and Remove button(s)
if(showaddbutto n){
button = new ButtonColumn();
button.Text = addbuttontext;
//button.HeaderTe xt ="Click to Add";
button.CommandN ame = "localgrid_ADD" ;
button.ButtonTy pe = ButtonColumnTyp e.PushButton;
localgrid.Colum ns.Add(button);
}

if(showremovebu tton){
button = new ButtonColumn();
button.Text = removebuttontex t;
//button.HeaderTe xt ="Click to Remove";
button.CommandN ame = "localgrid_REMO VE";
button.ButtonTy pe = ButtonColumnTyp e.PushButton;
localgrid.Colum ns.Add(button);
}
}
Log.Instance.wr ite(Log.INFO, this.ToString() , "total columns in grid = " +
localgrid.Colum ns.Count);

}

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

selectedsortfie ld = sortfield;

if(ascending){
//dv.Sort = sortfield + " ASC";
dv.Sort = sortfield;
Log.Instance.wr ite(Log.INFO, this.ToString() , "ascending is true and
sortfield is " + sortfield + " selectedSortfie ld is " + selectedsortfie ld);
}
else{
//dv.Sort = sortfield + " DESC";
dv.Sort = sortfield;
Log.Instance.wr ite(Log.INFO, this.ToString() , "ascending is false and
sortfield is " + sortfield + " selectedSortfie ld is " + selectedsortfie ld);
}
}

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

void Page_Load(Objec t sender, EventArgs e) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "EVENT:: Page_load for
datagridcontrol called");

if(!Page.IsPost Back){
localgrid.Enabl eViewState = true;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Call is not a post back");
if (sortfield.Leng th == 0) {
RowData row = (RowData)listda ta.getRowData(0 );
sortfield = (String)row.get FieldName(0); // default - the first
field in the listdata
selectedsortfie ld = sortfield;
}

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

localgrid.Alter natingItemStyle .BackColor=Syst em.Drawing.Colo r.FromName(even r
owcolor);

Log.Instance.wr ite(Log.INFO, this.ToString() , "Value of Sort field is:" +
sortfield + " and value of selected sort field is " + selectedsortfie ld );
localgrid.CssCl ass= classname;
GenerateGrid();
CreateDataSourc e();
BindGrid(false) ;

localgrid.ShowH eader = showheader;
localgrid.ShowF ooter = showfooter;
localgrid.ToolT ip = tooltip;
}
}

void localgrid_Page( Object sender, DataGridPageCha ngedEventArgs e) {
try{
Log.Instance.wr ite(Log.INFO, this.ToString() , "localgrid_ page event
called");
localgrid.Curre ntPageIndex = e.NewPageIndex;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Pageindex on page event is
: " + localgrid.Curre ntPageIndex);
BindGrid(false) ;
}

catch (Exception ex){
Log.Instance.wr ite(Log.EXCEPTI ON,this.ToStrin g(), "Exception occured in
event localgrid_page with message "+ ex.Message);
throw(ex);
}
}

void localgrid_Sort( Object sender, DataGridSortCom mandEventArgs e) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "localgrid_ sort called");
sortfield = (string)e.SortE xpression;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Sort field in sort event
is:" + sortfield);
BindGrid(true);
}

void localgrid_Item( Object sender, DataGridCommand EventArgs e) {

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

if (((LinkButton)e .CommandSource) .CommandName == "localgrid_REMO VE") {

}
}

// --------- PUBLIC METHODS -------------------------------------------
public void MakeColumnAsHyp erlink(String columnname, String URL){
if (columnname.Len gth > 0){
// add the column name to the arraylist of hyperlink columns
hyperlinkcolumn s.Add(columnnam e);
navigatetoURL.A dd(URL);
}
}

public void MakeColumnAsSor table(String columnname){
if (columnname.Len gth > 0){
sortablecolumns .Add(columnname );
}
}

public void MakeColumnInvis ible(String columnname){
if (columnname.Len gth > 0){
invisiblecolumn s.Add(columnnam e);
}
}

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

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

if(headertext.L ength >0){
tag = tag + "<TABLE width=\"100%\"> <TR><TD>" +
Misc.Instance.g etSpaces(spaces fromleft) + headertext + "</TD></TR>";
tag = tag + "<TR><TD>";
}

return tag;
}

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

return tag;
}

</script>
<%=getBeginTag( )%>
<asp:DataGrid id="localgrid" name="localgrid " runat="server"
OnPageIndexChan ged="localgrid_ Page"
OnSortCommand=" localgrid_Sort"
OnItemCommand=" localgrid_Item"
AutoGenerateCol umns="false" />
<%=getEndTag()% >
Nov 19 '05 #1
1 2700
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*********@ho tmail.com> wrote in message
news:uQ******** ******@TK2MSFTN GP14.phx.gbl...
Hello All,

I am working to create a generic datagrid which accepts a
datasource(List Data - 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="goon demand.framewor k" %>
<%@ import Namespace="goon demand.framewor k.types" %>
<%@ import Namespace="goon demand.ui.util" %>
<%@ import Namespace="Syst em.Data" %>
<script runat="server">
// private elements

private String accesskey="";
private String addbuttontext=" +";
private String align = "left";
private bool allowsorting=fa lse;
private bool allowpaging=fal se;
private String classname = "grid-body-default";
private int colspan=0;
private bool displayrownumbe r=false;
private bool editable=false;
private String evenrowcolor = "White";
private String headertext="";
private bool hideborder=true ;
private ListData listdata;
private String nextpagetext="N ext";
private String oddrowcolor = "WhiteSmoke ";
private int pagesize=10;
private String pagestyle="Nume ric";
private String prevpagetext="P rev";
private String removebuttontex t = "-";
private bool rowtag = false;
private String rownumberlabel= "S_No";
private bool showaddbutton=t rue;
private bool showremovebutto n = true;
private bool showheader=true ;
private bool showfooter=fals e;
private int spacesfromleft = GlobalConfig.SP ACESFROMLEFT;
private String tooltip="";
private int width = 0;
private bool visible=true;

// implementation elemnts
private String sortfield="";
private bool ascending=true;
private String selectedsortfie ld;
private ArrayList hyperlinkcolumn s = new ArrayList();
private ArrayList navigatetoURL = new ArrayList();
private ArrayList sortablecolumns = new ArrayList();
private ArrayList invisiblecolumn s = new ArrayList();
private int itemtoaddororem ove;
private bool itemremove=fals e;
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.Acces sKey =accesskey;
}
}

public String ADDBUTTONTEXT{
get{
return addbuttontext;
}

set{
addbuttontext = value;
}
}

public String ALIGN{
get{
return align;
}
set{
align = value;
if(align.ToLowe r() == "left"){
localgrid.Pager Style.Horizonta lAlign=Horizont alAlign.Left;
}
else{
localgrid.Pager Style.Horizonta lAlign=Horizont alAlign.Right;
}
}
}

public bool ALLOWPAGING{
get{
return allowpaging;
}

set{
allowpaging = value;
localgrid.Allow Paging = allowpaging;
if(allowpaging) {
if(pagesize==0) {
pagesize=10;
}
localgrid.PageS ize = pagesize;
if(pagestyle.To Upper() == "NUMERIC"){
localgrid.Pager Style.Mode = PagerMode.Numer icPages;
}
localgrid.Pager Style.NextPageT ext = nextpagetext;
localgrid.Pager Style.PrevPageT ext = prevpagetext;
}
}
}
public bool ALLOWSORTING{
get{
return allowsorting;
}

set{
allowsorting = value;
localgrid.Allow Sorting = allowsorting;
}
}

public String CLASSNAME{
get{
return classname;
}

set{
classname = value;
localgrid.CssCl ass=classname;
}
}

public int COLSPAN{
get{
return colspan;
}

set{
colspan = value;
}
}
public bool DISPLAYROWNUMBE R{
get{
return displayrownumbe r;
}

set{
displayrownumbe r = value;
}
}

public bool EDITABLE{
get{
return editable;
}

set{
editable = value;
}
}

public String EVENROWCOLOR{
get{
return evenrowcolor;
}

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

localgrid.Alter natingItemStyle .BackColor=Syst em.Drawing.Colo r.FromName(even r
owcolor);
}
}
}

public String HEADERTEXT{
get{
return headertext;
}

set{
headertext = value;
}
}

public bool HIDEBORDER{
get{
return hideborder;
}

set{
hideborder = value;
if (hideborder){
localgrid.Borde rStyle = BorderStyle.Sol id;
}
else{
localgrid.Borde rStyle = BorderStyle.Non e;
}
}
}

public ListData LISTDATA{
get{
return listdata;
}

set{
listdata = value;
}
}

public String NEXTTEXT{
get{
return nextpagetext;
}

set{
nextpagetext = value;
if (nextpagetext.L ength > 0){
localgrid.Pager Style.NextPageT ext = nextpagetext;
}
}
}

public String ODDROWCOLOR{
get{
return oddrowcolor;
}

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

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

public int PAGESIZE{
get{
return pagesize;
}

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

public String PAGESTYLE{
get{
return pagestyle;
}

set{
pagestyle = value;
if (pagestyle.ToUp per() == "NUMERIC"){
localgrid.Pager Style.Mode = PagerMode.Numer icPages;
}
else{
localgrid.Pager Style.Mode = PagerMode.NextP rev;
}
}
}

public String PREVTEXT{
get{
return prevpagetext;
}

set{
prevpagetext = value;
if (prevpagetext.L ength > 0){
localgrid.Pager Style.PrevPageT ext = prevpagetext;
}
}
}

public String REMOVEBUTTONTEX T{
get{
return removebuttontex t;
}

set{
removebuttontex t = 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 SHOWREMOVEBUTTO N{
get{
return showremovebutto n;
}

set{
showremovebutto n = value;
}
}

public bool SHOWHEADER{
get{
return showheader;
}

set{
showheader = value;
localgrid.ShowH eader = showheader;
}
}

public bool SHOWFOOTER{
get{
return showfooter;
}

set{
showfooter = value;
localgrid.ShowF ooter = showfooter;
}
}

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

public String TOOLTIP{
get{
return tooltip;
}
set{
tooltip = value;
localgrid.ToolT ip = 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 CreateDataSourc e() {

try{
Log.Instance.wr ite(Log.INFO, this.ToString() , "Createdatasour ce
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)listda ta.getRowData(0 );
for(i=0; i< row.getFieldCou nt(); i++){
if(displayrownu mber){ // then add an extra row header as S No.
if(i == 0){
dt.Columns.Add( new DataColumn(rown umberlabel, typeof(String)) );
}
}

switch(System.T ype.GetTypeCode (row.getFieldVa lue(i).GetType( ))){

case System.TypeCode .Boolean:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (bool)));
break;

case System.TypeCode .DateTime:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (DateTime)));
break;

case System.TypeCode .Decimal:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (bool)));
break;

case System.TypeCode .Int16:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int16)));
break;

case System.TypeCode .Int32:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int32)));
break;

case System.TypeCode .Int64:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (Int64)));
break;

case System.TypeCode .String:
default:
dt.Columns.Add( new
DataColumn((Str ing)row.getFiel dName(i),typeof (String)));
break;
}
}

// then add the data rows to the data table

for(i=1; i<listdata.getC ount(); i++){
row = (RowData)listda ta.getRowData(i );
dr = dt.NewRow();
for(j=0; j< row.getFieldCou nt(); j++){
if(displayrownu mber){ // then add an extra row header as S No.
if(j == 0){
dr[j] = i;
}
dr[j+1] = row.getFieldVal ue(j);
}
else{
dr[j] = row.getFieldVal ue(j);
}
}
dt.Rows.Add(dr) ;
}
}

dv = new DataView(dt);
}
catch (Exception ex){
Log.Instance.wr ite(Log.EXCEPTI ON,this.ToStrin g(), "Exception
occured in method createdatasourc e with message "+ ex.Message);
throw(ex);
}
}

protected bool IsColumnSortabl e(String columnname){
bool matchfound = false;
int i;
if(sortablecolu mns.Count == 0){
return false;
}
for(i=0; i<sortablecolum ns.Count; i++){
if(((String)sor tablecolumns[i]).ToLower() == columnname.ToLo wer()){
matchfound = true;
break;
}
}
return matchfound;

}

protected bool IsColumnHyperli nk(String colname){
bool matchfound = false;
int i;
if (hyperlinkcolum ns.Count == 0){
return false;
}

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

protected bool IsColumnInvisib le(String colname){
bool matchfound = false;
int i;
if (invisiblecolum ns.Count == 0){
return false;
}

for(i=0; i<invisiblecolu mns.Count; i++){
if(((String)inv isiblecolumns[i]).ToLower() == colname.ToLower ()){
matchfound = true;
break;
}
}
return matchfound;
}
protected void BindGrid(bool sortit) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "Bind Grid called");
if (sortit){
SortGrid();
}
localgrid.Enabl eViewState = true;
localgrid.DataS ource = dv;
localgrid.DataB ind();

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

String columnname="";

if(localgrid.Co lumns.Count >0){
localgrid.Colum ns.Clear();
}
if(null != listdata){

RowData row = (RowData)listda ta.getRowData(0 );
for(i=0; i < row.getFieldCou nt(); i++){
// add the row number column if its ON
if(displayrownu mber){
if(i==0){
bcolumn = new BoundColumn();
bcolumn.DataFie ld = rownumberlabel;
bcolumn.HeaderT ext = rownumberlabel;
bcolumn.ReadOnl y = true;
localgrid.Colum ns.Add(bcolumn) ;
}
}

columnname = (String)row.get FieldName(i);
Log.Instance.wr ite(Log.INFO, this.ToString() , "Fieldname is :" +
columnname);

if(editable){ // the grid is editable

tcolumn = new TemplateColumn( );
tcolumn.HeaderT ext = (String)row.get FieldLabel(colu mnname);
tcolumn.ItemTem plate = new DataGridTemplat e(ListItemType. Item,
columnname);
tcolumn.EditIte mTemplate = new DataGridTemplat e(ListItemType. EditItem,
columnname);
localgrid.Colum ns.Add(tcolumn) ;
}
else{ // the grid is read only
if(IsColumnHype rlink(columnnam e)){
hlink = new HyperLinkColumn ();
hlink.DataNavig ateUrlField = columnname;
hlink.DataNavig ateUrlFormatStr ing=associatedU RL;
//"detailspage.as px?id={0}";
hlink.DataTextF ield=columnname ;
hlink.HeaderTex t = (String)row.get FieldLabel(colu mnname);
hlink.Visible = !(IsColumnInvis ible(columnname ));
if(IsColumnSort able(columnname )){
hlink.SortExpre ssion = columnname;
}
hlink.Target="_ new";
localgrid.Colum ns.Add(hlink);
}
else{
bcolumn = new BoundColumn();
bcolumn.DataFie ld = columnname;
bcolumn.HeaderT ext = (String)row.get FieldLabel(colu mnname);
if(IsColumnSort able(columnname )){
bcolumn.SortExp ression = columnname;
}
bcolumn.ReadOnl y = true;
bcolumn.Visible = !(IsColumnInvis ible(columnname ));
localgrid.Colum ns.Add(bcolumn) ;
}
}
}

// now add the ADD and Remove button(s)
if(showaddbutto n){
button = new ButtonColumn();
button.Text = addbuttontext;
//button.HeaderTe xt ="Click to Add";
button.CommandN ame = "localgrid_ADD" ;
button.ButtonTy pe = ButtonColumnTyp e.PushButton;
localgrid.Colum ns.Add(button);
}

if(showremovebu tton){
button = new ButtonColumn();
button.Text = removebuttontex t;
//button.HeaderTe xt ="Click to Remove";
button.CommandN ame = "localgrid_REMO VE";
button.ButtonTy pe = ButtonColumnTyp e.PushButton;
localgrid.Colum ns.Add(button);
}
}
Log.Instance.wr ite(Log.INFO, this.ToString() , "total columns in grid = "
+
localgrid.Colum ns.Count);

}

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

selectedsortfie ld = sortfield;

if(ascending){
//dv.Sort = sortfield + " ASC";
dv.Sort = sortfield;
Log.Instance.wr ite(Log.INFO, this.ToString() , "ascending is true and
sortfield is " + sortfield + " selectedSortfie ld is " +
selectedsortfie ld);
}
else{
//dv.Sort = sortfield + " DESC";
dv.Sort = sortfield;
Log.Instance.wr ite(Log.INFO, this.ToString() , "ascending is false and
sortfield is " + sortfield + " selectedSortfie ld is " +
selectedsortfie ld);
}
}

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

void Page_Load(Objec t sender, EventArgs e) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "EVENT:: Page_load for
datagridcontrol called");

if(!Page.IsPost Back){
localgrid.Enabl eViewState = true;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Call is not a post
back");
if (sortfield.Leng th == 0) {
RowData row = (RowData)listda ta.getRowData(0 );
sortfield = (String)row.get FieldName(0); // default - the first
field in the listdata
selectedsortfie ld = sortfield;
}
localgrid.ItemS tyle.BackColor= System.Drawing. Color.FromName( oddrowcolor);

localgrid.Alter natingItemStyle .BackColor=Syst em.Drawing.Colo r.FromName(even r
owcolor);

Log.Instance.wr ite(Log.INFO, this.ToString() , "Value of Sort field is:"
+
sortfield + " and value of selected sort field is " + selectedsortfie ld );
localgrid.CssCl ass= classname;
GenerateGrid();
CreateDataSourc e();
BindGrid(false) ;

localgrid.ShowH eader = showheader;
localgrid.ShowF ooter = showfooter;
localgrid.ToolT ip = tooltip;
}
}

void localgrid_Page( Object sender, DataGridPageCha ngedEventArgs e) {
try{
Log.Instance.wr ite(Log.INFO, this.ToString() , "localgrid_ page event
called");
localgrid.Curre ntPageIndex = e.NewPageIndex;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Pageindex on page event
is
: " + localgrid.Curre ntPageIndex);
BindGrid(false) ;
}

catch (Exception ex){
Log.Instance.wr ite(Log.EXCEPTI ON,this.ToStrin g(), "Exception occured in
event localgrid_page with message "+ ex.Message);
throw(ex);
}
}

void localgrid_Sort( Object sender, DataGridSortCom mandEventArgs e) {
Log.Instance.wr ite(Log.INFO, this.ToString() , "localgrid_ sort called");
sortfield = (string)e.SortE xpression;
Log.Instance.wr ite(Log.INFO, this.ToString() , "Sort field in sort event
is:" + sortfield);
BindGrid(true);
}

void localgrid_Item( Object sender, DataGridCommand EventArgs e) {

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

if (((LinkButton)e .CommandSource) .CommandName == "localgrid_REMO VE") {

}
}

// --------- PUBLIC METHODS -------------------------------------------
public void MakeColumnAsHyp erlink(String columnname, String URL){
if (columnname.Len gth > 0){
// add the column name to the arraylist of hyperlink columns
hyperlinkcolumn s.Add(columnnam e);
navigatetoURL.A dd(URL);
}
}

public void MakeColumnAsSor table(String columnname){
if (columnname.Len gth > 0){
sortablecolumns .Add(columnname );
}
}

public void MakeColumnInvis ible(String columnname){
if (columnname.Len gth > 0){
invisiblecolumn s.Add(columnnam e);
}
}

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

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

if(headertext.L ength >0){
tag = tag + "<TABLE width=\"100%\"> <TR><TD>" +
Misc.Instance.g etSpaces(spaces fromleft) + headertext + "</TD></TR>";
tag = tag + "<TR><TD>";
}

return tag;
}

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

return tag;
}

</script>
<%=getBeginTag( )%>
<asp:DataGrid id="localgrid" name="localgrid " runat="server"
OnPageIndexChan ged="localgrid_ Page"
OnSortCommand=" localgrid_Sort"
OnItemCommand=" localgrid_Item"
AutoGenerateCol umns="false" />
<%=getEndTag()% >

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8915
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 problem is that when you click the added column header it doesn't trigger the sort. The code : <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
3
4933
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 are Web Control & not HTML control. One of these buttons whose title is Delete is added on the aspx page in design view & also I double clicked on this button in design view to get the onclick code for this button in the code behind page. & for...
6
4485
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 clicked. I have read several posts which say to put your column generating section in the Page_Init section and it will solve the problem....however, it hasn't solved mine. Can somebody please take a look at this and provide any insight if possible?
4
1524
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 HeaderText="Check In"> <ItemStyle HorizontalAlign="Center" Width="30px"></ItemStyle> <ItemTemplate> <asp:CheckBox id="chkReturn" runat="server" OnCheckedChanged="CheckItIn"></asp:CheckBox>
1
4355
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 (watch the layout, some have child controls):
2
3379
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 and .CS file. When I initialize my .CS file, in that code there is a method that goes: Page.LoadControl(FILENAME) Which associates a .ascx file with my .CS file, allowing me to plug in any
1
4240
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
15
3143
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 OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
9
2735
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 code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10752
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.