views:

123

answers:

1

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?

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

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

 void UpdateTypesRow(int rowIndex, int value)
 {
  fDg._dg.Rows[rowIndex].Cells["Types"].Value = value;
 }
A: 

Using vbforums.com I found examples using binding sources and made sure to add the combo Box before setting the datagridview's datasource property and was able to get this working. The Thread and code in c#

Maslow