DataTable and DataGridView

I

Ilya Dyoshin

There is a problem:

I'm writting a client for use with various ODBC drivers.

There is a function:

System::Data::DataTable ^buildTable(System::String ^sqlQuery)

which builds new instance of DataTable based on the results of sqlQuery.
The table name is "noname".

Then I'm adding this DataTable to DataSet ^ds like

ds->Tables->Add(buildTable("some query"));

Then I'm linking my DataGridView object to this table:

dataGridView1->DataSource = ds;
dataGridView1->DataMember = "noname";

and this works well.

Unlikely, when I'm trying to build another view from another sqlQuery
there is a problem:

//Before adding new samenamed-tables I'm deleting all tables
while (ds->Tables->Count)
ds->Tables->RemoveAt(ds->Tables->Count - 1);

And table is really removed from DataSource (I've watched that). And
then I run again

ds->Tables->Add(buildTable("another query"));
dataGridView1->DataSource = ds;
dataGridView1->DataMember = "noname";

And I see table "noname" actually wasn't deleted... it's still in the
memory! and I can't see another results, but only the first.


Any suggestions?? :-(
 
M

Miha Markic [MVP C#]

Hi Ilya,

Perhaps the newly added table has the same name?
Btw, there is a method DataSet.Reset that will remove the tables for you.
Or, just create another instance of dataset.
 
I

Ilya Dyoshin

Earl said:
Use ds.Tables.Remove("noname")

There's a good example in the Help.

Thanks for your suggestion, but it doesn't help! :-(

I tried as ds.Tables.Remove("noname") as well as
ds.Tables.RemoveAt(ds->Tables->Count -1 ) but none of them bring me
correct result.
 
I

Ilya Dyoshin

Using of
ds.Reset();

or

ds.Tables.Remove("noname");

doesn't works!

Any Suggestions? :-(
 
M

Miha Markic [MVP C#]

Are you sure that new table your are adding has a different name?
Did you check ds.Tables.Count after Reset?
 
J

Jesus Lopez

Is your application an ASP.NET app ? Are you forgetting to call
dataGridView.DataBind ? are you putting the code in "if not ispostpack"
statement?
 
I

Ilya Dyoshin

Miha Markic [MVP C#] ÐÉÛÅÔ:
Are you sure that new table your are adding has a different name?
Did you check ds.Tables.Count after Reset?

I do ds.Reset() - and I'm really switched to my original state (without
any table).
then I'm adding the same-named table from a new instance.

Because adding always new tables will result in huge amount of operating
memory for this application :-(
 
I

Ilya Dyoshin

Hi all!

I've solved my problem, probably a bit awkward.
Maybe somebody can help me with more mutual code.

before add same-named table into DataSet ds.Reset() doesn't work. So I'm
just deleting out of memory my DataSet and then build it again..

delete ds;
ds = gcnew System::Data::DataSet();

and this works well (as I wished :) And memory doesn't grow up.


But nevertheless I think such method is unsafe, and there is more exact
solution for this problem. Maybe someone has solved this problem.

Ilya said:
Miha Markic [MVP C#] ÐÉÛÅÔ:
Are you sure that new table your are adding has a different name?
Did you check ds.Tables.Count after Reset?

I do ds.Reset() - and I'm really switched to my original state (without
any table).
then I'm adding the same-named table from a new instance.

Because adding always new tables will result in huge amount of operating
memory for this application :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top