Sort structure on date

R

RP

I have three grids on my Form namely, grid1, grid2 and grid3. grid3 is
readonly and is only to display result. Each grid has identical
columns i.e., Date and Amount. grid1 and grid2 can have n number of
rows depending upon user-entry. I need to combine entries in grid1 and
grid2 in a place where they can be sorted on date. since both grid1
and grid2 have identical columns.

At present I used DataSet and DataTable to collect the rows and sorted
with DataView. The DataTable contains three columns, Date, Amount and
GridName. Assume following data in the DataTable:

Date Amount GridType
---------- --------------- ------------------
2/apr/07 100 grid1
4/apr/07 200 grid2
5/apr/07 150 grid1

These dates and amounts are collectively used in a calculation
forumula to fill grid3, which too has identical columns. But grid3
will display dates in sorted (ascending) order.

Everything worked with DataTable, but I want to use Structure and
Indexer and try this out. I wrote a small code, but it is giving error
in the indexer:

========[CODE SAMPLE]=======================
//Structure
struct Capital
{
DateTime GDate;
double GAmount;
string GridType;
}

//Indexer
public int this[DateTime cellDate]
{
get
{
return (capital[cellDate]);
}
set
{
Capital[cellDate] = value;
}
}
============================================

Is it possible to sort a structure on date field. i.e, GDate in the
structure?
 
J

Jon Skeet [C# MVP]

Is it possible to sort a structure on date field. i.e, GDate in the
structure?

You don't generally sort a single object - you sort a *collection*.
Also, sorting is not the same as using an indexer.

Could you give more information about what you're really trying to do?

(I'd also suggest using a class rather than a struct, by the way.)

Jon
 
R

RP

Could you give more information about what you're really trying to do?
(I'd also suggest using a class rather than a struct, by the way.)

Jon

The Structure finally has records of both the grids. I want these
records to be sorted on GDate (in structure) bbut the other fields,
i.e., Amount and GridName must also remain available. I want a sorted
table like view of the structure.
 
J

Jon Skeet [C# MVP]

RP said:
The Structure finally has records of both the grids. I want these
records to be sorted on GDate (in structure) bbut the other fields,
i.e., Amount and GridName must also remain available. I want a sorted
table like view of the structure.

The structure you specified before had a single DateTime, a single
double and a single string - no collections at all.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top