10572 : Custom cells with custom draw and custom edit

Question

I want to create a cell with custom draw and also use a custom editor. Can this be done with the Grid from plexityHide?

Answer

Yes. You will need to implement 4 events

#1 OnGridCustomCellDraw, to control how the cell data will be drawn when not in edit mode


    private void gantt1_OnGridCustomCellDraw(PlexityHide.GTP.Grid aGrid, PlexityHide.GTP.CustomCellEventArgs e)
    {
      //e.G.DrawImage(pictureBox1.Image,e.Cell.CellRect.Left,e.Cell.CellRect.Top);
      int x = 0;
      if (e.Cell.Content.Value != null)
        x = (int)e.Cell.Content.Value;
      e.G.DrawString(x.ToString() + " Custom", aGrid.Font, new SolidBrush(Color.Firebrick), e.DrawRect.Location);
    }
#2 & #3 OnGridCustomCellEditStartByMouse and OnGridCustomCellEditStartByKey to start edit and create the custom editor, 
place it, fill it with the current cell value etc.
    private void gantt1_OnGridCustomCellEditStartByMouse(Grid aGrid, CustomCellEventArgs e)
    {
      // This event triggers for custom cells when edit Edit Starts from a mouseclick: Show editor, with data from Cell.Content.Value
      // Why two start edit events? Mouse and Key? - Well you may want to catch the key press as input, but click should selet all in the editor...
      if (e.MouseEventThatStartedEdit.Clicks==2)
        InitCustomEditor(aGrid,e);
    }

    private void gantt1_OnGridCustomCellEditStartByKey(Grid aGrid, CustomCellEventArgs e)
    {
      // This event triggers for custom cells when edit Edit Starts from a key press: Show editor, with data from Cell.Content.Value      
      // Why two start edit events? Mouse and Key? - Well you may want to catch the key press as input, but click should selet all in the editor...
      InitCustomEditor(aGrid,e);
    }
    
    private TrackBar customEditTrackBar=null;
    private void InitCustomEditor(Grid aGrid,CustomCellEventArgs e)
    {    
      customEditTrackBar = new TrackBar(); // keep the component until edit ends

      aGrid.Controls.Add(customEditTrackBar);
      customEditTrackBar.Bounds = e.Cell.Content.LimitRect;
      customEditTrackBar.AutoSize = false;

      customEditTrackBar.Maximum=100;
      customEditTrackBar.TickFrequency=10;
      int x=0;
      if (e.Cell.Content.Value!=null)
        x = (int)e.Cell.Content.Value;
      customEditTrackBar.Value=x;
      customEditTrackBar.Focus();
    }
	}

#4 OnGridCustomCellEndEdit that ends a current edit session and moves data from custom editor into the Cell

    private void gantt1_OnGridCustomCellEndEdit(Grid aGrid, CustomCellEventArgs e)
    {
      // This event triggers for custom cells when edit is done: move new data from editor to Cell.Content.Value
     
   
      if (customEditTrackBar != null)
      {
        e.Cell.Content.Value = customEditTrackBar.Value;
        aGrid.Controls.Remove(customEditTrackBar);
        customEditTrackBar = null;
      }

    }

Leave a Reply