473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DataGrid 1.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 1766
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.goo gle.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_DataGrid 1.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.Ta bles.Clear();
}
catch{}

this.dataTable = null;
this.dataTable = dt.Copy();
this.dataSet.Ta bles.Add(dataTa ble);
this.dataSet.Ac ceptChanges();

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

Hope this will help
Paul

jo*******@gmx.d e (Alexander) wrote in message news:<84******* *************** ****@posting.go ogle.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_DataGrid 1.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
7934
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 header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
0
239
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 = myNewDataTable; this.Refresh(); }
5
12239
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. The ID is a member of the keys array. I then create a DataView dv over the table, and sort it by Display and ID column (in case of duplicate Display). I then set my DataGrid.DataSource = dv; I then load the datatable with my rows, and this is...
3
4886
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 the best method? Do you have a sample of how to do this?
1
4342
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 (watch the layout, some have child controls):
1
3298
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 days of struggling, I figure asking you all (the experts) will keep me from going down some dark and dangerous road. The project I have is a fairly simple one, in theory anyway. The gist is to create a page where the user enters an IDNumber,...
0
1705
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 single worksheet into a dataset. I successfully built a dynamic datagrid. And I did successfully bind it to the dataset. Then I added the datagrid control to a PlaceHolder. The first worksheet displays beautifully. The next step is to allow...
7
2040
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 (http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc), but I get no alignment at all when calling the class. George Shephard's site, while imminently useful, does not have an anwer for this issue.
10
2753
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 values of A are used to populate the grid via a DataView. The instance of form2 is called Matrix_A . The basic layout is as follows: #Region "Globals Dim A As Double(,) Dim Matrix_A As form2 #End Region
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.