Avatar of rwheeler23
rwheeler23
Flag for United States of America asked on

C# datagridview property ColumnCount creates blank columns.

I was experimenting with properties of a datagridview and tried this.

dgvdataGridView.ColumnCount = 6;


The problem I had was the datagridview did have six columns but when the grid appeared on on the C# winform the first six columns were blank on all rows and the next 6 columns repeated with the same correct column headings with all the data. Why does it behave this way?

C#

Avatar of undefined
Last Comment
rwheeler23

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rwheeler23

ASKER
I think I am missing something with how datagridviews work. In my current application there are 6 fields in the query so therefore there are columns 0-5. I want to have total control of the grid and this includes the row header and column names and types. I do not want any autogenerating of columns.

I am correct in starting with
                dgvdataGridView.ReadOnly = false;
                dgvdataGridView.RowHeadersVisible = false;
                dgvdataGridView.AutoGenerateColumns = false;
                dgvdataGridView.DefaultCellStyle.NullValue = ' ';
                dgvdataGridView.DataSource = _ds.Tables["TableName"].DefaultView;

I tried something like this but it fails miserably:

                dgvdataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "User ID", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
                dgvdataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "Password", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
                dgvdataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "Last Changed Date", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
                dgvdataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "Changed By", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
                dgvdataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Admin", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
                dgvdataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "User Name", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });

In this case I am also try to convert Admin, which is defined as int in the database to a boolean and hence a checkbox.

Does it matter whether
                dgvdataGridView.DataSource = _ds.Tables["TableName"].DefaultView;
is positioned before or after adding the columns? I tried it both ways and neither worked. What is the correct code to get the datagridview to build properly?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy