I have a bound datagridview and I add 2 unbound columns and set the cell value for each row to "X" for the unbound PKG column and "Y" for the MISC column. When the window comes up, the 2 new columns are stil empty. I put a Msgbox to show that it does get there and it is populating the correct column. What am I missing?

// create new unbound columns
private void addUnboundCols()
{
    addNewCol( "MISC", "Miscellaneous");
    addNewCol( "PKG", "Package");        
}

private void addNewCol(string name, string headertext)
{
    DataGridViewTextBoxColumn newcol = new DataGridViewTextBoxColumn();
    newcol.Name = name;
    newcol.HeaderCell.Value = headertext;
    newcol.ValueType = typeof(string);
    newcol.Visible = true;
    newcol.ReadOnly = false;
    newcol.Frozen = false;
    newcol.Width = 50;
    dgvTbList.Columns.Add(newcol);  
}
// put Y in Misc unbound col; and X in PKG col
private void setNewColData()
{
            this.dgvTbList.EditMode = DataGridViewEditMode.EditProgrammatically;
            foreach (DataGridViewRow dgvrow in dgvTbList.Rows)
            {
                dgvrow.Cells["MISC"].Value = "Y";    
                dgvrow.Cells["PKG"].Value = "X";

                // now just to make sure the date was added it to the right place
                object val = dgvrow.Cells["PKG"].Value;                  
                string hdr  = dgvrow.Cells["PKG"].OwningColumn.HeaderText;
                MessageBox.Show(hdr + " val= " + val.ToString());  // this comes out correct
            }
            dgvTbList.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
            this.Refresh();
        }

When do you add them exactly? I hope AFTER binding it. And I would use Insert method, not Add. Where you have to specify 2 arguments:
- DGV column reference
- index of column to insert (starting from 0 up (0 is 1st column, 1 is 2nd column,...)

But I can see you actually ADD coumns - they exist.
You even add data into those 2 new columns correctly.

What you dont do right, is that 2nd last line of code, where you are trying to get "...OwningColumn.HreaderText".
What is this actually?
Remove that line, and try it out ones again, or only retreive the value from the cell like:

     object val = dgvrow.Cells["PRG"].Value;
    string hdr  = dgvrow.Cells["PKG"].Value.ToString();
    MessageBox.Show(hdr + " val = " + val.ToString());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.