Can't get DataGridView inserted column to display

macnab

Member
Joined
Oct 26, 2014
Messages
12
Programming Experience
10+
I have created a datatable with the required values
I have created a DataGRidViewTextBoxColumn:
C#:
    foodNameTable = [COLOR=#191970][B]CreateFoodNameTable[/B][/COLOR]();
    foodNameColumn = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#191970][B]DataGridViewTextBoxColumn[/B][/COLOR]();
    foodNameColumn.Width = [COLOR=#00008b]280[/COLOR];
    foodNameColumn.HeaderText = [COLOR=#0000ff]"Eats"[/COLOR];
    foodNameColumn.Name = [COLOR=#0000ff]"Eats"[/COLOR];

In the DGV Paint event I have:
C#:
    dgvCats.Columns[[COLOR=#0000ff]"Id"[/COLOR]].Visible = [COLOR=#008b8b][B]false[/B][/COLOR];
         
    dgvCats.Columns[[COLOR=#0000ff]"Name"[/COLOR]].Width = [COLOR=#00008b]280[/COLOR];
    dgvCats.Columns[[COLOR=#0000ff]"Name"[/COLOR]].DisplayIndex= [COLOR=#00008b]0[/COLOR];
         
    dgvCats.Columns[[COLOR=#0000ff]"Food"[/COLOR]].Visible = [COLOR=#008b8b][B]false[/B][/COLOR];
         
[COLOR=#008000]//         dgvCats.Columns.Insert(1, foodNameColumn);[/COLOR]
    dgvCats.Columns.[COLOR=#191970][B]Add[/B][/COLOR](foodNameColumn);
    dgvCats.Columns[[COLOR=#0000ff]"Eats"[/COLOR]].DisplayIndex = [COLOR=#00008b]1[/COLOR];
    dgvCats.Columns[[COLOR=#0000ff]"Eats"[/COLOR]].Visible = [COLOR=#008b8b][B]true[/B][/COLOR];
            
    dgvCats.Columns[[COLOR=#0000ff]"BowlSize"[/COLOR]].Width = [COLOR=#00008b]65[/COLOR];
    dgvCats.Columns[[COLOR=#0000ff]"BowlSize"[/COLOR]].DisplayIndex = [COLOR=#00008b]2[/COLOR];
         
    dgvCats.Columns[[COLOR=#0000ff]"Name"[/COLOR]].ReadOnly = [COLOR=#008b8b][B]true[/B][/COLOR];
    dgvCats.Columns[[COLOR=#0000ff]"Eats"[/COLOR]].ReadOnly = [COLOR=#008b8b][B]true[/B][/COLOR];
    dgvCats.Columns[[COLOR=#0000ff]"BowlSize"[/COLOR]].ReadOnly = [COLOR=#008b8b][B]true[/B][/COLOR];

The column gets populated during the RowsAdded event;

I have tried both the .Add and the .Insert methods.
The column does not display.
 
There's some serious craziness going on there. Firstly, why would you be setting all those column properties in the Paint event handler (note "event handler", not "event", which is a method and a member of the form, not the grid)? The Paint event gets raised numerous times and for various reasons. Why would you need to set those properties every time the grid gets repainted? Surely you only need to set those properties once, when the form is first displayed.

What's actually going on here? Are you saying, without actually saying, that you want to add an unbound column to a bound DataGridView, i.e. the other columns are generated automatically when you bind a DataTable but you want that extra column added to the grid without it being bound to the DataTable?
 
There is an historical reason why it was done this (when things were simpler) and I just forgot to fix everything up.

EDIT: Deleted the bindingsource and added a new one. Fixed.
 
Last edited:
Back
Top Bottom