Getting the DataSource of DataBound DataGridView when source is POCO Class (C# Windows Forms App)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
To add rows to a Databound DataGridView, you must add the rows to the related BindingSouce. I am unable to do that in the same way I have seen in other related posts because the DataSource in those posts were DataTables. The DataSource for
my dgvs are based on POCO classes:
I followed this short walk through to create this form(see code below) http://msdn.microsoft.com/en-us/library/gg197523(v=vs.103).aspx
http://msdn.microsoft.com/en-us/library/gg197523(v=vs.103).aspx
I added each DataSource (class definition are in code below) to the Designer, changed their default view to datagridview and dragged each source to the form. Dragging Photographer automatically created grids and the binding sources for
both the parent grid (Photographers) and child grid (Assignments). I did the same thing with Events to which Photographers are assigned.
So there 3 dgv: photographerList, assignmentList, eventList.
Objective: drag selected Events from eventList to assignmentList and create a record of assignments. The drag and drop part goes fine. I do not know how to get the DataSource of the the assignmentBindingSource to add records to that DatasSource.
The final method of the code below contains the entire code of the assignmentList_DragDrop method. DragDrop is irrevelant because it is working. I just cant do what I need to do. The excerpt below shows the problem.
//cannot get correct data source to enable adding rows to the assignments DataGridView<br/>
//?? = assignmentsBindingSource.DataSource;
I have a referenced to the dropped events called draggedEvent as a DataGridViewSelectedRowCollection. I just need to iterate through it and assign events (add rows) to the datasource of the assignmentList dgv.

Any help would be appreciated.


<pre> public class Photographer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Phone { get; set; }

public virtual ObservableListSource<PhotographerAssignment> Assignments { get; set; }

}

public class PhotographerAssignment
{
public int PhotographerID { get; set; }
public string LabOrderNbr { get; set; }
public string RoleName { get; set; }
public string PayType { get; set; }
public int? SetID { get; set; }
public decimal RateAdjustment { get; set; }
public decimal Bonus { get; set; }

// Navigation properties
public virtual Photographer Photographer { get; set; }
}

public class ObservableListSource<T> : ObservableCollection<T>, IListSource
where T : class
{
private IBindingList _bindingList;
bool IListSource.ContainsListCollection { get { return false; } }

IList IListSource.GetList()
{
// ToBindingList is defined in the EntityFramework.dll, System.Data.Entity namespace.
return _bindingList ?? (_bindingList = this.ToBindingList());
}
}


public partial class PhotographerAssignment : Form
{

private EFDbContext context = new EFDbContext();


public PhotographerAssignment()
{
InitializeComponent();
}

private void closeApplication(object sender, EventArgs e)
{
Application.Exit();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Text = "Photographer Assignment System";
try
{
context.Photograhers.OrderBy(p => p.LastName).Load();


// load both Photographer DataGridView and related Assignments in Assignment Datagrid of current selected photographer
this.photographerBindingSource.DataSource = context.Photograhers.Local.ToBindingList();

var dt = DateTime.Parse("05/01/2011");
// load Events which can be assigned to 1 or more Photographers
var currentEvents = context.Events.Where(d => d.EventDT > dt).ToList(); //read only list of Events
this.eventList.DataSource = currentEvents;
}
catch (Exception ex)
{
string msg = ex.Message + " " + ex.InnerException.Message;
}
} // end onload

private void photographerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();

this.context.SaveChanges();

this.photographerList.Refresh();
this.assignmentList.Refresh();

}

private void eventList_MouseDown(object sender, MouseEventArgs e)
{
if (eventList.SelectedRows.Count > 0)
{
DataGridViewSelectedRowCollection dragEvents = eventList.SelectedRows;
eventList.DoDragDrop(dragEvents, DragDropEffects.Copy);
}
} // end MouseDown on Event DataGridView

private void assignmentList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
this.Text = "Drag Enter";
e.Effect = DragDropEffects.Copy;
}
} // end DragEnter on Assignment DataGridView

private void assignmentList_DragDrop(object sender, DragEventArgs e)
{
this.Text += " Dropped";
try
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
Type myType = typeof(DataGridViewSelectedRowCollection);
DataGridViewSelectedRowCollection draggedEvents = (DataGridViewSelectedRowCollection)e.Data.GetData(myType);

//cannot get correct data source to enable adding rows to the assignments DataGridView
//?? = assignmentsBindingSource.DataSource;


foreach (DataGridViewRow rw in draggedEvents)
{
//To do: add logic to add assignments rows to datasource of the assingmentBindingSource
}
}
}
catch (Exception ex)
{
string msg = ex.Message + " " + ex.InnerException.Message;
}


} // end DragDrop on Assignment DataGridView
}


[/code]
<hr class="sig WhiskeyRomeoLima

View the full article
 
Back
Top