Hi All,
I have one table with Column say D,B,C,A in Database. Now when i am creating a list in WCF service using Sql Connection, it returns a list in alphabetical order of column say A,B,C,D.
My problem is, I want to display column order as it is present in Database i.e D,B,C,A.
How to solve this problem??
Note : 1. DataGrid columns are Auto Generated, which is not going to change to (AutoGenerateColumns="False")
2. Also there are 16 columns in my table, i know using DisplayIndex its possible but dnt want this too.
Thanks in Advance
Loading
Prakash ChotaliyaPosted Mar 17, 2011, 6:44 AM
Thanks for reply here ...
The solution which has given by amit is not worked for me , any ways i found a new solution to handle this problem.
Solution :
when we drag a table from server to dbml file, a class for that table is automatically generated. so we can set the order of columns by using [DataMember(Order=1)] in that dbml class.
for e.g if i have drag a table named Character into dbml file & the class will look like this :
public class Character
{
public int D { get; set; }
public int C { get; set; }
public int B { get; set; }
public int A { get; set; }
}
Now if i want to change the order of columns, i have done some changes with this class :
[DataContract]
public class Student
{
[DataMember(Order=1)]
public int D { get; set; }
[DataMember(Order=2)]
public int B { get; set; }
[DataMember(Order=3)]
public int C { get; set; }
[DataMember(Order=4)]
public int A { get; set; }
}
This will return me D,B,C,A not a sorted columns i.e A,B,C,D.
Suthish NairPosted Mar 16, 2011, 3:33 PM
Amit ChoudharyPosted Mar 16, 2011, 9:08 AM
Well you are not creating a list of your columns in your code but you are creating an Object with properties D,B,C,A and adding it to List. so this object will be treated like a single item and the properties of this object i.e. D,B,C,A will no longer be in order cause they never be in order.
I suggest you to use the the SortedList:
List
while (dr.Read())
{
listObj.Add("0", dr[0].ToString());
listObj.Add("1", dr[1].ToString());
listObj.Add("2", dr[2].ToString());
listObj.Add("3", dr[3].ToString());
}
Return listObj;
you have to change the return type of the method too. if you use D,C,E,F then they'll again get sorted like A,B,C,D. so to maintain the order its better you use 0,1,2,3 etc.
Hope this helps you.
Thanks.
Prakash ChotaliyaPosted Mar 16, 2011, 3:01 AM
I am creating List in this way :
List
while (dr.Read())
{
var trn = new UserClass
{
D= dr[0].ToString(),
B= dr[1].ToString(),
C= dr[2].ToString(),
A=dr[3].ToString()
};
listObj .Add(trn); //Here i create the final list which is in arrange in default way (D,B,C,A)
}
return listObj;
//UserClass is a table in DB, which is drop in dbml file.
After returning this list to completed event in xaml .. there i got a problem as i told you above (list arrange in A,B,C,D format).
Amit ChoudharyPosted Mar 15, 2011, 3:13 AM
Can you post the code where you are creating the list?? It may help us to look behind the scene.
Thanks.