last day (14 days later) » 

4:12 AM
1
A: Reordering all rows of a datatable based on one of the fields

Handoko.ChenYou can use LINQ to sort your DataTable. The result of the LINQ operation (dt2) is IEnumerable, but you can still it to bind your grid. var dt = new DataTable(); dt.Columns.Add("Key", typeof(string)); dt.Columns.Add("Position", typeof(int)); dt.Rows.Add("Key1Value", 2); dt.Rows.Add("Key2Value",...

 
This seems like a great solution, but I don't know if I fully understand it. Will it give me full flexibility to place any of the 20 rows wherever I want? I don't really have a field that I can sort by. I need to say if Key="Field2" insert at index 0, if Key="Field4" insert at index 1, and so on..
I implemented your first solution since it made sense to me now, but will I be able to additionally change the name of the key before returning the order? For example all of my keys are database field names and I would like to add spaces in between the words. For example I need to change the actual value of Key from Key5Value to "Key 5 Value", It seems like I will only be able to do something like this using the second technique based off of mine that you provided
 
Please see version 2 of my code.
 
I tried implementing version 2 and it says 'The column key does not belong to table'. On this part: x["Position"] = dict[x.Field<string>("Key")].Item2; By the way my actual Key column is called CategoryName. I replaced everywhere you had Key with that and still getting the error.
ahh I figured out my problem. My source datatable has rows in it that I do not wish to add to the grid so I wasn't adding them to the dictionary. Therefore, I was getting an error saying key not valid in dictionary. I guess I can't really use this method unless I am using all of the rows in the datatable. Is there a way I can do a check to see if x.Field exists before using it. Basically, lets say Key6Value is in your datatable, but you don't want it in the end result. How would you avoid this.
 
I only use 2 values in the dictionary because I only want to replace 2 columns in the table (Key and Position). Column Val1, Val2 and Val3 are not included in the dictionary because I don't replace the values.
If you encounter an error message "The given key was not present in the dictionary", it means the key you are seeking is not found in the dictionary. See my remark when I replace the Position and Key in the ForEach() method. If I replace the Key column first, the same error will occur because the Key value in the DataTable is changed to "Key 1 Value", and it can't find that key in the dictionary.
 
Thank you for all of the work you have done to show me how to do this. Sorry to bother you in here.
What I was trying to say is that my source dataTable has rows in it that will not be in the final dataSource that gets bound to the grid.
In your example you added 6 rows to your source datatable. How would you handle if you didn't want the row with key: KeyValue6 to be in your final dataSource
 
4:14 AM
What kind of application? Web or winform?
 
is it possible using this technique? because as of now I feel like for every single row in the datatable there has to be a key/value pair added to the dictionary.
Web\
 
Tell me your column names first.
 
Here is a simplified version:
PeriodLabel | Val1 | Val2 | Val3
the example you used to create your source dataTable was very similar.
But you added an additional column Position which is completely fine. I did the same thing.
 
Now I need some values in that table. Is it similar to what I code?
 
Yes. The values are like the following:
Category1 | 23 | 45 | 67
Category2 | 45 | 67 | 92
Category3 | 87 | 34 | 56
there are about 40 rows....
Now lets say I only want Category1, Category2, and Category3 in the end result, but as Category 1, Category 2, and Category 3
how do I exclude all of the other rows without adding them to the dictionary.
 
4:19 AM
DataTable has 40 rows? And you only want 3 of them?
 
that is just an example to keep it simple but yes. It has about 40 rows and I want to keep about 20 of them
 
How do you decide which row you are going to use?
 
I know based on the Category
Im just hardcoding that part.
 
By using switch-case?
 
4:21 AM
I see
 
I just need to figure out how to implement it with what you gave me
 
In that case, you can clone your data table first.
Then modify the body inside my ForEach() method
 
ok, after I clone I will have the same structure
but with zero rows
how do I get only the rows I want into the cloned version
 
You should add the row you want into that cloned data table
 
This seems similar to the code that I already have. I just didn't like doing it this way. I liked your way
please look at this:
dtInvertedProfDetail.Rows[21]["PeriodLabel"] = "Market Share";
dtInvertedProfDetailOrdered.ImportRow(dtInvertedProfDetail.Rows[21]);

dtInvertedProfDetail.Rows[33]["PeriodLabel"] = "National Share";
dtInvertedProfDetailOrdered.ImportRow(dtInvertedProfDetail.Rows[33]);

dtInvertedProfDetail.Rows[28]["PeriodLabel"] = "Therapeutic Growth";
dtInvertedProfDetailOrdered.ImportRow(dtInvertedProfDetail.Rows[28]);

dtInvertedProfDetail.Rows[34]["PeriodLabel"] = "Price Per Unit";
dtInvertedProfDetailOrdered.ImportRow(dtInvertedProfDetail.Rows[34]);
 
4:23 AM
I forgot to tell you, instead of iterating your table, you can iterate the dictionary.
 
oh really!!!
 
the dictionary only holds key rows that you want
 
that is correct because I only added those key/value pairs to it
so how would I modify it to iterate the dictionary rather than the source DT
 
So if you have 40 rows in your datatable, but only 20 in the dictionary, you iterate the 20 rows in the dictionary. and insert the data into the cloned one.
wait a moment
have you installed LINQPad?
it
it's a great tool for working with LINQ
 
No I don't have LinqPad on my computer right now. I should probably get it again. I used to use it
 
4:26 AM
are you familiar with LINQ?
 
Yes, I use it all of the time, but I never use it to the level of the code that your giving me.
 
ok, wait a moment while i change the code
 
I use it like this:
var x = from i in myList
where i.Name = "Name"
select x
select i*
 
that's LINQ expression
I prefer LINQ methods
 
I also have resharper. Yeah I have to touch up on linq methods and lambdas
that is my weakness, I just use the expressions to filter down my original data
I hate dealing with inverse tables. In the database the rows are the columns and the columns are the rows.
The table I am currently dealing with is inverse, but that part you didn't really need to know to help me.
inverted*
So yeah I am currently taking a table that has 13 rows and about 40 columns, flipping it so I have 13 columns (one of them being my X axis because it is unique, the key in our case) and then out of the 40 columns I am only displaying about 20 of them. So this comes along with dynamically generating the columns in my RadGrid since their could be a random amount. Sorry just bored up late at night working overtime to get this done. I really appreciate you helping me with this.
So everything starts with this line:
DataTable dtInversedProfDetail = Utils.GetInversedDataTable(table: dtProformaDetail, columnX: "PeriodLabel");

Then before binding to my grid, I need to not only remove rows that I do not need, but also order them in a completely custom order based on what the client wants.
 
4:42 AM
Ok, here it is. pastebin.com/fJfHc24Z
If you have LINQPad installed, just uncomment that Dump() line. It will show you the content of the datatable
I changed my mind. It easier to iterate the table because there's 1 statement in your original code that kinda hard without accessing the row in the original table
 
ok checking it now
 
It sounds like a bad table design if you have to invert it.
 
worked perfectly.
1 question though.
How come we do not need to add and use the Position column anymore
 
Oops, I forgot that you need to reorder it.
 
it is strange thought because everything looked as if it was in the order that I added it to the dictionary.
I put them in order from index 0 to 21
I guess what your talking about is if I didn't add them all to the dictionary in the exact order they needed to be correct?
 
4:52 AM
Since I removed the Position column, I also didn't set the Position value in each row.
Wait, I'll copy it to a new pastebin.
 
Hmm I also just noticed that my keys are not using the new versions.
the ones with spaces
ok let me know when its done and I'll try again
 
Oops, I forgot to change the Key into the new value too. How silly of me :)
just add this 1 line before inserting the dRow
dRow["Key"] = dict[key].Item1;
The content of ForEach() method is like this:
string key = x.Field<string>("Key").ToString();
if (dict.ContainsKey(key))
{
var dRow = dt2.NewRow();
dRow.ItemArray = x.ItemArray;
dRow["Key"] = dict[key].Item1;
dRow["Position"] = dict[key].Item2;
dt2.Rows.InsertAt(dRow, dict[key].Item2);
}
I removed Key2Value, Key4Value, Key6Value in the dictionary. Then reorder them so that Key5Value is at the first row.
 
haha ok so wait which line do I have to add
 
Just 1 line, see my previous message
dRow["Key"] = dict[key].Item1;
 
oh I see ok let me do it and we should be good.
There we go!! thank so so much for working through this with me.
Now I can use this dictionary to set which rows I want appearing in my final dataSource and order them however I want.
 
4:58 AM
:)
 
Now it isn't a big deal that I am binding the grid with an IEnumerable rather than a dataTable right?
 
No problem with it. All LINQ operation results in IEnumerable
 
All of the same values will be there I am assuming. I am guessing it works just like binding a grid to a list of IEnumerable<Object>
 
Actually it's IEnumerable<DataRow>
 
haha got ya
DataRow[] I think
ItemArray or whatever it is
 
5:00 AM
dt.AsEnumerable() convert datatable into IEnumerable<DataRow>
 
got ya
Well this motivates me to get better at linq
Are you a web developer?
 
LINQ is cool, I like it too
no, I'm a desktop developer. Although I know some basic ASP.Net programming.
 
I am a .NET developer, going on my 3rd year.
 
Cool :)
 
that's cool, never did much desktop development.
Anyway don't want to take up anymore of your time. Thanks again for going above and beyond to help me out with this.
It is a really interesting way of doing it and handles all of my situations
 
5:03 AM
OK, good luck with your project :)
 
Thanks!
have a good night
 
See you later. It's noon here. Lunch time :)
 
wow.. where are you located I am in the US
 
Indonesia
 
as you can tell by my programming skills :-)
 
5:04 AM
It's 12 pm now
Ok, I guess I must go now. I need to have my lunch. See you later :)
 
Thats cool, It is 1AM here and I had to work overtime all weekend on this project so I am beat.
Seeya!
 

last day (14 days later) »