473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataGrid exchanging a DataSource, but Columns are added...

Hi, I am using a DataGrid and assign a DataTable as a DataSource to
it:

void ShowData()
{
DataTable myNewDataTable = new DataTable();
... // fill table
this.m_DataGrid1.DataSource = myNewDataTable;
this.Refresh();
}

I create the DataTable from the scratch of some XML data and then
simply assign it. This works perfectly, the table with all the columns
is displayed.

If I call the function a second time, and assign a new DataTable as a
DataSource, the new table is added to the grid instead of replacing
the old one. So if the old table had columns 1, 2 and 3 and the new
table 98, 99, the result is a grid with the columns 1, 2, 3, 98, 99.

I already debugged, to make sure if the new DataTable really has only
2 columns and that afterwards the new DataSource of the DataGrid has
only 2 columns, too. But displayed are 5 columns afterwards...

Is there a special trick to replace the DataSource?
Nov 16 '05 #1
3 1740
Hi,

If it's a WinForms DataGrid, try using the SetDataBinding method instead.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Alexander" <jo*******@gmx.de> wrote in message
news:84**************************@posting.google.c om...
Hi, I am using a DataGrid and assign a DataTable as a DataSource to
it:

void ShowData()
{
DataTable myNewDataTable = new DataTable();
... // fill table
this.m_DataGrid1.DataSource = myNewDataTable;
this.Refresh();
}

I create the DataTable from the scratch of some XML data and then
simply assign it. This works perfectly, the table with all the columns
is displayed.

If I call the function a second time, and assign a new DataTable as a
DataSource, the new table is added to the grid instead of replacing
the old one. So if the old table had columns 1, 2 and 3 and the new
table 98, 99, the result is a grid with the columns 1, 2, 3, 98, 99.

I already debugged, to make sure if the new DataTable really has only
2 columns and that afterwards the new DataSource of the DataGrid has
only 2 columns, too. But displayed are 5 columns afterwards...

Is there a special trick to replace the DataSource?


Nov 16 '05 #2
If I understand correctly, you are going through the same problem I had recently.
here is some code.
all you need to do is to create a new table.

private void LoadData(System.Data.DataTable dt)
{
try
{
this.dataSet.Tables.Clear();
}
catch{}

this.dataTable = null;
this.dataTable = dt.Copy();
this.dataSet.Tables.Add(dataTable);
this.dataSet.AcceptChanges();

string member = dataTable.TableName;
this.dataGrid1.SetDataBinding(dataSet,member);
this.dataGrid1.CaptionText = member;
}

Hope this will help
Paul

jo*******@gmx.de (Alexander) wrote in message news:<84**************************@posting.google. com>...
Hi, I am using a DataGrid and assign a DataTable as a DataSource to
it:

void ShowData()
{
DataTable myNewDataTable = new DataTable();
... // fill table
this.m_DataGrid1.DataSource = myNewDataTable;
this.Refresh();
}

I create the DataTable from the scratch of some XML data and then
simply assign it. This works perfectly, the table with all the columns
is displayed.

If I call the function a second time, and assign a new DataTable as a
DataSource, the new table is added to the grid instead of replacing
the old one. So if the old table had columns 1, 2 and 3 and the new
table 98, 99, the result is a grid with the columns 1, 2, 3, 98, 99.

I already debugged, to make sure if the new DataTable really has only
2 columns and that afterwards the new DataSource of the DataGrid has
only 2 columns, too. But displayed are 5 columns afterwards...

Is there a special trick to replace the DataSource?

Nov 16 '05 #3
Thanks Paul, that brought me near to the solution.

I was not aware of the internal structure of the DataSource and what
happens if I assign something to it. So I created tables without names
and thought I would replace the old table by assigning it to the
datasource, but instead I added a nameless table to it. The solution
is simply give the table I create a unique name, so it really replaces
the old one with the same name.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
0
by: Alexander | last post by:
Hi, I am using a DataGrid and assign a DataTable as a DataSource to it: void ShowData() { DataTable myNewDataTable = new DataTable(); ... // fill table this.m_DataGrid1.DataSource =...
5
by: John Richardson | last post by:
I've been bothered for some time about my DataGrid not populating my rows very quickly. I have about 10K rows loading into the grid. I create a datatable dt with 2 columns, an ID and a display. ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
10
by: mark | last post by:
I have a simple windows application that has a function to read a csv file and enter the values into an array A as double(,). Also, an instance of form 2 (which has a DataGrid) is created and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.