1

I've got a DataTable which is declared at the beginning of my class like so:

private DataTable table = new TestData().FillTable();

Throughout the class, I'm able to access the table's contents.

However, I've got a GridView sorting event which changes the contents of the DataTable. Within the event itself, the contents is changed and I've confirmed this through debugging. However, if I call the table from anywhere outside of this method, the contents of the DataTable remains as it was before, i.e. the new values seem to be erased once the soerting event's scope is over.

Here is the code of my sorting event:

protected void TableGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        table.DefaultView.Sort = "GroupNumber, " + e.SortExpression + GetSortDirectio(e.SortExpression);
        TableGridView.DataSource = table;
        TableGridView.DataBind();
    }

When I test the table's contents within the above event, the new values seem to be added. However, if I call the table from any other method, the old values are returned.

What can I do to make the table's contents be updated globally, i.e. to the variable declared at the beginning of my class, and not have different states depending on methods?

Dot NET
  • 4,891
  • 13
  • 55
  • 98
  • is there any specific reason your using this.table? if its a class variable, just use it with out this. But again, this is not the answer i guess. Just try. Damn i wish i had a IDE here to work :( – Zenwalker Aug 17 '11 at 08:36
  • I've tried without 'this' too, still doesnt work :/ – Dot NET Aug 17 '11 at 08:37
  • Its strange, even MSDN says a new DT is created and returned. – Zenwalker Aug 17 '11 at 08:39
  • I've debugged it countless times - each time, the table's differences are present in the sorting event, but the minute the scope is over, the changes arent saved. It's driving me crazy - somehow I think I'm doing something wrong – Dot NET Aug 17 '11 at 08:41
  • Well all the articles says ToTable createsa new table and persists the info on DT than just Table method. Hey btw, your created View from table. So view is kinda linked to table and your updating to table back. I see some fishy here. – Zenwalker Aug 17 '11 at 08:45
  • Can you show us more of you code. Maybe you missing something somewhere else. – Jethro Aug 17 '11 at 08:52
  • Tried updating the code, but I've still got the same problem. – Dot NET Aug 17 '11 at 08:52
  • May be issue related with this: [ASP.NET private member field loses value on postback](http://stackoverflow.com/questions/1313697/asp-net-private-member-field-loses-value-on-postback) – KorVet Aug 17 '11 at 09:22

1 Answers1

1

I've tested this with GridView Sorting event. Leaving sorting event this.table contains sorted values so it works as exptected. Are you sure you are testing your solution in single page request. After PostBack this.table is assigned from TestData().FillTable() again.

muzieh
  • 98
  • 9
  • This is the problem, you're right. Just tested it and sure enough after the event, the table is assigned from TestData().FillTable() again. Do you have any idea as to how I can prevent it from doing that? – Dot NET Aug 17 '11 at 09:38
  • I am not sure why you need sorted DataTable class field so my solution may be inappropriate for you. First you have to assign DataKeyNames property in GridView. Then you can use YourGridViewObject.DataKeys collection to get order of the currently bound rows. In Page_Load load your DataTable and then use DataKeyName to sort DataTable. Other solution would be serializing DataTable to ViewState (impact on page size) or Session (impact on server memory and performance). You can also use database. You can find more information on [link](http://www.asp.net/web-forms) – muzieh Aug 17 '11 at 10:29
  • Thanks for the great information :) I'll be trying to use the DataKeyname method, however is this a problem when taking into account that once the sorting is done, the GridView is databound once again. I.e. does the gridview lose the DataKeyname values which were added before? – Dot NET Aug 17 '11 at 10:38