473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Sorting DataGrid (c#, asp, xml)

Hello

I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control
I have problem when sorting data in datagrid

I have created SortCommand event in my .aspx file for my datagrid DataGrid2 but I am not able to access my datarecord nor dataset from my SortCommand event. I recieve also an error when running with created event if don't change event method declaration to public void from private void

Compiler Error Message: CS0122: 'myspace.home.com.tpmsHtml.DataGrid2_ItemDataBound (object, System.Web.UI.WebControls.DataGridItemEventArgs)' is inaccessible due to its protection leve

I have also tried to create DataView but no luck

What do I have to do to be able to create working event in mycode? (appreciate if you could look at my code and see what's wrong

Thank you in advance

-xro

Here is aspx.cs code

using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
using System.Net
using System.Xml
using System.IO
using System.Security
using System.Security.Permissions

namespace myspace.home.co

/// <summary
/// Summary description for myspace
/// </summary
///

public class productsHtml : System.Web.UI.Pag
protected System.Web.UI.WebControls.DropDownList List1
protected System.Web.UI.WebControls.DataGrid DataGrid1
protected System.Web.UI.WebControls.DataGrid DataGrid2
protected System.Web.UI.WebControls.DataGrid DataGrid3

protected void Page_Load(object sender, System.EventArgs e

// Put user code to initialize the page her

//populate the products listbo
XmlDocument xmldoc = new XmlDocument()
xmldoc.Load( Server.MapPath("products.xml") )
System.Xml.XmlNodeList productnodes = xmldoc.SelectNodes("products/product")

if (!IsPostBack

foreach (System.Xml.XmlNode productnod in productnodes
List1.Items.Add(productnod.Attributes["name"].Value);

myLists.Lists list = new myLists.Lists();

Uri url = new Uri(new Uri("http://myserver/"), "_vti_bin/Lists.asmx");
list.Url = url.ToString()

list.Credentials = CredentialCache.DefaultCredentials

//check which product to get documents fo
string productname = List1.SelectedValue

XmlNode productnode = xmldoc.SelectSingleNode("products/product[@name='"+ productname +"']")

int iItems = productnode.FirstChild.ChildNodes.Count
System.Xml.XmlNode documentsnode = productnode.FirstChild

System.Xml.XmlNodeList documentsnodes = documentsnode.ChildNodes

DataSet ds = new DataSet()

StringReader stream
XmlTextReader reader = null

//for each docid addit the the camlsq
foreach (System.Xml.XmlNode docnode in documentsnodes
{
string sCaml = "<Eq><FieldRef Name='documentid'/><Value Type='Text'>"+ docnode.InnerText +"</Value></Eq>";

XmlDocument xmlDoc = new System.Xml.XmlDocument()
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "")

ndQuery.InnerXml = "<Where>"+ sCaml +"</Where>";
System.Xml.XmlNode currentresultnode = list.GetListItems("Document Library", null, ndQuery, null, null, null)

stream = new StringReader(currentresultnode.OuterXml)
reader = new XmlTextReader(stream)

ds.ReadXml(reader)

// encode urls so filenames in non-standard-ascii named documents are resolved correctl
foreach (System.Data.DataRow dr in ds.Tables[1].Rows)
//foreach (System.Data.DataRow dr in ds.Tables[1].Columns[i].Caption
for (int i = 0; i <= 14;i++
dr[1] = System.Web.HttpUtility.UrlPathEncode(dr[1].ToString())

// create new datavie
DataView dv = new DataView(ds.Tables[1])
DataTable dt = dv.Table

ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'daily list'"
DataGrid1.DataSource = ds.Tables[1].DefaultView
DataGrid1.DataBind()
ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'yearly list'";
DataGrid2.DataSource = ds.Tables[1].DefaultView;
DataGrid2.DataBind();

ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'custom list'";
DataGrid3.DataSource = ds.Tables[1].DefaultView;
DataGrid3.DataBind();

}
}

// register event method for datagrid styles
public void dg_menu_ItemDataBound(object sender, DataGridItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{

// rewrite!
// get public css stylesheet class-file
e.Item.Attributes.Add("onmouseover", "this.style.cursor='arrow'; this.style.backgroundColor='#efefef';");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='';");
}

}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 16 '05 #1
1 9480
Hi,

You dont have access to the datasource from the sort command, I would keep
the datasource in Session so that it would not be necesary to recreate it on
each postback, also keep a reference to it in a variable accesible to the
class ( and its derived classes ) , if you do this you will have access from
aany method:

BTW ; I did not see the sort code in your code.

- put the declaration of ds outside the method so it will be accesible to
all the class
protected DataSet ds;
- change the Page_load like this:

Page_Load( .. )
{
if ( !IsPostBack)
{
// do ALL you stuff and
Session["DataSource"] = ds;
}
else
{
//get the dataset back
//you should check if the ds exist first !!!! (not shown )
ds = ( DataSet) Session["DataSource"];
}

}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"xrow" <xr**@msn.com> wrote in message
news:D9**********************************@microsof t.com...
Hello!

I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control. I have problem when sorting data in datagrid.

I have created SortCommand event in my .aspx file for my datagrid DataGrid2 but I am not able to access my datarecord nor dataset from my
SortCommand event. I recieve also an error when running with created event
if don't change event method declaration to public void from private void:
Compiler Error Message: CS0122: 'myspace.home.com.tpmsHtml.DataGrid2_ItemDataBound (object,
System.Web.UI.WebControls.DataGridItemEventArgs)' is inaccessible due to its
protection level
I have also tried to create DataView but no luck.

What do I have to do to be able to create working event in mycode? (appreciate if you could look at my code and see what's wrong)
Thank you in advance!

-xrow

Here is aspx.cs code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Xml;
using System.IO;
using System.Security;
using System.Security.Permissions;
namespace myspace.home.com
{
/// <summary>
/// Summary description for myspace.
/// </summary>
///

public class productsHtml : System.Web.UI.Page
{

protected System.Web.UI.WebControls.DropDownList List1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid3;

protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//populate the products listbox
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load( Server.MapPath("products.xml") );
System.Xml.XmlNodeList productnodes = xmldoc.SelectNodes("products/product");
if (!IsPostBack)
{
foreach (System.Xml.XmlNode productnod in productnodes)
{

List1.Items.Add(productnod.Attributes["name"].Value);

}
}
myLists.Lists list = new myLists.Lists();

Uri url = new Uri(new Uri("http://myserver/"), "_vti_bin/Lists.asmx");
list.Url = url.ToString();

list.Credentials = CredentialCache.DefaultCredentials;
//check which product to get documents for
string productname = List1.SelectedValue;

XmlNode productnode = xmldoc.SelectSingleNode("products/product[@name='"+ productname +"']");
int iItems = productnode.FirstChild.ChildNodes.Count;
System.Xml.XmlNode documentsnode = productnode.FirstChild;

System.Xml.XmlNodeList documentsnodes = documentsnode.ChildNodes;

DataSet ds = new DataSet();

StringReader stream;
XmlTextReader reader = null;

//for each docid addit the the camlsql
foreach (System.Xml.XmlNode docnode in documentsnodes)
{
string sCaml = "<Eq><FieldRef Name='documentid'/><Value Type='Text'>"+ docnode.InnerText +"</Value></Eq>";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

ndQuery.InnerXml = "<Where>"+ sCaml +"</Where>";
System.Xml.XmlNode currentresultnode = list.GetListItems("Document Library", null, ndQuery, null, null, null);
stream = new StringReader(currentresultnode.OuterXml);
reader = new XmlTextReader(stream);

ds.ReadXml(reader);

}

// encode urls so filenames in non-standard-ascii named documents are resolved correctly foreach (System.Data.DataRow dr in ds.Tables[1].Rows)
{

//foreach (System.Data.DataRow dr in ds.Tables[1].Columns[i].Caption)
for (int i = 0; i <= 14;i++)
dr[1] = System.Web.HttpUtility.UrlPathEncode(dr[1].ToString());

// create new dataview
DataView dv = new DataView(ds.Tables[1]);
DataTable dt = dv.Table;

ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'daily list'";
DataGrid1.DataSource = ds.Tables[1].DefaultView;
DataGrid1.DataBind();
ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'yearly list'";
DataGrid2.DataSource = ds.Tables[1].DefaultView;
DataGrid2.DataBind();

ds.Tables[1].DefaultView.RowFilter = "my_listtype = 'custom list'";
DataGrid3.DataSource = ds.Tables[1].DefaultView;
DataGrid3.DataBind();

}
}

// register event method for datagrid styles
public void dg_menu_ItemDataBound(object sender, DataGridItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) {

// rewrite!
// get public css stylesheet class-file
e.Item.Attributes.Add("onmouseover", "this.style.cursor='arrow'; this.style.backgroundColor='#efefef';"); e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='';");
}

}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 16 '05 #2

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

Similar topics

3
by: Philipp Lenssen | last post by:
I have this XML file which I would like to XSLT-sort based on the string contained within the item-children elements using basic MSXML (no recent version of IIS, so it might be an outdated MSXML --...
2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. It is easy to reproduce the problem I have. If...
1
by: Gacu | last post by:
Hi, How can I make my Datagrid object sort rows properly by "date" column which is in format DD-MM-YYYY ? Thanx gacu@poczta.onet.pl
1
by: cashdeskmac | last post by:
When using a DataGrid I can easily sort the items by clicking on the column header, but is there a way to sort them automatically? If I have 20 records, each numbered 1 to 20, and delete number 13...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
2
by: Diego F. | last post by:
Hi. I need to sort datagrid fields. What are the steps I had to follow? If I sort a DataGrid, does the bound DataTable alter its fields also or it's independent? -- Regards, Diego F.
4
by: Pat | last post by:
I'm trying to do a sorting in a Datagrid below but i'm getting ERRROR:- "Line 1: Incorrect syntax near 'CompanyID'." strQuery = "Select InvoiceDate,OrderDate From CMRC_Orders ORDER BY " & _...
4
by: Joris De Groote | last post by:
Hi, I have created a webpage with a datagrid. The page fills in the datagrid after the user selected some things (it's a searchpage). Now the page show everything from an SQL server sorted on...
0
by: zamuel | last post by:
Hey everyone. As I'm new developing web applications and I have little bit problems with creating a sorting function to datagrid. I have datagrid which is populated and sorted from SQL server with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.