Results 1 to 3 of 3

Thread: [RESOLVED] Why does my datagridviewcolumn not accept changes at this point?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Resolved [RESOLVED] Why does my datagridviewcolumn not accept changes at this point?

    I'm bringing down a strongly typed dataset table into a datagridview. then adding a column of combo boxes to let the foreign keys be selected by name instead of ID. Perhaps there's an easier way to do this.

    can I transform the typeID column into a comboboxcolumn while still being able to cast the datasource back to my strongly typed dataset table type? Do I need to get it back from the datasource or do changes on the datagridview automatically get passed back to the Dataset?

    What's wrong with this code?

    Code:
    FormDg fDg;
            internal DsTech.TechObjectsDataTable ShowDg(IWin32Window owner, Db db)
            {
                    fDg = new FormDg();
    
                    fDg._dg.DataSource = db.getDs().TechObjects;
                    var TypeSelecter = new DataGridViewComboBoxColumn();
                    var types = db.getdsObjectTypes();
                    TypeSelecter.DataSource = types;
                    TypeSelecter.DisplayMember = "type";
                    TypeSelecter.Name = "Types";
                    TypeSelecter.ValueMember = "ID";
                    fDg._dg.Columns["typeID"].DisplayIndex=fDg._dg.Columns.Count-1;
                    fDg._dg.Columns.Add(TypeSelecter);
                    fDg._dg.ReadOnly = false;
                    Application.DoEvents();
                    for (int i = 0; i < fDg._dg.RowCount; i++)
                    {
                            if ((fDg._dg.Rows[i].Cells["typeID"].Value is DBNull) == false)
                    //Line below does not work
                                    UpdateTypesRow(i,(int)fDg._dg.Rows[i].Cells["typeID"].Value);
                    }
                    fDg._dg.CellEndEdit += new DataGridViewCellEventHandler(_dg_CellEndEdit);
    
    
                    if (fDg.ShowDialog(owner) == DialogResult.OK)
                    {
                            return (DsTech.TechObjectsDataTable)fDg._dg.DataSource;
                    }
                    else return null;
    
            }
    While this code works just fine

    Code:
    void _dg_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                    if (fDg._dg.Columns[e.ColumnIndex].Name == "Types")
                    {
                            fDg._dg.Rows[e.RowIndex].Cells["typeID"].Value = fDg._dg.Rows[e.RowIndex].Cells["Types"].Value;
                            //update typeID column
    
                    }
                    else if (fDg._dg.Columns[e.ColumnIndex].Name == "typeID")
                    {
                            //update types column
                            UpdateTypesRow(e.RowIndex,(int) fDg._dg.Rows[e.RowIndex].Cells["typeID"].Value);
                    }
            }
    Here's the code I factored the 2 into just in case I was reading wrong and they were actually different

    Code:
      void UpdateTypesRow(int rowIndex, int value)
            {
                    fDg._dg.Rows[rowIndex].Cells["Types"].Value = value;
            }
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why does my datagridviewcolumn not accept changes at this point?

    You should be adding the combo box column BEFORE binding, not after. Follow the CodeBank link in my signature and you'll find a thread dedicated to the topic of adding combo boxes to a DataGridview.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: Why does my datagridviewcolumn not accept changes at this point?

    ok, I found the post I will look into it more as soon as I get to work tomorrow. Thanks
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width