Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag 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?

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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?