Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Master/detail drag and drop problem.
 

Master/detail drag and drop problem.

I have 3 datagridviews that are master/details to each other:.

dgTestPartitions.DataSource = ds;

dgTestPartitions.DataMember = "TestPartitions";

dgTestCases.DataSource = ds;

dgTestCases.DataMember = "TestPartitions.FK_TestPartitions_TestCases";

dgKeywords.DataSource = ds;

dgKeywords.DataMember = "TestCases.FK_TestCases_TestKeywords";

I have been able to allow a user to drag/drop a row in the grandchild datagridview(dgKeywords) with no problems. The problem I'm having is that the parent or child datagridviews will not allow me to have this behaviour. I suspect this is because of the way the master/detail keeps track of the relationships between the datagridviews. When I try to drag a row in these two tables it will not allow me to drop them (I have already set the AllowDrop = true)

Thanks,

Jeff

Jeff Hale  Wednesday, December 21, 2005 2:09 PM

Not sure I understand what is going on. The grid certainly doesn't "restrict" anything just because you are in a master/details mode. The question is -- what are you doing when you drop a row to reorder? This operating might not be supported on your datasource.

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�/SPAN>

 

Mark Rideout  Thursday, December 22, 2005 1:17 AM

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)

}

Jeff Hale  Thursday, December 22, 2005 2:17 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView with RichTextBox Column
• Getting data from a cell
• Exponential slow down in Datagrid binding
• DataGridView Navigator toolbar
• Graphic artifacts while scrolling DataGridView without scroll
• DataGridView renders slowly, incompletely
• Binding Navigator and its delete button is inconsistant
• Udate or edit table in database at backend through making changes in datagridview
• Assining Null value to data Field - Help !!!
• How can I get the default image of DataGridViewImageCell?