473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting dataset Columns

I have a dataset which am trying to sort by 3 specific columns. I am not
making any connection to the database to get the dataset. I have the raw
dataset already.
What i tried doing was to create a DataView and sort it by a column name.

mydataset.ReadX ml(@sResponseXM L);
DataView mydataview = new DataView();
mydataview = ds.Tables[0].DefaultView;
mydataview.Sort = "ColumnName ";

Then I call a function Export() passing the dataset. The function loops thru
the dataset and writes the output as a csv file. So technically I cannot say
mydataview.Tabl es[0].Columns to loop thru the data view nor can I use the
foreach statement on a dataview.

I even tried sorting the dataset itself and passing it to the Export function
ds.Tables[0].DefaultView.So rt = "Country of Risk"; This doesnt work either.

Can someone give any ideas about how to handle this? I'd really appreciate.

Thanks
Deepa
Nov 22 '05 #1
4 3911
Deepa,

I once made a snippet, maybe you can use it.

\\\
DataView dv = new DataView(dt);
dv.Sort = "bla"; // sorts column bla
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow (dvr.Row);
dt.Clear();
dt = dtnew.Copy();
///

I hope this helps?

Cor
Nov 22 '05 #2
Cor
in the statement DataView dv = new DataView(dt);
is dt another data table object?
In my scenario am loading the dataset object using the ReadXml. Later am
looping thru the dataset.

-Deepa

"Cor Ligthert" wrote:
Deepa,

I once made a snippet, maybe you can use it.

\\\
DataView dv = new DataView(dt);
dv.Sort = "bla"; // sorts column bla
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow (dvr.Row);
dt.Clear();
dt = dtnew.Copy();
///

I hope this helps?

Cor

Nov 22 '05 #3
hey Cor
thanks a lot dude, this is exactly what i was looking for and it works :)

"Cor Ligthert" wrote:
Deepa,

I once made a snippet, maybe you can use it.

\\\
DataView dv = new DataView(dt);
dv.Sort = "bla"; // sorts column bla
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow (dvr.Row);
dt.Clear();
dt = dtnew.Copy();
///

I hope this helps?

Cor

Nov 22 '05 #4
Deepa,

dt is the datatable you want to sort.
Something as ds.Tables[0]

As sample with a datagrid.
\\\
private void button1_Click(o bject sender, System.EventArg s e)
{
DataTable dt = (DataTable)(dat aGrid1.DataSour ce);
dataGrid1.DataS ource = null;
dataGrid1.DataS ource = sortmytable(dt) ;
}
private DataTable sortmytable(Dat aTable dt )
{
DataView dv = new DataView(dt);
dv.Sort = "bla";
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow (dvr.Row);
return(dtnew.Co py());
}
//

I hope this helps

Cor
Nov 22 '05 #5

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

Similar topics

1
1772
by: Dave S | last post by:
Hi, I'm using a datagrid to display contents of a dataset and I've set the datagrid to allow paging and sorting. Rather than re-retrieve from the DB, I store the original dataset in a Session variable and then rebind to it for each page (which works). I'd also like to be able to sort any of the columns of the datagrid, so I do this using a dataview as shown below triggered from the normal Grid_SortCommand event :
4
441
by: Deepa | last post by:
I have a dataset which am trying to sort by 3 specific columns. I am not making any connection to the database to get the dataset. I have the raw dataset already. What i tried doing was to create a DataView and sort it by a column name. mydataset.ReadXml(@sResponseXML); DataView mydataview = new DataView(); mydataview = ds.Tables.DefaultView; mydataview.Sort = "ColumnName";
2
1509
by: Mike P | last post by:
I have just written my first page with a datagrid that allows ASC and DESC sorting. But I want to know how I can get it so that the header shows the type of sorting that has just been done on it (i.e. Column1 DESC). Also I would need to be able to remove this from the last column that was sorted on so that it now says just Column2. <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import...
2
1604
by: ddaniel | last post by:
I have read many posts and seen many papers on the different techniques for sort and filtering datagrids. Many do re-queries against the dB ala Fritz Onion. I am trying to leverage the Dataview. The following control simply responds to a sort request and/or a pageing reqeust with an empty table (header only). Any ideas ? Code behind : namespace OakTree.data {
4
1700
by: Richard | last post by:
When i try sorting in the database, it sorts the numbers: 0 1 102 2 304 305 4 etc....
1
7005
by: tfsmag | last post by:
Hello, I have a function that returns a dynamically created gridview. This works fine, however it does not seem to be able to maintain state when adding sorting or paging to the gridview. Does anyone have any idea how to get this to work? below is the code. Please bear in mind that the function is actually located in a seperate class file from the page that actually returns the grid. ---code for function that returns the grid, this is...
1
2744
by: puja | last post by:
hi all, am using grid view in asp.net 2.0 . My problem is easy but can't get it to work. My grid view id = grdContract and am binding grid view using dataset My dataset returns 5 columns from sql database and I have auto generate columns = true for grdContract grdContract.DataSource = ........dataset returned by a function
5
5969
by: Cindy Lee | last post by:
I'm getting my data from an XML file. The data binds fine, but I can't sort on it. Do I have to do anything special? I have enabled sorting on my grid view. Autogenerate columns is off, and I use bound fields. The sort expression is just the same datafield that binds the column, but when I try to sort with the post back, nothing works.
2
5578
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string"); con.Open(); SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter da = new SqlDataAdapter(cmd);
0
2077
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7316
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...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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

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.