mercredi 2 août 2017

Checkbox in one DevExpress grid triggers checkbox in second grid

I have two simple DevExpress grids which are connected with checkboxes and they both have only one column which is Name

When you click on Name in left grid, you get options in right grid and for each row in left grid you get different set of options in right grid

So left grid has always one data source and right grid has different data source each time different row is selected in left grid

I have two problems I can't solve so I need help

1. problem:

When I click check all button in grid header, it should check all checkboxes in both grids for each view. Now it checks all rows in left grid and only current data source for right grid. I need to check all for all data sources for right grid. Uncheck should work the same way too

2. problem:

When I click on checkbox in left grid, it checks all checkboxes in right grid. But when checkbox in left grid is unchecked and I check one of the checkboxes in right grids I need to checkbox in left grid automatically be selected too. Also, when checkbox in left grid is checked and I uncheck all checkboxes in right grid, the checkbox in left grid should be unchecked too

Below is the code for my small application

To put things in context, gridView1 is left grid and gridView2 is right grid

I have 3 rows in left grid and several rows in right grid for each row in left (string out of the backets are in the left grid and strings in the backets are in the right grid):

Application management (Databases, Site collection), Services on server (Services) and Applications (Web applications, Server applications)

public partial class Form1 : Form
        {
            List<OptionMaster> list = new List<OptionMaster>();

            public Form1()
            {
                InitializeComponent();

                list = new List<OptionMaster>()
                {
                    new OptionMaster()
                    {
                        Name = "Application management", 
                        Options = new List<Option>()
                        {
                            new Option() {Name = "Databases"},
                            new Option() {Name = "Site collection"},
                        }
                    },
                    new OptionMaster()
                    {
                        Name = "Services on server",
                        Options = new List<Option>()
                        {
                            new Option() {Name = "Services"}
                        }
                    },
                    new OptionMaster()
                    {
                        Name = "Applications",
                        Options = new List<Option>()
                        {
                            new Option() {Name = "Web applications"},
                            new Option() {Name = "Server applications"}

                        }
                    },
                };

                gridSharePointObjects.DataSource = list;
                gridView1.PopulateColumns();
            }

            private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
            {
                var obj = (gridView1.GetFocusedRow() as OptionMaster);
                gridSharePointObjectProperties.DataSource = obj.Options;
                gridSharePointObjectProperties.RefreshDataSource();
                gridView2.PopulateColumns();
            }

            private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
            {
                var obj = (gridView1.GetFocusedRow() as OptionMaster);
                obj.Checked = gridView1.IsRowSelected(gridView1.FocusedRowHandle);

                if (obj.Checked)
                {
                    gridView2.SelectAll();
                }
                else
                {
                    foreach (var row in gridView2.GetSelectedRows())
                    {
                        gridView2.UnselectRow(row);
                    }
                }

                gridSharePointObjectProperties.RefreshDataSource();
            }

            private void gridView2_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
            {
                var obj = (gridView2.GetFocusedRow() as Option);
                if (obj == null) return;
                obj.Checked = gridView2.IsRowSelected(gridView2.FocusedRowHandle);
            }

            private void gridView2_DataSourceChanged(object sender, EventArgs e)
            {
                var obj = gridView2.DataSource as List<Option>;
                if (obj == null) return;

                foreach (var b in obj.Where(o=>o.Checked))
                {
                    gridView2.SelectRow(gridView2.FindRow(b));
                }
            }

        }

        public class OptionMaster
        {
            public string Name
            {
                get;
                set;
            }

            [Browsable(false)]
            public bool Checked
            {
                get { return Options.Any(o => o.Checked); }
                set
                {
                    foreach (var option in Options)
                    {
                        option.Checked = value;
                    }
                }
            }

            [Browsable(false)]
            public List<Option> Options { get; set; }
        }

        public class Option
        {
            public string Name { get; set; }

            [Browsable(false)]
            public bool Checked { get; set; }
        }




Aucun commentaire:

Enregistrer un commentaire