C# .NET - DataView Position of Row - Asked By shekhar kumar on 18-Nov-09 07:28 AM

I am using dataview for binding data to the griview in asp.net. I am using sorting using this view.
It is working fine.
But Now After sorting the position of a row inside a view does not changes?
Is it true? 
Because on the position of the row I need for further code.

What I am doing is Binding a data to the gridview using dataview.
And after sorting the same row should be shown selected.
For this I am looping the the dataview but not finding the same position which was before sorting.


try this - Jack jack replied to shekhar kumar on 18-Nov-09 07:38 AM


Dim dvEmpolyees as New DataView(dtEmployees)


DataView dvEmployees = new DataView(dtEmployees);

Next, Sort it by calling the DataView's Sort Property:


dvEmployees.Sort = "DateOfHire"


dvEmployees.Sort = "DateOfHire";

Compare using 2 lines of code with the effort it would take to requery the database and then refill a table?  Few things are so cleear cut.

Now, what if you wanted to sort it by multiple fields, say DateOfHire and Department


dvEmployees.Sort = "DateOfHire, Department"


dvEmployees.Sort = "DataOfHire, Department";

Just as simple, right?

Now, lets say that you wanted DateOfHire sorted by normally, but you wanted department sorted in Descending Order.  Piece of cake.


dvEmployees.Sort = "DateOfHire, Department DESC"


dvEmployees.Sort = "DateOfHire, Department DESC";

There's one other thing you might want to be aware of.  Every now and then, you might be confronted with a case where you have Spaces in your field names or Reserved words.  Typically, the 'correct' approach is to go back and change them in the database ASAP.  There is so much downside to having fields named like this, but I know, there are a lot of people who are in love with their naming conventions even if there's a ridiculously high cost associated with such terrible habits.  Anyway, if you can't change the names of the column, you can use the "[]" to get it to work.


dvEmployees.Sort = "[Date Of Hire], Deparment"


dvEmployees.Sort = "[Date Of Hire], Department"

>>>after this bind dataview to grid.

thanks for the reply - shekhar kumar replied to Jack jack on 18-Nov-09 07:53 AM

But Do the position of the record in dataview changes when I sort the view.
Mens if I have sorted the view eg. dvEmployees.Sort = "DateOfHire"
will the row position of the view which was containing Department ='Hr' will change or it will be the same.


re - Ravenet Rasaiyah replied to shekhar kumar on 18-Nov-09 11:55 AM


When you are do the sort, the position of the value may or not change,so you need find that vlaue  and set the selected row again by the JavaScript.

when you are bind write code dataRowBound event and set the selected row to that value row

Just see my sample code here

protected void Unnamed1_RowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType==DataControlRowType.DataRow)
string value = DataBinder.Eval(e.Row.DataItem, "ColmName").ToString();
if (value.Equals("Your earlier value"))
Unnamed1.SelectedIndex = e.Row.RowIndex;

Thank you