Link to home
Start Free TrialLog in
Avatar of finance_teacher
finance_teacher

asked on

C# -- DataGridView "AuotSizing" HEIGHT ?

How can I setup the attached 1.jpg "COMPONENTS"
and "SUPPLIERS" children to automatically resize
themselves like the attached 2.jpg ?

Maybe something like an
"AutoSizeGridResults=TRUE"
PROPERTY or some 3rd party control ?
0001.jpg
0002.jpg
Avatar of mikeada
mikeada
Flag of United States of America image

1.jpg and 2.jpg look the same to me.
Avatar of finance_teacher
finance_teacher

ASKER

1 has a GRAY gap between the COMPONENTS and SUPPLIERS table.

I want NO gap and allow the COMPONENTS table height to
autogrow / autoshrink like what is doable in Oracle JDeveloper.

Is this doable in VS 2008 dotNet 2 ?
OH, ok I see now I was looking at the columns for some reason.  Are you using a binding source?  The first thing I think of is to hook up to the datasource changed event on the BindingSource for the Components.  Inside that event I'd check the total number of rows and adjust the height of the data grid to fit, then call this.PerformLayout()

If you set your MasterGroup object to be Dock Top, then the components tool strip to dock top, then the components datagrid to dock top then the suppliers toolstrip to dock top, then the final data grid to dock Fill...  everything should layout with PerformLayout()

Do you have any working examples or URLs so I can learn ?
Do you need an example of how to dock components?
For the code, assuming that you have a a datagrid called dataGridViewComponents that is using bindingSource1 as it's binding source, put this in the DataSourceChanged event  of the bindingsource:

        private void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
            this.dataGridViewComponents.Height = this.dataGridViewComponents.RowTemplate.Height * this.bindingSource1.Count;
            this.PerformLayout();
        }
I added the below, ran the app, messageboxes displayed, I then changed the parameters again, clicked SUBMIT, and no messageboxes displayed, therefore this is NOT settable via the below EVENTS
(including the "DataSourceChanged" EVENT you talked about above.

How is this doable ?
--------------------------------------------------------------------------------------------------------------
        private void gMLCOMPONENTSBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_CurrentChanged");
        }

        private void gMLCOMPONENTSBindingSource_DataMemberChanged(object sender, EventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_DataMemberChanged");
        }

        private void gMLCOMPONENTSBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_CurrentItemChanged");
        }

        private void gMLCOMPONENTSBindingSource_DataError(object sender, BindingManagerDataErrorEventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_DataError");
        }

        private void gMLCOMPONENTSBindingSource_DataSourceChanged(object sender, EventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_DataSourceChanged");
        }

        private void gMLCOMPONENTSBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_ListChanged");
        }

        private void gMLCOMPONENTSBindingSource_PositionChanged(object sender, EventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_PositionChanged");
        }

        private void gMLCOMPONENTSBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            MessageBox.Show("gMLCOMPONENTSBindingSource_BindingComplete");
        }
Did you register the events with the BindingSource?

I tested my solution so I know it works.  We're just missing something here.  When you say you "changed the parameters again" what do you mean by that?  Changed them from what to what?
Yes, they are registered since
I did all EVENT setup via attached.

ran the app, messageboxes displayed,
I then changed the parameters again
  ** changed "Search by" parameters
clicked SUBMIT, and no messageboxes
displayed, therefore this is NOT
settable via the EVENTS

I am using VS 2008 dotNet 2.0

Please post a before and after image
of you DataGridView automatically
resizing and let me know what dotNet
version you are using.

Thanks
attached
0002.jpg
OK, I see.  I was using 3.5, but the example below I did on 2.0.  Don't you have a main binding source for the entire form?  Sorry if I wasn't clear but you should bind on the bindingSource for the main part if the Components binding source is just a filtered view of the main one.  The actual event that you bind to will depend, good thinking trying to bind to all of them.

Here are screenshots and some sample code.  If you need the Form.designer.cs code too let me know.  Hope this helps.  I think we just need to find the right event depending on your setup.  ALSO, I updated the resize code a little bit.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Height = this.dataGridView1.RowTemplate.Height * (this.bindingSource1.Count + 1);
            this.PerformLayout();
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            BindingList<Component> cs = new BindingList<Component>();
            cs.Add(new Component() { Name = "First" });
            cs.Add(new Component() { Name = "Second" });
            //cs.Add(new Component() { Name = "Third" });
            //cs.Add(new Component() { Name = "Fourth" });
            bindingSource1.DataSource = cs;
            
        }
    }
 
    public class Component
    {
        public string Name { get; set; }
    }
}

Open in new window

FormDesignSize.PNG
FormRunSizeWith2Items.PNG
FormRunSizeWith4Items.PNG
Below still fails.
Maybe I have something preventing this from working in my APP ?
------------------------------------------------------------------------------------
1. click on "gML_PKGCOMPONENTSBindingSource" COMPONENT
2. EVENTS
3. DoubleClick "DataSourceChanged"
4. add below code
            this.dataGridView1.Height = this.dataGridView1.RowTemplate.Height * (this.bindingSource1.Count + 1);
            this.PerformLayout();
5. add below code to bottom of existing "Form1_Load" method
            BindingList<Component> cs = new BindingList<Component>();
            cs.Add(new Component() { Name = "First" });
            cs.Add(new Component() { Name = "Second" });
            //cs.Add(new Component() { Name = "Third" });
            //cs.Add(new Component() { Name = "Fourth" });
            gML_PKGCOMPONENTSBindingSource.DataSource = cs;
6. add below code to "Form1"
           public class Component
           {
              public string Name { get; set; }
           }
7. run app
8. attached displays BLANK "Components"
   SECTION on all records
attached
0002.jpg
I can see from the screenshot that it actually worked...  You can clearly see that the datagrid size is the same size as the table for components.  What you're missing from my original post is that  you also need to set the Dock property of everything on your form to TOP (You'll also have to make sure the order is correct.  If you have something that is out of order, for example, suppliers ends up above components, then right click on the item and click "send to back" or "send to front" for each item until it's in the correct order.

Also, you can google the property Dock or look in msdn for more information on that.  Make sure you also learn the Anchor property as well.
ASKER CERTIFIED SOLUTION
Avatar of mikeada
mikeada
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial