473,396 Members | 1,671 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.

DataGridView Behaviour

Helo,

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

The DataGridView has 2 columns: colArtigoGenerico, 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.CArtigoGenerico, a.CArtigoFinal " +
"FROM ArtigosGenericos a ORDER BY a.CArtigoGenerico ";
SqlDataAdapter adaptador = new SqlDataAdapter(select, ligacao);
adaptador.Fill(Detalhe, "Detalhes");

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

DGV.Columns["colArtigoGenerico"].DataPropertyName = "CArtigoGenerico";
DGV.Columns["colArtigoFinal"].DataPropertyName = "CArtigoFinal";

ColArtigoGenerico.ValueMember = DGV.Columns[0].DataPropertyName;
ColArtigoFinal.ValueMember = DGV.Columns[1].DataPropertyName;

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 1544
Plater
7,872 Expert 4TB
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
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 Expert 4TB
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
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 Expert 4TB
If you added those columns more then once (some people do that) it confuses it
Nov 14 '07 #6
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
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...
4
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 =...
1
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...
1
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...
3
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,...
4
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...
3
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...
5
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: ...
0
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.