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

Sorting DataGrid (c#, asp, xml)

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.