using System;
using System.Threading;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using WeifenLuo.WinFormsUI.Docking;
namespace IReaper.CourseData{
public partial class CourseDataGridView : IReaper.BaseControl.RightClickableDataGridView
{
CourseCollection collection;
Rectangle rec = Rectangle.Empty;
public CourseDataGridView()
{
InitializeComponent();
collection = new CourseCollection(CourseCollectionType.Common);
Core.CoreData.PropertyChanged += new PropertyChangedEventHandler(CoreData_PropertyChanged);
CourseXmlParser.OnBeforeCourseAdded += new EventHandler(BeforeCourseAdded);
}
public CourseDataGridView(IContainer container)
{
container.Add(this);
InitializeComponent();
collection = new CourseCollection(CourseCollectionType.Common);
Core.CoreData.PropertyChanged += new PropertyChangedEventHandler(CoreData_PropertyChanged);
}
protected override void OnSelectionChanged(EventArgs e)
{
base.OnSelectionChanged(e);
Core.CoreData[CoreDataType.CurrentSelectedCourses] = this.GetSelectedCourses();
}
/// <summary>
/// DataGridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnCellContextMenuStripNeeded(DataGridViewCellContextMenuStripNeededEventArgs e)
{
base.OnCellContextMenuStripNeeded(e);
if (e.RowIndex < 0)
{ return; }
//
DataGridViewRow cRow = this.Rows[e.RowIndex];
this.courseDataGridViewContextMenu.ContextCourse = cRow.DataBoundItem as Course;
e.ContextMenuStrip = this.courseDataGridViewContextMenu;
}
protected override void OnDataError(bool displayErrorDialogIfNoHandler, DataGridViewDataErrorEventArgs e)
{
e.ThrowException = false;
displayErrorDialogIfNoHandler = false;
base.OnDataError(displayErrorDialogIfNoHandler, e);
}
private void BeforeCourseAdded(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ThreadStart(delegate()
{
this.BeforeCourseAdded(sender, e);
}));
return;
}
this.DataSource = null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private CourseCollection GetSelectedCourses()
{
//
DataGridViewSelectedRowCollection allRows = this.SelectedRows;
//
collection.Clear();
//
foreach (DataGridViewRow r in allRows)
{
//Course
Course item = r.DataBoundItem as Course;
//
if (item == null)
{ continue; }
collection.Add(item);
}
return collection;
}
private void CoreData_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == CoreDataType.CurrentViewedCourses.ToString())
{
CourseCollection courses = Core.CoreData[CoreDataType.CurrentViewedCourses] as CourseCollection;
if (courses != null)
{
this.collection.Type = courses.Type;
}
this.DataSource = courses;
//find parent form and activated it.
DockContent parent = this.FindForm() as DockContent;
if (parent != null)
{
parent.Activate();
}
}
else if (e.PropertyName == CoreDataType.CurrentFilteredCourses.ToString())
{
CourseCollection courses = Core.CoreData[CoreDataType.CurrentFilteredCourses] as CourseCollection;
if (courses != null)
{
this.collection.Type = courses.Type;
}
this.DataSource = courses;
//find parent form and activated it.
DockContent parent = this.FindForm() as DockContent;
if (parent != null)
{
parent.Activate();
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
if (this.rec != Rectangle.Empty && !this.rec.Contains(e.X, e.Y))
{
this.DoDragDrop(this.SelectedRows, DragDropEffects.Move | DragDropEffects.Copy);
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
//Rect
if(this.SelectedRows.Count == 0)
{ this.rec = Rectangle.Empty; }
else//
{
this.rec = this.DisplayRectangle;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.rec = Rectangle.Empty;
}
}
}
|