473,789 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView Behaviour

12 New Member
Helo,

I got this problem with a c# DataGridView in VS2005:

The DataGridView has 2 columns: colArtigoGeneri co, colArtigoFinal; and this columns are populated with data from 2 columns from a DataTable: "Detalhes".

1st i populate the DataGrid with a Datatable using this code:

string select = "SELECT a.CArtigoGeneri co, a.CArtigoFinal " +
"FROM ArtigosGenerico s a ORDER BY a.CArtigoGeneri co ";
SqlDataAdapter adaptador = new SqlDataAdapter( select, ligacao);
adaptador.Fill( Detalhe, "Detalhes") ;

// Populate the Grid:
DGV.AutoGenerat eColumns = false;
DGV.DataSource = Detalhe.Tables["Detalhes"];

DGV.Columns["colArtigoGener ico"].DataPropertyNa me = "CArtigoGeneric o";
DGV.Columns["colArtigoFinal "].DataPropertyNa me = "CArtigoFin al";

ColArtigoGeneri co.ValueMember = DGV.Columns[0].DataPropertyNa me;
ColArtigoFinal. ValueMember = DGV.Columns[1].DataPropertyNa me;

The above code is in a button_click event. And at the 1st time i run it, the grid is populated correctly. Then i set the DataSource to null and the DataGridView, as expected, collapses and shows no record.

Then i click again in the 1st button, and the DataGridView, this time, shows no record.

In a debug session i see that the DataTable has some records, and after the code line:
DGV.DataSource = Detalhe.Tables["Detalhes"];
The DGV.RowCount property returns 0, that's why it shows no record in the Grid.

But the DataTable that is the DataSource for the Grid has 3 records.
Why this behaviour in the 2nd and following times?

TIA,
Joaquim
Nov 14 '07 #1
6 1566
Plater
7,872 Recognized Expert Expert
What is all this buisness?
Expand|Select|Wrap|Line Numbers
  1. DGV.Columns["colArtigoGenerico"].DataPropertyName = "CArtigoGenerico";
  2. DGV.Columns["colArtigoFinal"].DataPropertyName = "CArtigoFinal";
  3.  
  4. ColArtigoGenerico.ValueMember = DGV.Columns[0].DataPropertyName;
  5. ColArtigoFinal.ValueMember = DGV.Columns[1].DataPropertyName;
  6.  
If you set auto-generate columns to on, then it will doo of this for you?

I am thinking your troubles are that you are not removing the columns you made when you set the datasource to null?
Nov 14 '07 #2
JoaquimC
12 New Member
Thank You Plater:

I didn't mention it, for readability reasons, but the two columns are comboboxes, so i need to set the datasource for each one of the comboboxes and the auto-generate behaviour does not do what i want, because it generates TextBox columns.

About removing columns: is that necessary? Maybe that's the reason for the behaviour, but thinking again: no, because in the 2nd time i run the code i see that the DataGridView has no records and the 1st time it had 3.

Why?

Joaquim


What is all this buisness?
Expand|Select|Wrap|Line Numbers
  1. DGV.Columns["colArtigoGenerico"].DataPropertyName = "CArtigoGenerico";
  2. DGV.Columns["colArtigoFinal"].DataPropertyName = "CArtigoFinal";
  3.  
  4. ColArtigoGenerico.ValueMember = DGV.Columns[0].DataPropertyName;
  5. ColArtigoFinal.ValueMember = DGV.Columns[1].DataPropertyName;
  6.  
If you set auto-generate columns to on, then it will doo of this for you?

I am thinking your troubles are that you are not removing the columns you made when you set the datasource to null?
Nov 14 '07 #3
Plater
7,872 Recognized Expert Expert
But are you are adding the columns each time? They get all kinds of confused when you have multiple columns with the same name
Nov 14 '07 #4
JoaquimC
12 New Member
No i'm not adding the columns each time, they're added automaticaly by VS2005 in the Class.Designer. cs file, and i do not touch them anymore.

And there is no columns with the same name, where did you get that idea?

Joaquim
Nov 14 '07 #5
Plater
7,872 Recognized Expert Expert
If you added those columns more then once (some people do that) it confuses it
Nov 14 '07 #6
JoaquimC
12 New Member
Thank you Plater.

Meanwile i solved my problem: it was related with a DataTable.Reset () i do between the 1st and 2nd call to the event.

I replaced the reset() with a clear() and now the DataGridView behaves as it should.
I don't understand, though, why the DataGrid was so closely bind to the DataTable that, with no DataBind in code, it reacted to its reset, but, when i populated the Table again, it did not react and show none of its records; instead, if i clear the Table data with the Clear() method, now it shows all the records.
From my point of view there's only one explanation: the Reset() method resets the DataBind also and the Clear() method don't.

Thank you again.
Joaquim
Nov 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2367
by: Bob | last post by:
During a form's development I noticed that when I changed bound dataset defintions with the dataset designer (for instance adding a column to one), when I come back and look at the form the ordering of the columns in the datagridviews has changed. The column order that I designed is totally different from what I had set using the datagridview column editor. As far as I'm concerned this is a bug that should be addressed real quick. its a...
4
22121
by: Stropher | last post by:
I have the following: this.dataGridViewBill.DataSource = tblResult; //hide the following columns this.dataGridViewBill.Columns.Visible = false; //email this.dataGridViewBill.Columns.Visible = false; //mobiltelefon When I run the programm, the columns are still displayed. I am developing with Visual C#2005 Express Edition Beta.
1
3746
by: George | last post by:
Hi, I have a DataGridView, bounded to a DataTable which is populated/updated by a OleDbDataAdapter (with OleDbConnection) to an Access Database. (Sounds familiar to some of you ... I have posted a number of related questions already.) I want to have a worker thread starting to consume data from the DataTable. Imagine that the DataTable contains a list of tasks. From the C# .NET specification, DataTable "is safe for multithreaded...
1
5013
by: Saravanan | last post by:
Hi, I am using DataGridView for displaying data I want to some idea about the following 1. How to split and merge cells in DataGridView 2. How to place column header in the bottom of the cell because by default it is coming as top of the cell, how to change the behaviour of the datagridview columnheader's Thanks in advance
3
2296
by: RichT | last post by:
Hi all, I am experiencing some odd behaviour with a DataGridView. The DataGridView is bound to a DataTable, which is populated with data from a csv file. The column Headings appear fine, but the data only appears if I select a cell otherwise the DGV look empty apart from the selected cell.
4
8458
by: Michael Torville | last post by:
Does anyone know if it's possible to prevent a user from entering a particular "DataGridView" cell. I simply want to redirect them into the next available cell but nothing I try works. Setting the "CurrentCell" property for instance causes no end of problems. If I call it in a "CellEnter" handler I get the error "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" (which others on the...
3
8539
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the following questions: 1. Can DataReader be bound DIRECTLY to DataGridView? 2. If DataReader cannot be bound directly to DataGridView, how can I load a DataReader into a DataTable and bind the DataTable to DataGridView? Can the DataTable be a...
5
2858
by: cj | last post by:
In VB 2008 how do I link the datagridview to a dataset? mydataadapter.fill(myds, "test") datagridview1.???????? When I was using 2005 I used the datagrid and it was done like: mydataadapter.fill(myds, "test") datagrid1.DataGrid1.SetDataBinding(ds, tableName)
0
1886
by: jchaturv | last post by:
All, I have a customized DataGridView where I have implemented extra functionality and bounded datasource in the same custom DataGridView (Using C# and .NET). Now, I could able to use it properly by placing it on a panel control. I have added label as button on panel control to display data from datasource on to datagrid and create a binding source. Another Label which act as a button is used to update data from grid to databse.
0
9663
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
9511
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
10404
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...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
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
6765
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
5415
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...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.