I changed the code a little to use BindingSources - when I used the dataset as a datasource the dgKeywords (3rd gridview) wasn't actually being related to dgTestCases (2nd gridview). What I mean is that when I selected a row in dgTestCases it would not refresh the dgKeywords datagridview as you would expect when you have a master/detail setup. Below is my code that I'm currently using.
My objective is to allow users to re-order the execution of a keyword driven test harness by being able to drag and drop the rows within each grid. The code then would change a column value in these tables called "SortOrder" that I use to determine the execution order of these tests.
The drag/ drop events listed below are used for the dgTestCases (2nd gridview) as well except I substitute 'dgTestCases' where the 'dgKeywords' are. I can't get it to drag/drop for dgTestCases.
Sorry this post is so long, just wanted to provide you with an accurate picture of what I'm trying to do.
/// <summary>
/// Binds the data for datagridviews.
/// </summary>
private void BindDataGridViews(int iReleaseID)
{
ds =
ADataAccess.AllTestsInRelease(iReleaseID);
ds.DataSetName =
"TestsInRelease";
oTestPartitionBindingSource.DataSource = ds;
oTestPartitionBindingSource.DataMember =
"TestPartitions";
oTestCaseBindingSource.DataSource = oTestPartitionBindingSource;
oTestCaseBindingSource.DataMember =
"FK_TestPartitions_TestCases";
oKeywordsBindingSource.DataSource = oTestCaseBindingSource;
oKeywordsBindingSource.DataMember =
"FK_TestCases_TestKeywords";
if (ds.Tables["TestPartitions"].Rows.Count > 0)
{
BindPartitionGrid();
BindTestCaseGrid();
BindKeywordGrid();
}
}
private void BindPartitionGrid()
{
dgTestPartitions.DataSource = oTestPartitionBindingSource;
dgTestPartitions.Columns[0].Visible =
false;
dgTestPartitions.Columns[4].Visible =
false;
dgTestPartitions.Columns[1].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgTestPartitions.Columns[2].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgTestPartitions.Columns[3].SortMode =
DataGridViewColumnSortMode.NotSortable;
}
private void BindTestCaseGrid()
{
dgTestCases.DataSource = oTestCaseBindingSource;
if (ds.Tables["TestCases"].Rows.Count > 0)
{
dgTestCases.Columns[0].Visible =
false;
dgTestCases.Columns[1].Visible =
false;
dgTestCases.Columns[5].Visible =
false;
dgTestCases.Columns[2].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgTestCases.Columns[3].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgTestCases.Columns[4].SortMode =
DataGridViewColumnSortMode.NotSortable;
}
}
private void BindKeywordGrid()
{
dgKeywords.DataSource = oKeywordsBindingSource;
if (ds.Tables["Keywords"].Rows.Count > 0)
{
dgKeywords.Columns[0].Visible =
false;
dgKeywords.Columns[1].Visible =
false;
dgKeywords.Columns[5].Visible =
false;
dgKeywords.Columns[2].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgKeywords.Columns[3].SortMode =
DataGridViewColumnSortMode.NotSortable;
dgKeywords.Columns[4].SortMode =
DataGridViewColumnSortMode.NotSortable;
}
}
Drag - Drop Methods (essentially copied from one of the posts in the forum about drag/drop within a datagridview)
private void dgKeywords_DragOver(object sender, DragEventArgs e)
{
e.Effect =
DragDropEffects.Move;
}
private void dgKeywords_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = dgKeywords.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dgKeywords.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
ReviseSortOrder(rowIndexFromMouseDown, rowIndexOfItemUnderMouseToDrop,
"Keywords");
}
}
private void dgKeywords_MouseDown(object sender, MouseEventArgs e)
{
// So users can't drag a row from one gridview to another.
dgTestPartitions.AllowDrop =
false;
dgTestCases.AllowDrop =
false;
dgKeywords.AllowDrop =
true;
rowIndexFromMouseDown = dgKeywords.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown =
new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
{
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dgKeywords_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = dgKeywords.DoDragDrop(
dgKeywords.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);
}
}
}
private void ReviseSortOrder(int iRowIndexFromMouseDown, int iRowIndexOfItemUnderMouseToDrop, string sTableName)
{
// Logic here that alters a column called "SortOrder" in the sTableName for every row in the table.
// Updates the data in the database.
ResetGrids(); // Clears the grids, calls BindDataGridViews(cboRelease.SelectedValue)
}