C# Combo Box Data Sort - c#

I'm using a DataTable as the datasource for my ComboBox and I need to sort the Data into a strict order. The problem is that the I don't need in Ascending Order or Descending, it's how the customer see's the items as a priority, so there's not really any logic too it.
Will I need to create a column that has an ordering value and then sort the data by that column? I'm just looking for the best way to do this. In time, more items may be added and the ordering may need to change. This is something i want to achieve without hard coding each time.
The Data at present only has two columns, which look like below:-
ApplicationName | ApplicationType
I just need the best way of doing this.
Thanks

Related

Change order of items when on index MVC5 asp.net C#

So I have a question about MVC5.
I want to change the order of items listed on the view.
When a new record is created with the create view it automatically puts the new one under the old one when showed on the index view. I want it the other way around. So that the new record which is showing in the view is on top. I have no clue on how to achieve this. Hope someone can help me.
You should have a order column in your table. and order Items by that, this can be an integer, but however I Prefer a datetime type.
If you want to change the order of items just change their order field value.
Imagine that you are ordering descending, then setting current datetime to an item will move it to the end.
It is difficult to give a good answer to this question because of the lack of details in the question. Assuming however that you are creating the data in a database and returning a list from there you can add this to the Linq expression providing you with the list
OrderByDescending(x => x.ID)
where ID is the ID of the entity being created.

Optimal keep data inside program

I have a table which it has 2000 row. This tables "name" columns are static. But Others is dinamic. The table follow as :
Can i keep this table inside program? Which format must use? (Class,list, datatable, struct Json or etc.).
I'll search and update on table in future...
Thanks.
You can easily put your row data into a MyRow class with Id, Position, Weight and FullEmp properties and then put those row objects into a list of rows like List<MyRow>.
Performance considerations largely depend on what you want to do with your data. How often do you read it? How often do you change it? How often do you insert, append or remove rows? Which properties do you access during your search?
A list is a good starting point that you can use until you find out that the performance is unsatisfactory for your usecase.

C# Linq - Elegant way to order a dataset by the maximum value between two columns?

To elaborate on what I'm asking here, I'm working in C# trying to order a list of data that is coming out of a database. A certain object (table) has two separate properties (columns) which can hold dates. I want to sort this list based on date, meaning for each data row, I want to take the maximum of the two dates and use that for the sort.
Is there an elegant way to do this? At the moment I'm thinking that I should just get the max time from each record, and store it as a key in a Dictionary, with the data row being the value, and then iterate through the Dictionary with the sorted keys. Although this wouldn't be terrible to code, I'm trying to see if there is a nicer way to do this using Linq. Any help would be appreciated. Thanks.
If you want an alternative for sorting your collection everytime, .NET contains a class called SortedList. Here is the link to the MSDN article: http://msdn.microsoft.com/en-us/library/system.collections.sortedlist(v=vs.110).aspx
MSDN states that a SortedList:
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
So if you want to sort by date you can declare your sorted list as:
SortedList<DateTime, YourData> mySortedList = new SortedList<DateTime, YourData>();
and when you add values to it, it will already be sorted. Or you can just go with LINQ and #Alexander answer.
Edit
I just understood what you want to do. You can do:
table.OrderBy(item => Math.Max(item.Date1.Ticks, item.Date2.Ticks));
Note: The linq query above will not be performant on a large collection.

DataView Sort - how to when column names are unknown

I need advice on how to go about a problem I have with sorting a DataView in C#.
I need to add a sort property to my DataView because I later need it when I call a Find on the DataView (which needs to have a sort property specified in order to work). I need the Find in order to properly map a DataRow to a DataGridViewRow. I currently just use the row index to map to the DataGridViewRow, but this won't work in all cases if the user physically moves rows, or changes the sort order in the DataGridView (UI) because then the ids no longer match-up/map.
When I create the DataView, I load in a DataTable to pass in for creation. This DataTable can be of any form, I do not know what the DataTable contains for columns and rows. Therefore, I cannot create a sort if I don't know any column names to sort by.
I was thinking of just taking the first column name (column[0]) - no matter what it is and using that to sort. But this seems kinda sketchy, no?
My question is, is there a better way to determine what to sort my DataView by when column names are unknown?
Thanks!
I haven't tried it, but since it sorts based on the column name you pass in, couldn't you sort based on index with something like:
dataview.Sort = dataview.Table.Columns[index].ColumnName + " DESC";

C# SQL: What is the best way to implement a dynamic table?

I want to allow the user to add columns to a table in the UI.
The UI: Columns Name:______ Columns Type: Number/String/Date
My Question is how to build the SQL tables and C# objects so the implementation will be efficient and scalable.
My thought is to build two SQL tables:
TBL 1 - ColumnsDefinition:
ColId, ColName, ColType[Text]
TBL 2 - ColumnsValues:
RowId, ColId, Value [Text]
I want the solution to be efficient in DB space,
and I want to allow the user to sort the dynamic columns.
I work on .NET 3.5 / SQL Server 2008.
Thanks.
I believe that is essentially how the WebParts.SqlPersonalizationProvider works, which doesn't necessarily mean it's the best, but does mean that after some smart people thought about it for a while, that's what they came up with.
Sorting on a given field will be a bit tricky, particularly if the field text need a non-text sorting (i.e., if you want "2" to come before "10").
I'd suggest that from C#, you do one query on ColumnsDefinition, and based on that, choose one of several different queries for selecting/sort the data.
Add a DefaultValue to your ColumnDefinition. Only add a value in ColumnsValues if the value is not the default value. This will speed up things a lot.
The thing I hate about these kind of systems is that it is very difficult to transfer changes betwween dev/stage/production because you will have to keep structure and content of tables in sync.

Categories

Resources