Archive for the ‘Uncategorized’ Category

dOOdads MarkAsDeleted might mislead you.

Wednesday, May 14th, 2008

For everyone who starts using MyGeneration.dOOdads (and suffices it to read the quick reference), a problem might happen when deleting multiple rows through iteration (as shown below) - some rows don’t get deleted.

Places places = new Places();
places.Where.Name.Value = "ABC%";
places.Where.Name.Operator = WhereParameter.Operand.Like;
places.Query.Load();
if(places.RowCount > 0)
{
   places.Rewind();
   do
   {
      places.MarkAdDeleted();
   } while( places.MoveNext()) ;
   places.Save();
}

Reason: You should not use MarkAsDeleted in a loop through a collection, because although this method does not physically remove a row (as the quick reference states), it does make the row invisible to a foreach (this is what the reference don’t tell you and is hard to guess). And it is obvious that you shouldn’t do anything inside a foreach that changes the contents of the collection.

Solution: You may instead use the DeleteAll() method, which deletes all rows resulted by your query (or filter) not all rows in the table. And don’t forget to call Save(), by the way.

Places places = new Places();
places.Where.Name.Value = "ABC%";
places.Where.Name.Operator = WhereParameter.Operand.Like;
places.Query.Load();
places.DeleteAll();
places.Save();

Mahdieh

Two Little Problems

Tuesday, February 5th, 2008

Two little problems that you may encounter in a Windows Application project:

1- You are using a DataGridView and want to change some (all) rows’ (cells) style, for example their color. Setting the Style property of rows or cells is the way to do so, but it won’t change anything unless done in dataGridView_RowsAdded event handler - at least I couldn’t find any other way to make it work.

2- You have a TabControl and usually want to make TabPages closable (using a ContextMenu)
and ,of course, don’t want the context menu to open inside the client area of the tab page, but just on the small header rectangle, as in VisualStudio tab pages.

Solution: ContextMenu has an Opening event and TabControl has a MouseDown event which fires before the ‘Opening’ event, so you’re gonna use them to cancel the opening whenever the click is not in the right place. Obviously, you should use a private bool allowContextMenuOpening.

private void tabControlPrograms_MouseDown(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Right)
      for (int i = 0; i < tabControlPrograms.TabCount; i++)
         if (tabControlPrograms.GetTabRect(i).Contains(e.Location))
         {
            allowContextMenuOpening = true;
            return;
         }
}

private void closeVisitorContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
   if (!allowContextMenuOpening)
      e.Cancel = true;
   else
      allowContextMenuOpening = false;
}

Mahdieh

Hello world!

Wednesday, December 12th, 2007