0

I have datagridview which has three columns in which two columns are simple non- editable text columns.Third one is a combo box column.If I change a value from dropdown of combo box and click another cell, value is reset to default.Selection changed is not retained.

Which event should I handle the selection change? How do I retain the value in combo box selection?

I am attaching data source in the below mentioned

DataGridViewComboBoxColumn checkType = (DataGridViewComboBoxColumn)grdCheck.Columns[(int)CheckColumn.CheckType];
CheckType.DisplayMember = "Name";
CheckType.ValueMember = "Name";

CheckType.DataSource = CheckTypes();

whereas CheckTypes() contains

CheckTypes()
{
   var CheckTypes = new[] 
   { 
      new { Name = "None", Id = "1" },
      new { Name = "View", Id = "2" },
      new { Name = "Edit", Id = "3" }
   };

  return CheckTypes;
}

I have not added events here as I have a save button which saves the selection directly from datagridview to database.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Where is the code that applies the `DataSource`? You're not doing that on selection change are you? – Crowcoder Jun 25 '18 at 11:48
  • I am doing CheckType.DataSource = CheckTypes(); during form initialization.Yes you right.I am not doing that on selection change.Should I add it also during selection change? Thanks for immediate help :) – CuriousExtreme Jun 25 '18 at 11:55
  • No, definitely not. Your value should stick, there must be something about some code you haven't shown. – Crowcoder Jun 25 '18 at 11:57
  • Exactly,I am not getting how to make the modified selection retain.I am assuming that it will happen here itself (CheckType.DataSource = CheckTypes();) This is the total code I have. – CuriousExtreme Jun 25 '18 at 12:07
  • I am not sure which event will be called during selection change of combobox in datagridview. – CuriousExtreme Jun 25 '18 at 12:11
  • I doubt you are even concerned with the selection changed event. If you have properly data bound your data to the grid then change tracking will be handled. [On my blog](https://contrivedexample.com/2017/05/27/c-data-binding-example-with-adapters-and-datagridview/) I have a link to a project that demonstrates databinding with a grid and combobox columns. It might help you to look it over. – Crowcoder Jun 25 '18 at 12:15
  • Thank you for sharing you blog,I am checking your project.I was just assuming some missing links has to be provided, may be during an event, so the thoughts :) – CuriousExtreme Jun 25 '18 at 12:21
  • Thank you for sharing your project, It helped me in understanding binding combo box of data grid view with underlying database. – CuriousExtreme Jul 15 '18 at 07:03

0 Answers0