Results 1 to 2 of 2

Thread: [RESOLVED] Custom combobox with hosted Datagridview - cannot set datasource

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] Custom combobox with hosted Datagridview - cannot set datasource

    Hi,

    I've created a combobox that hosts Datagridview as dropdown, but I cannot set datasource of Datagridview:

    Code:
     class CustomCombo: ComboBox
        {
            new public event EventHandler DropDownClosed;
    
            private ToolStripDropDown tsdd = new ToolStripDropDown();
            private ToolStripControlHost tsch = null;
    
            protected override void OnDropDown(EventArgs e)
            {
                if (!tsdd.Visible)
                {
                    tsch.Control.Dock = DockStyle.Fill;
                    tsdd.Show(this, 0, this.Height);
                    tsdd.Size = tsch.Size;
                    tsch.Control.Location = new Point(0, 0);
                    tsch.Focus();
                }
            }
    
            protected override void OnHandleCreated(EventArgs e)
            {
                base.OnHandleCreated(e);
               
                ShowHost();
            }
    
            private void ShowHost()
            {
                //  configure parent drop container
                tsdd.BackColor = this.BackColor;
                tsdd.AutoSize = false;
    
                //  call extended implementations to provide a guest control
                tsch = new ToolStripControlHost(new DropGrid());
    
                //  configure host container
                //tsch.AutoSize = true;
                tsch.BackColor = tsdd.BackColor;
                tsch.Control.Location = new Point(0, 0);
              
                //  assign host to parent drop container
                tsdd.Items.Add(tsch);
                tsdd.Width = 1500;
    
                //  we need to track the drop window close event
                tsdd.Closed += new ToolStripDropDownClosedEventHandler(OnToolStripDropDownClosed);
            }
    
            private void OnToolStripDropDownClosed(object sender, ToolStripDropDownClosedEventArgs e)
            {
                //  here we raise the base ComboBox close event
                if (
                        this.IsHandleCreated &&
                        (DropDownClosed != null))
                {
                    //expanded = false;
                    if (DropDownClosed != null)
                        DropDownClosed(this, e);
                }
            }
    
        }
    
        /// <summary>
        /// Datagridview
        /// </summary>
        public class DropGrid: DataGridView
        {
            public DropGrid()
            {
    
            }
    
            protected override void OnHandleCreated(EventArgs e)
            {
                base.OnHandleCreated(e);
                
                FillGrid();
              }
    
           private void FillGrid()
            {
                
               var dt = new DataTable();
    
                try
                {
                    using (var conn = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\\Test.accdb"))
                    {
                        conn.Open();
    
                        using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employees"))
                        {
                            cmd.Connection = conn;
    
                            using (OleDbDataAdapter da = new OleDbDataAdapter())
                            {
                                da.SelectCommand = cmd;
                                da.Fill(dt);
                            }
                        }
                        this.DataSource = dt;
    
                 }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
    
                    return;
                }
            }
       }
    I've found something about setting binding context to hosted control, but I don't know how to do that. Can somebody help me ?
    Last edited by LuckyLuke82; Feb 9th, 2018 at 01:24 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Custom combobox with hosted Datagridview - cannot set datasource

    Solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width