Windows Develop Bookmark and Share   
 index > Windows Forms General > Binding DataGridViewComboBoxColumn in DataGridView using a BindingSource
 

Binding DataGridViewComboBoxColumn in DataGridView using a BindingSource

Hi,

This is another issue with the same project asked aboutand answered a few days ago on this forum.

I have a form w/ three DataGridViews (dgvInterivews, dgvDemarcationSets & dgvCodingSets), all related in a parent-child-grandchild kind of relationship. There are three BindingSources providing the two-way databinding for the grids, as well as managment of the parent-child relationships . This form performs as a dialog box, which can be instantiated in either read-only or in editable mode.
All works great, except...

The third grid, dgvCodingSets, has two fields which display as text boxes in read-only mode, and as a combo-box in edit mode. They bind & display correctly in read-only mode, but I am so far unable to both populate and data-bind the combo boxes in edit mode.

How do Ipopulate and data-bind the combo boxes?

The (relevant) code is below.

Thanks.

Dan



public class dlgBrowseInterviews : System.Windows.Forms.Form
{
	public int InterviewID { get; set; }
	public int DemarcationSetID { get; set; }
	public int CodingSetID { get; set; }
	private string Mode { get; set; }

	private BindingSource bsInterviews = new BindingSource();
	private BindingSource bsDemarcationSets = new BindingSource();
	private BindingSource bsCodingSets = new BindingSource();

	DataSet dsInterviews;
	DataTable dtInterviews;
	DataTable dtDemarcationSets;
	DataTable dtCodingSets;

	public dlgBrowseInterviews(string strMode, string strSelectButtonText,
				int intInterviewID,
				int intDemarcationSetID,
				int intCodingSetID)
	{
		InitializeComponent();

		this.InterviewID = intInterviewID;
		this.DemarcationSetID = intDemarcationSetID;
		this.CodingSetID = intCodingSetID;

		bool boolIsMultiSelect = false;
		this.Mode = strMode;

		switch (strMode)
		{
			case "select" :	
				...
				break;
			case "delete":		
				...
				break;
			case "sets":		//  this is the editable mode
				lblDialogTitle.Text = "Demarcation and Coding Sets";
				pnlInterviewFields.Visible = true;
				dgvInterviews.Visible = false;
				btnSelect.Enabled = false;		//  until a change has been made
				break;
			default :
				//  should never get here	
				HelperStuff.ShowErrorMessage("Invalid switch value");
				break;
		}

		PopulateInterviewsGrid(boolIsMultiSelect, strSelectButtonText);
		btnSelect.Text = strSelectButtonText;
	}		//  close for dlgBrowseInterviews constructor


	private DataTable BindGrids(int intInterviewID, bool bFilterUserName, bool bFilterInterviews)
	{
		dgvInterviews.DataSource = bsInterviews;
		dgvDemarcationSets.DataSource = bsDemarcationSets;
		dgvCodingSets.DataSource = bsCodingSets;

		//  get the data
		string[] arTableNames = new string[] { "Interviews", "DemarcationSets", "CodingSets" };
		string[,] arParams = 
					{
						{"FilterInterviews", bFilterInterviews.ToString(), "SqlDbType.Bit"},
						{"InterviewID", intInterviewID.ToString(), "SqlDbType.Int"},
						{"FilterUserName", bFilterUserName.ToString(), "SqlDbType.Bit"},
						{"UserName", HelperStuff.GetUserName(), "SqlDbType.Int"}
					};

		dsInterviews = DataHelper.GetDataSet("spGetInterviews", arParams, arTableNames);
		dtInterviews = dsInterviews.Tables["Interviews"];
		dtDemarcationSets = dsInterviews.Tables["DemarcationSets"];
		dtCodingSets = dsInterviews.Tables["CodingSets"];

		//  create the relationships
		DataColumn dcInterviewID1 = dtInterviews.Columns["InterviewID"];
		DataColumn dcInterviewID2 = dtDemarcationSets.Columns["InterviewID"];
		DataRelation drInterviewsToDemarcationSets = new DataRelation("InterviewsToDemarcationSets", dcInterviewID1, dcInterviewID2);
		dsInterviews.Relations.Add(drInterviewsToDemarcationSets);

		DataColumn dcDemarcationSetID1 = dtDemarcationSets.Columns["DemarcationSetID"];
		DataColumn dcDemarcationSetID2 = dtCodingSets.Columns["DemarcationSetID"];
		DataRelation drDemarcationSetsToCodingSets = new DataRelation("DemarcationSetsToCodingSets", dcDemarcationSetID1, dcDemarcationSetID2);
		dsInterviews.Relations.Add(drDemarcationSetsToCodingSets);

		//  bind the data
		bsInterviews.DataSource = dsInterviews;	//  comes from spGetInterviews
		bsInterviews.DataMember = "Interviews";

		bsDemarcationSets.DataSource = bsInterviews;
		bsDemarcationSets.DataMember = "InterviewsToDemarcationSets";

		bsCodingSets.DataSource = bsDemarcationSets;
		bsCodingSets.DataMember = "DemarcationSetsToCodingSets";
		return dtInterviews;
	}		//  close for BindGrids


	//  this is the OK button on the dialog
	private void btnSelect_Click(object sender, EventArgs e)
	{
		DataGridViewCell theCell;
		switch (this.Mode)
		{
			case "select":	
				...
				break;
			case "delete":		
				...
				break;
			case "sets":		
				//  DemarcationSetID
				theCell = dgvDemarcationSets[0, dgvDemarcationSets.CurrentCell.RowIndex];
				this.DemarcationSetID = Convert.ToInt32(theCell.Value);

				//  CodingSetID
				theCell = dgvCodingSets[0, dgvCodingSets.CurrentCell.RowIndex];
				this.CodingSetID = Convert.ToInt32(theCell.Value);

				bsDemarcationSets.EndEdit();
				bsCodingSets.EndEdit();
				Save();
				break;
			default:
				//  should never get here	
				HelperStuff.ShowErrorMessage("Invalid switch value");
				break;
		}
	}		//  close for btnSelect_Click


	private void dgvCodingSets_SelectionChanged(object sender, EventArgs e)
	{
		DataGridViewCell theCell = dgvCodingSets[0, dgvCodingSets.CurrentCell.RowIndex];
		this.CodingSetID = Convert.ToInt32(theCell.Value);
	}		//  close for btnSelect_Click


	private void dgvDemarcationSets_SelectionChanged(object sender, EventArgs e)
	{
		DataGridViewCell theCell = dgvDemarcationSets[0, dgvDemarcationSets.CurrentCell.RowIndex];
		this.DemarcationSetID = Convert.ToInt32(theCell.Value);
	}


	private void PopulateInterviewsGrid(bool boolIsMultiSelect, string strSelectButtonText)
	{	
		//  Disable event handlers to prevent spurious events
		//  re-enable at the end of the method
		this.dgvDemarcationSets.SelectionChanged -= new System.EventHandler(this.dgvDemarcationSets_SelectionChanged);
		this.dgvCodingSets.SelectionChanged -= new System.EventHandler(this.dgvCodingSets_SelectionChanged);

		DataTable dtInterviews = BindGrids(this.InterviewID, bFilterUserName, bFilterInterviews);

		//  the default grid style
		DataGridViewCellStyle styleDefault = new DataGridViewCellStyle();
		styleDefault.BackColor = SystemColors.Window;
		styleDefault.ForeColor = Color.Black;

		// Set the column header style.
		DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
		columnHeaderStyle.BackColor = Color.LightBlue;
		columnHeaderStyle.ForeColor = Color.Black;

		#region dgvInterviews
		dgvInterviews.ColumnHeadersVisible = true;
		dgvInterviews.AllowUserToAddRows = false;			
		dgvInterviews.AllowUserToDeleteRows = false;
		dgvInterviews.AllowUserToOrderColumns = true;
		dgvInterviews.AllowUserToResizeColumns = true;
		dgvInterviews.AllowUserToResizeRows = true;
		dgvInterviews.ReadOnly = true;
		dgvInterviews.DefaultCellStyle = styleDefault;
		dgvInterviews.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

		dgvInterviews.AutoGenerateColumns = false;
		dgvInterviews.Columns.Clear();

		DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
		colID.HeaderText = "ID";
		colID.Name = "ID";
		colID.Width = 100;		
		colID.DataPropertyName = "ID";
		dgvInterviews.Columns.Add(colID);

		DataGridViewTextBoxColumn colVisit = new DataGridViewTextBoxColumn();
		colVisit.HeaderText = "Visit";
		colVisit.Width = 100;
		colVisit.DataPropertyName = "Visit";
		dgvInterviews.Columns.Add(colVisit);

		DataGridViewTextBoxColumn colSite = new DataGridViewTextBoxColumn();
		colSite.HeaderText = "Site";
		colSite.Width = 100;
		colSite.DataPropertyName = "Site";
		dgvInterviews.Columns.Add(colSite);

		DataGridViewTextBoxColumn colTimeStamp = new DataGridViewTextBoxColumn();
		colTimeStamp.HeaderText = "Interview Date/Time";
		colTimeStamp.Width = 120;
		colTimeStamp.DataPropertyName = "InterviewTimeStamp";
		dgvInterviews.Columns.Add(colTimeStamp);

		DataGridViewTextBoxColumn colCreatedBy = new DataGridViewTextBoxColumn();
		colCreatedBy.HeaderText = "Created By";
		colCreatedBy.Width = 110;
		colCreatedBy.DataPropertyName = "CreatedBy";
		dgvInterviews.Columns.Add(colCreatedBy);

		DataGridViewTextBoxColumn colBeginCodingDate = new DataGridViewTextBoxColumn();
		colBeginCodingDate.HeaderText = "Begin Coding";
		colBeginCodingDate.Width = 120;
		colBeginCodingDate.DataPropertyName = "BeginCodingDate";
		dgvInterviews.Columns.Add(colBeginCodingDate);

		DataGridViewTextBoxColumn colSourceFile = new DataGridViewTextBoxColumn();
		colSourceFile.HeaderText = "Source File";
		colSourceFile.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		colSourceFile.DataPropertyName = "SourceFile";
		dgvInterviews.Columns.Add(colSourceFile);

		DataGridViewTextBoxColumn colInterviewID = new DataGridViewTextBoxColumn();
		colInterviewID.DataPropertyName = "InterviewID";
		colInterviewID.Name = "InterviewID";
		colInterviewID.Visible = false;
		dgvInterviews.Columns.Add(colInterviewID);
		#endregion		//  dgvInterviews

		#region  dgvDemarcationSets
		dgvDemarcationSets.ColumnHeadersVisible = true;
		dgvDemarcationSets.AllowUserToAddRows = false;			
		dgvDemarcationSets.AllowUserToDeleteRows = false;
		dgvDemarcationSets.AllowUserToOrderColumns = true;
		dgvDemarcationSets.AllowUserToResizeColumns = true;
		dgvDemarcationSets.AllowUserToResizeRows = true;

		if (strSelectButtonText != "Apply")				//  signifies read-only
			dgvDemarcationSets.ReadOnly = true;

		dgvDemarcationSets.DefaultCellStyle = styleDefault;
		dgvDemarcationSets.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

		dgvDemarcationSets.AutoGenerateColumns = false;
		dgvDemarcationSets.Columns.Clear();

		DataGridViewTextBoxColumn colDemarcationSetID = new DataGridViewTextBoxColumn();
		colDemarcationSetID.HeaderText = "ID";
		colDemarcationSetID.Name = "DemarcationSetID";
		colDemarcationSetID.Width = 50;		
		colDemarcationSetID.DataPropertyName = "DemarcationSetID";
		colDemarcationSetID.ReadOnly = true;
		dgvDemarcationSets.Columns.Add(colDemarcationSetID);

		DataGridViewTextBoxColumn colSetName = new DataGridViewTextBoxColumn();
		colSetName.HeaderText = "Set Name";
		colSetName.Width = 100;		
		colSetName.DataPropertyName = "SetName";
		colSetName.ReadOnly = false;
		dgvDemarcationSets.Columns.Add(colSetName);

		DataGridViewTextBoxColumn colDescription = new DataGridViewTextBoxColumn();
		colDescription.HeaderText = "Description";
		colDescription.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		colDescription.DataPropertyName = "Description";
		dgvDemarcationSets.Columns.Add(colDescription);
		#endregion  //		dgvDemarcationSets

		#region  dgvCodingSets
		dgvCodingSets.ColumnHeadersVisible = true;
		dgvCodingSets.AllowUserToAddRows = false;			
		dgvCodingSets.AllowUserToDeleteRows = false;
		dgvCodingSets.AllowUserToOrderColumns = true;
		dgvCodingSets.AllowUserToResizeColumns = true;
		dgvCodingSets.AllowUserToResizeRows = true;

		if (strSelectButtonText != "Apply")				//  signifies read-only
			dgvCodingSets.ReadOnly = true;

		dgvCodingSets.DefaultCellStyle = styleDefault;
		dgvCodingSets.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

		dgvCodingSets.AutoGenerateColumns = false;
		dgvCodingSets.Columns.Clear();

		DataGridViewTextBoxColumn colCodingSetID = new DataGridViewTextBoxColumn();
		colCodingSetID.HeaderText = "ID";
		colCodingSetID.Width = 50;		
		colCodingSetID.Name = "CodingSetID";
		colCodingSetID.DataPropertyName = "CodingSetID";
		colCodingSetID.ReadOnly = true;
		dgvCodingSets.Columns.Add(colCodingSetID);

		DataGridViewTextBoxColumn colCodingSetName = new DataGridViewTextBoxColumn();
		colCodingSetName.HeaderText = "Set Name";
		colCodingSetName.Width = 100;		
		colCodingSetName.DataPropertyName = "SetName";
		dgvCodingSets.Columns.Add(colCodingSetName);

		DataGridViewTextBoxColumn colCoder = new DataGridViewTextBoxColumn();
		colCoder.HeaderText = "Coder";
		colCoder.Width = 100;		
		colCoder.DataPropertyName = "CoderDisplayName";
		dgvCodingSets.Columns.Add(colCoder);

		if (strSelectButtonText != "Apply")				//  signifies read-only
		{
			DataGridViewTextBoxColumn colCodingSystem = new DataGridViewTextBoxColumn();
			colCodingSystem.HeaderText = "Coding System";
			colCodingSystem.Width = 100;		
			colCodingSystem.DataPropertyName = "CodingSystemName";
			dgvCodingSets.Columns.Add(colCodingSystem);
		}
		else
		{
			DataGridViewComboBoxColumn colCodingSystem = new DataGridViewComboBoxColumn();
			colCodingSystem.DataSource = HelperStuff.GetCodingSystems();
			colCodingSystem.DisplayMember = "SystemName";
			colCodingSystem.HeaderText = "Coding System";
			colCodingSystem.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
			colCodingSystem.DataPropertyName = "Coding System";
			colCodingSystem.ReadOnly = false;
			dgvCodingSets.Columns.Add(colCodingSystem);
		}

		DataGridViewTextBoxColumn colCodingSetDescription = new DataGridViewTextBoxColumn();
		colCodingSetDescription.HeaderText = "Description";
		colCodingSetDescription.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		colCodingSetDescription.DataPropertyName = "Description";
		dgvCodingSets.Columns.Add(colCodingSetDescription);
		#endregion  //		dgvCodingSets


		SelectCurrentRecords();

		this.dgvDemarcationSets.SelectionChanged += new System.EventHandler(this.dgvDemarcationSets_SelectionChanged);
		this.dgvCodingSets.SelectionChanged += new System.EventHandler(this.dgvCodingSets_SelectionChanged);
	}	//  close for PopulateInterviewsGrid

	private bool Save()
	{
		SqlDataAdapter daDemarcationSets = new SqlDataAdapter();
		SqlDataAdapter daCodingSets = new SqlDataAdapter();

		SqlConnection connection = new SqlConnection(DataHelper.GetConnectionString());
		connection.Open();			
		SqlTransaction transaction;
		transaction = connection.BeginTransaction();

		int intNumberRows;
		try
		{
			SqlParameter param;

			#region DemarcationSets
			//  create the DemarcationSets update & insert command objects
			SqlCommand cmdDemarcationSetsUpdateInsertCommand = new SqlCommand("spSaveDemarcationSet", connection);
			cmdDemarcationSetsUpdateInsertCommand.CommandType = CommandType.StoredProcedure;
			cmdDemarcationSetsUpdateInsertCommand.Transaction = transaction;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@DemarcationSetID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "DemarcationSetID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@InterviewID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "InterviewID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@SetName", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "SetName";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@DefaultCodingSetID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "DefaultCodingSetID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@Description", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "Description";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdDemarcationSetsUpdateInsertCommand.Parameters.Add("@ModifiedBy", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "ModifiedBy";
			param.SourceVersion = DataRowVersion.Current;

			daDemarcationSets.UpdateCommand = cmdDemarcationSetsUpdateInsertCommand;
			daDemarcationSets.InsertCommand = cmdDemarcationSetsUpdateInsertCommand;
			#endregion		//  DemarcationSets

			#region CodingSets
			//  create the CodingSets update & insert command objects
			SqlCommand cmdCodingSetsUpdateInsertCommand = new SqlCommand("spSaveCodingSet", connection);
			cmdCodingSetsUpdateInsertCommand.CommandType = CommandType.StoredProcedure;
			cmdCodingSetsUpdateInsertCommand.Transaction = transaction;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@CodingSetID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "CodingSetID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@DemarcationSetID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "DemarcationSetID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@CoderID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "CoderID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@CodingSystemID", SqlDbType.Int);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "CodingSystemID";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@SetName", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "SetName";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@Description", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "Description";
			param.SourceVersion = DataRowVersion.Current;

			param = cmdCodingSetsUpdateInsertCommand.Parameters.Add("@ModifiedBy", SqlDbType.VarChar);
			param.Direction = ParameterDirection.Input;
			param.SourceColumn = "ModifiedBy";
			param.SourceVersion = DataRowVersion.Current;

			daCodingSets.UpdateCommand = cmdCodingSetsUpdateInsertCommand;
			daCodingSets.InsertCommand = cmdCodingSetsUpdateInsertCommand;
			#endregion		//  CodingSets

			//  Now do all the updating.
			//  there are no deletes because they are handled by the buttons
			intNumberRows = daDemarcationSets.Update(dtDemarcationSets.Select("", "", DataViewRowState.Added | DataViewRowState.ModifiedCurrent));
			intNumberRows = daCodingSets.Update(dtCodingSets.Select("", "", DataViewRowState.Added | DataViewRowState.ModifiedCurrent));
			transaction.Commit();
			btnSelect.Enabled = false;
		}
		catch (Exception ex)
		{
			transaction.Rollback();
			HelperStuff.ShowErrorMessage("Error Saving set data: " + ex.Message);
			return false;
		}
		finally
		{
			connection.Close();
		}

		return true;
	}		//  close for Save

	private void SelectCurrentRecords()
	{
		//  select the current Interview record
		foreach (DataGridViewRow row in dgvInterviews.Rows)
		{
			if (Convert.ToInt32(row.Cells["InterviewID"].Value) == this.InterviewID)
			{
				dgvInterviews.CurrentCell = row.Cells[0];
				break;
			}
		}

		// select the current demarcation set
		foreach (DataGridViewRow row in dgvDemarcationSets.Rows)
		{
			if (Convert.ToInt32(row.Cells["DemarcationSetID"].Value) == this.DemarcationSetID)
			{
				dgvDemarcationSets.CurrentCell = row.Cells[0];
				break;
			}
		}

		// select the current Coding set
		foreach (DataGridViewRow row in dgvCodingSets.Rows)
		{
			if (Convert.ToInt32(row.Cells["CodingSetID"].Value) == this.CodingSetID)
			{
				dgvCodingSets.CurrentCell = row.Cells[0];
				break;
			}
		}
	}		//  close for SelectCurrentRecords

}		//  close for class dlgBrowseInterviews








Dan Hurwitz
dhurwitz  Thursday, September 17, 2009 3:01 PM

Hi Dan Hurwitz,

> has two fields which display as text boxes in read-only mode, and as a combo-box in edit mode. They bind & display correctly in read-only mode, but I am so far unable to both populate and data-bind the combo boxes in edit mode.

For this requirement, I think you can set DataGridViewColumn’s ReadOnly property to true if you want that column to be read only. To do this when using databinding, you’ll need to handle the DataBindingComplete event of DataGridView.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

{

dataGridView1.Columns[0].ReadOnly = true;

}

To populate DataGridViewComboBoxColumn, you can use the following sample

DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();

dgvComboBox.DataSource = dtComboBox;

dgvComboBox.DisplayMember = "Name";

dgvComboBox.ValueMember = "ID";

dgvComboBox.DataPropertyName = "Column1";

dgvCodingSets.Columns.Add(dgvComboBox);

dgvCodingSets.DataSource = bsCodingSets;

dtComboBox is a DataTable which has 2 column, id(int), name(string). After you populate the dgvComboBox with dtComboBox, you need to bind that column to the special column in DataGridView.

From your code, I know that you have bind bsCodingSets with bsDemarcationSets's DemarcationSetsToCodingSets table. So the DataPropertyName ought to be set to one of the column’s column name in DemarcationSetsToCodingSets table.

If you have any doubt with my post, please feel free to tell me.

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, September 18, 2009 5:52 AM
Thanks for the reply. It gave me the exact hint I needed to solve the problem. I had forgotten to set the ValueMember property, and also needed to tweak the DataPropertyName properties. Now it works like a charm.

Thanks.

Dan
Dan Hurwitz
  • Marked As Answer bydhurwitz Friday, September 25, 2009 4:01 PM
  •  
dhurwitz  Friday, September 25, 2009 4:00 PM

Hi Dan Hurwitz,

> has two fields which display as text boxes in read-only mode, and as a combo-box in edit mode. They bind & display correctly in read-only mode, but I am so far unable to both populate and data-bind the combo boxes in edit mode.

For this requirement, I think you can set DataGridViewColumn’s ReadOnly property to true if you want that column to be read only. To do this when using databinding, you’ll need to handle the DataBindingComplete event of DataGridView.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

{

dataGridView1.Columns[0].ReadOnly = true;

}

To populate DataGridViewComboBoxColumn, you can use the following sample

DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();

dgvComboBox.DataSource = dtComboBox;

dgvComboBox.DisplayMember = "Name";

dgvComboBox.ValueMember = "ID";

dgvComboBox.DataPropertyName = "Column1";

dgvCodingSets.Columns.Add(dgvComboBox);

dgvCodingSets.DataSource = bsCodingSets;

dtComboBox is a DataTable which has 2 column, id(int), name(string). After you populate the dgvComboBox with dtComboBox, you need to bind that column to the special column in DataGridView.

From your code, I know that you have bind bsCodingSets with bsDemarcationSets's DemarcationSetsToCodingSets table. So the DataPropertyName ought to be set to one of the column’s column name in DemarcationSetsToCodingSets table.

If you have any doubt with my post, please feel free to tell me.

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, September 18, 2009 5:52 AM
Thanks for the reply. It gave me the exact hint I needed to solve the problem. I had forgotten to set the ValueMember property, and also needed to tweak the DataPropertyName properties. Now it works like a charm.

Thanks.

Dan
Dan Hurwitz
  • Marked As Answer bydhurwitz Friday, September 25, 2009 4:01 PM
  •  
dhurwitz  Friday, September 25, 2009 4:00 PM
You are welcome. I am glad I can help you.

Have a good day.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Sunday, September 27, 2009 3:10 AM

You can use google to search for other answers

Custom Search

More Threads

• OpenFileDialog generating exception "The process cannot access this file because it is being used by another process"
• Certain letters being cut off in CheckedListBox
• When is the LostFocus event actually fired?
• Scope of Events
• reverse numbericupdown
• Keyevent F5
• Component attribute changing don't work
• Finding the same word twice in a richtextbox
• get the index of a controllers in a table layout panle in C#
• Publishing a WinForm Project - Missing Dlls