locked
is it possible to have build in code in a custom component that fires after component creation on a form? RRS feed

  • Question

  • I'm coding a custom DataGridView component and I want to set some styles based on the data inside the component. I want this to be automatic functionality of the component (i.e. it formats a cell blue if it contains the value 1 and has that cellstyle as a property on the component)

     

    I wrote working code to do it after the component is generated via an event, like a keypress or a button, but i want it to happen automatticaly when the form is opened. And coded within the control class itself if possible, so when i add the control that it's default behaviour without coding new events.

     

    So all another dev would have to do is drag the control on the form, set up cell styles and thats it.

     

    Thanks for the help

    Tuesday, August 23, 2011 7:54 AM

Answers

  • Hi,

    maybe the HandleCreated-Event:

      public class MYData : DataGridView
      {
        private int _f = 0;
    
        public MYData()
        {
          _f = 1000;
        }
        protected override void OnHandleCreated(EventArgs e)
        {
          base.OnHandleCreated(e);
    
          //do this or that...
          MessageBox.Show(_f.ToString());
        }
      }
    

    Regards,

      Thorsten

    • Marked as answer by Yev001 Wednesday, August 24, 2011 6:38 AM
    Tuesday, August 23, 2011 3:04 PM
  • Thanks,

    Tried overriding other events and this one worked:

     

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
          base.OnDataBindingComplete(e); ApplyStyleFormat();
        }
    

    • Marked as answer by Yev001 Wednesday, August 24, 2011 6:38 AM
    Wednesday, August 24, 2011 6:38 AM

All replies

  • Hi,

    maybe the HandleCreated-Event:

      public class MYData : DataGridView
      {
        private int _f = 0;
    
        public MYData()
        {
          _f = 1000;
        }
        protected override void OnHandleCreated(EventArgs e)
        {
          base.OnHandleCreated(e);
    
          //do this or that...
          MessageBox.Show(_f.ToString());
        }
      }
    

    Regards,

      Thorsten

    • Marked as answer by Yev001 Wednesday, August 24, 2011 6:38 AM
    Tuesday, August 23, 2011 3:04 PM
  • Just tried this
        protected override void OnHandleCreated(EventArgs e)
        {
          base.OnHandleCreated(e);
          ApplyStyleFormat();
        }
    


    But didnt work.

     

    I achieved what I wanted via a regular method in the control, but it needs to run from the parent form's Activated event and the columns must be manually generated during binding. If any one knows of a better way to do this, please let me know.

    //Inside the control
    
        public void PopulateGrid()
        {     
          AutoGenerateColumns = false; //manualy generate columns
          Columns.Clear(); //clears any old columns from the grid
          DataSource = createDS(); //creates a table and returns it as a datasource (columns added here manually)
          ApplyStyleFormat(); //sets styles depending on column/cell/row types via properties on the control
        }
    
    //add columns manually during createDS method
        private BindingSource createDS()
        {
          DataTB = new DataTable();
          BindingSource _bs = new BindingSource();
    
          addColumn("Name", typeof(string));
          //code to add data to table here
    
          _bs.DataSource = DataTB;
          return _bs;
        }
    
    
        private void addColumn(string p_ColName, Type type)
        {
          string ColName = p_ColName;
          DataTB.Columns.Add(ColName, type);
          Columns.Add(ColName, ColName);
          Columns[ColName].DataPropertyName = ColName;
        }
    
    //....Parent Form event (control is called dataGridView)
    
        private void ListEditTags_Activated(object sender, EventArgs e)
        {
          dataGridView.PopulateGrid();
          ListEditBindNavBar.BindingSource = (BindingSource)dataGridView.DataSource;
        }
    
    
    
    Tuesday, August 23, 2011 3:24 PM
  • Hi,

    there's also the ParentChanged-Event that could be overridden, you can check some variable/values to decide what to do, cause this event might be fired more than once...

    Inside your control:

     protected override void OnParentChanged(EventArgs e)
     {
     base.OnParentChanged(e);
    
     //if(this.Parent.GetType().Equals(...))
     MessageBox.Show(_f.ToString());
     }
    

     


    Regards,

      Thorsten






    Tuesday, August 23, 2011 3:32 PM
  • I think you can override the DataGridView.OnCellValueChanged Method and DataGridView.OnCellValidated Method like methods.

    And set that cell style in one of those method.

     

    If there's any concern, please feel free to let me know.

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 24, 2011 5:56 AM
  • Thanks,

    Tried overriding other events and this one worked:

     

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
          base.OnDataBindingComplete(e); ApplyStyleFormat();
        }
    

    • Marked as answer by Yev001 Wednesday, August 24, 2011 6:38 AM
    Wednesday, August 24, 2011 6:38 AM
  • You're welcome!

    If you want response to every cell then the event mentioned in my post is hit it, and if you want to response to the whole DataGridView, then the method you used is hit it.


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 24, 2011 7:14 AM