I have two data tables set up in a Master-Details configuration with a relation/Foreign Key "Ticket_CallSegments" between them. I also have two Binding Sources and a Data Grid View configured like this
//
// dgvTickets
//
(snip irrelevent data)
this.dgvTickets.DataSource = this.ticketsDataSetBindingSource;
//
// ticketsDataSetBindingSource
//
this.ticketsDataSetBindingSource.DataMember = "Ticket";
this.ticketsDataSetBindingSource.DataSource = this.ticketsDataSet;
this.ticketsDataSetBindingSource.CurrentChanged += new System.EventHandler(this.ticketsDataSetBindingSource_CurrentChanged);
//
// callSegementBindingSource
//
this.callSegementBindingSource.DataMember = "Ticket_CallSegments";
this.callSegementBindingSource.DataSource = this.ticketsDataSetBindingSource;
this.callSegementBindingSource.Sort = "CreateDate";
Function to update the rich text box that will display the tickets.
private void ticketsDataSetBindingSource_CurrentChanged(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
rtbTickets.Clear();
foreach (DataRowView drv in callSegementBindingSource)
{
TicketsDataSet.CallSegmentsRow row = (TicketsDataSet.CallSegmentsRow)drv.Row;
sb.AppendLine("**********************************");
sb.AppendLine(String.Format("CreateDate: {1}, Created by: {0}", row.USERNAME, row.CREATEDATE));
sb.AppendLine("**********************************");
rtbTickets.SelectionFont = new Font("Arial", (float)11, FontStyle.Bold);
rtbTickets.SelectedText = sb.ToString();
rtbTickets.SelectionFont = new Font("Arial", (float)11, FontStyle.Regular);
rtbTickets.SelectedText = row.NOTES + "\n\n";
}
}
However when when I select a new row in my Data Grid View and ticketsDataSetBindingSource_CurrentChanged
gets called callSegementBindingSource.IsBindingSuspended
is set to true and my text box does not update correctly (it seems to always pull from the same row in CallSegments). Can anyone see what I am doing wrong or tell me how to unsuspend the binding so it will pull the correct data?
EDIT ---
Adding callSegementBindingSource.ResumeBinding()
at the start of the function does not change the status of IsBindingSuspended
EDIT -- I changed callSegmentBindingSource so it would be a instance varaible insted of created with the form however I got a new execption when i did that
private void ticketsDataSetBindingSource_CurrentChanged(object sender, EventArgs e)
{
var callSegementBindingSource = new BindingSource();
callSegementBindingSource.DataMember = "Ticket_CallSegments";
callSegementBindingSource.DataSource = this.ticketsDataSetBindingSource; // Errors on this line
callSegementBindingSource.Sort = "CreateDate";
callSegementBindingSource.ResumeBinding();
StringBuilder sb = new StringBuilder();
rtbTickets.Clear();
foreach (DataRowView drv in callSegementBindingSource)
{
TicketsDataSet.CallSegmentsRow row = (TicketsDataSet.CallSegmentsRow)drv.Row;
sb.AppendLine("**********************************");
sb.AppendLine(String.Format("CreateDate: {1}, Created by: {0}", row.USERNAME, row.CREATEDATE));
sb.AppendLine("**********************************");
rtbTickets.SelectionFont = new Font("Arial", (float)11, FontStyle.Bold);
rtbTickets.SelectedText = sb.ToString();
rtbTickets.SelectionFont = new Font("Arial", (float)11, FontStyle.Regular);
rtbTickets.SelectedText = row.NOTES + "\n\n";
}
}
I get the InvalidOperationExecption
System.InvalidOperationException was unhandled by user code
Message=Collection was modified; enumeration operation might not execute.
Source=System.Data
StackTrace:
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at System.Data.Index.InitRecords(IFilter filter)
at System.Data.Index..ctor(DataTable table, Int32[] ndexDesc, IndexField[] indexFields, Comparison`1 comparison, DataViewRowState recordStates, IFilter rowFilter)
at System.Data.Index..ctor(DataTable table, Int32[] ndexDesc, IndexField[] indexFields, DataViewRowState recordStates, IFilter rowFilter)
at System.Data.Index..ctor(DataTable table, IndexField[] indexFields, DataViewRowState recordStates, IFilter rowFilter)
at System.Data.DataTable.GetIndex(IndexField[] indexDesc, DataViewRowState recordStates, IFilter rowFilter)
at System.Data.DataTable.GetIndex(String sort, DataViewRowState recordStates, IFilter rowFilter)
at System.Data.DataView.UpdateIndex(Boolean force, Boolean fireEvent)
at System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, IFilter newRowFilter, Boolean fireEvent)
at System.Data.RelatedView.SetIndex(String newSort, DataViewRowState newRowStates, IFilter newRowFilter)
at System.Data.DataRowView.CreateChildView(DataRelation relation)
at System.Data.DataRelationPropertyDescriptor.GetValue(Object component)
at System.Windows.Forms.ListBindingHelper.GetList(Object dataSource, String dataMember)
at System.Windows.Forms.BindingSource.ResetList()
at System.Windows.Forms.BindingSource.set_DataSource(Object value)
at ContractFlowTool.Form1.ticketsDataSetBindingSource_CurrentChanged(Object sender, EventArgs e) in E:\Visual Studio 2010\Projects\Contract Flow Suite\Contract Flow Tool\CFTMain.cs:line 538
at System.Windows.Forms.BindingSource.OnCurrentChanged(EventArgs e)
at System.Windows.Forms.BindingSource.CurrencyManager_CurrentChanged(Object sender, EventArgs e)
at System.Windows.Forms.CurrencyManager.OnCurrentChanged(EventArgs e)
InnerException: