10 Replies - 26044 Views - Last Post: 24 July 2012 - 09:23 PM Rate Topic: -----

#1 upndown4par  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 10

Set SelectedItem in DataGridViewComboBoxCell

Posted 10 March 2010 - 11:01 AM

Hi,

I have a Windows application, written in c# and in my application, I have a DataGridView that has 2 DataGridViewComboBoxColumns in it, along with a DataGridViewTextBoxColumn. The 2 ComboBoxColumns are each populated by a DataSet. For each row, the user selects an item from each of the ComboBoxColumns and enters a number into the TextBoxColumn. As an example, let's say the Color ComboBox has 3 choices in it: Red, Blue & Green and the Size ComboBox has 3 choices in it: Small, Medium & Large. In our example, for the first row, the user has selected Red from the Color ComboBox, Medium for the Size ComboBox and has entered "5" in the Qty TextBox. For Row 2, the user selected Green in that Color ComboBox, Large in the Size ComboBox and has entered "4" in the Qty TextBox.


Row Color(ComboBox) Size(ComboBox) Qty(TextBox)
----------------------------------------------------------------------------------------
1 Red Medium 5
2 Green Large 4

Once the user is done, this information is saved to a table. Let's also assume that this information was for January, 2010.

When the user starts a new month, (February, 2010, for example), their data could be exactly the same as January or it may change a little. If it IS the same or very close to the same, rather than force the user to reenter all of the information as they did in January, I'd like to be able to give them the option to "import" the data from January but still give them all the options that they had in January. What this means is that the Color ComboBoxColumn for each row still has Red, Blue & Green as its items that can be chosen, but for Row1, "Red" needs to be the selected item and for Row 2, "Green" needs to be the selected item. Keeping with that same idea, the Size ComboBox still has Small, Medium and Large as its items that can be selected for each row, but for Row 1, "Medium" needs to be set as the selected item and for Row 2, "Large" needs to be the selected item.

My DataGridView would be populated the exact same way as it did in January but the selected items that show would be the data that the user had chosen in January. Because I'm retrieving that data (what the user chose in Jan) from the database, I know that in the DataGridView cell for Row1, Color ComboBox, the selected item should be "Red". Since I know that, how do I set the selected item in that ComboBox to "Red"? If this was a straight ComboBox, I know how to set the SelectedItem (or SelectedIndex). But because this is a DataGridViewComboBox, it doesn't seem to work the same way.

Any ideas or suggestions will be greatly appreciated. If more information is needed in order to help me solve this, please let me know.

Thanks in advance.

Ron

Is This A Good Question/Topic? 0
  • +

Replies To: Set SelectedItem in DataGridViewComboBoxCell

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 10 March 2010 - 02:25 PM

Hi Ron,

Using combobox columns in datagridview can be quite complicated... I made a few simple test and here is what I came up with:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace GridViewExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitCustomComponents();
        }

        private void InitCustomComponents()
        {
            InitColors();
            InitSizes();
            InitGridView();
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn quantityColumn = new DataGridViewTextBoxColumn();
            quantityColumn.DataPropertyName = "Quantity";
            quantityColumn.Name = "Quantity";
            quantityColumn.HeaderText = "Quantity";

            DataGridViewComboBoxColumn colorColumn = new DataGridViewComboBoxColumn();
            colorColumn.DataPropertyName = "ItemColor";
            colorColumn.Name = "ItemColor";
            colorColumn.HeaderText = "ItemColor";
            colorColumn.DataSource = colors;
            colorColumn.DisplayMember = "Name";
            colorColumn.ValueMember = "This";

            DataGridViewComboBoxColumn sizesColumn = new DataGridViewComboBoxColumn();
            sizesColumn.DataPropertyName = "ItemSize";
            sizesColumn.Name = "ItemSize";
            sizesColumn.HeaderText = "ItemSize";
            sizesColumn.DataSource = sizes;
            sizesColumn.DisplayMember = "Name";
            sizesColumn.ValueMember = "This";

            myGridView.Columns.Add(quantityColumn);
            myGridView.Columns.Add(colorColumn);
            myGridView.Columns.Add(sizesColumn);
        }

        private void InitColors()
        {
            colors = new List<ItemColor>();
            colors.Add(new ItemColor(1, "Red", Color.Red));
            colors.Add(new ItemColor(2, "Green", Color.Green));
            colors.Add(new ItemColor(3, "Yellow", Color.Yellow));
        }

        private void InitSizes()
        {
            sizes = new List<ItemSize>();
            sizes.Add(new ItemSize(1, "Small", 1));
            sizes.Add(new ItemSize(2, "Medium", 5));
            sizes.Add(new ItemSize(3, "Large", 10));
        }

        private void btnSaveOrder_Click(object sender, EventArgs e)
        {
            orderedItems = new List<OrderItem>();

            foreach (DataGridViewRow row in myGridView.Rows)
            {
                if (!row.IsNewRow)
                {
                    OrderItem itemOrdered = new OrderItem();
                    itemOrdered.Quantity = Convert.ToInt32(row.Cells["Quantity"].Value);
                    itemOrdered.ItemColor = row.Cells["ItemColor"].Value as ItemColor;
                    itemOrdered.ItemSize = row.Cells["ItemSize"].Value as ItemSize;

                    orderedItems.Add(itemOrdered);
                }
            }
        }

        private void btnClearGridView_Click(object sender, EventArgs e)
        {
            myGridView.DataSource = null;
            myGridView.Rows.Clear();
            myGridView.Columns.Clear();
        }

        private void btnLoadSavedData_Click(object sender, EventArgs e)
        {
            if (myGridView.Columns.Count <= 0)
            {
                InitGridView();
            }

            myGridView.DataSource = orderedItems;
        }


        private IList<ItemSize> sizes;
        private IList<ItemColor> colors;
        private IList<OrderItem> orderedItems;
    }

    public class ItemColor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Color ColorValue { get; set; }
        public ItemColor This { get; private set; }

        public ItemColor() { }
        public ItemColor(int id, string name, Color value)
        {
            Id = id;
            Name = name;
            ColorValue = value;
            This = this;
        }
    }

    public class ItemSize
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int SizeValue { get; set; }
        public ItemSize This { get; private set; }

        public ItemSize() { }
        public ItemSize(int id, string name, int value)
        {
            Id = id;
            Name = name;
            SizeValue = value;
            This = this;
        }
    }

    public class OrderItem
    {
        public int Quantity { get; set; }
        public ItemColor ItemColor { get; set; }
        public ItemSize ItemSize { get; set; }

        public OrderItem() { }
        public OrderItem(int quantity, ItemColor color, ItemSize size)
        {
            Quantity = quantity;
            ItemColor = color;
            ItemSize = size;
        }
    }
}




I believe there might be a better solution to this and if any DIC member has more experience with DataGrids, I'll be happy to hear how to do this better.
Anyway, if you need any further assistance, just drop a post...
Was This Post Helpful? 0
  • +
  • -

#3 upndown4par  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 10

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 10 March 2010 - 03:24 PM

Hi Flash,

Thank you for the quick response. If I'm reading your code accurately, when you click btnLoadSavedData, it retrieves the orderedItems list and uses that as the DataSource for the DataGrid. If that's correct, then I don't think this is what I'm looking for. I need the DataSource for the ComboBoxColumns to remain the same (such as you did with InitColors() and InitSizes()) so that the user could change what's selected in the cell to something else. For example, if the user wanted to change the color in Row 1 to Blue from Red, I'd want Red to show up in the ComboBox as the SelectedItem but still have the other 2 choices available when he clicks on the down-arrow in the ComboBox.

The first screen shot shows what the DataGrid should look like when the user starts February. Instead of being the ComboBoxes being blank, as they would be normally, January's choices are shown.

The second screen shot shows the user clicking on the Color ComboBox in Row 1 so that he can make a change for February. All the same available choices are still in the ComboBox. What it boils down to is that the values in the DataGridView are being populated correctly. The only difference is that for each row, I need to be able to set the SelectedItem (or what shows in the ComboBoxCell) to a given string that I retrieve from the database. In this case, the ComboBoxCell in Row 1 needs to show "Red" but still have all of the other choices available to select if the user wants to make a change.

If I'm reading your code incorrectly, I apologize and wonder if you could elaborate on the code and how it sets the SelectedItem.

Thanks!

Ron

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 10 March 2010 - 03:39 PM

Actually you are reading it incorrectly... Datasource for combobox columns remains the same at all time...

In this example I wanted to show you this procedure:

1. When application starts, I initialize Colors and Sizes list and I add 3 columns in my grid view. Application looks like this:

Attached Image

2. Then I populate few rows just for a test and I click the SAVE button, which saves all the rows in my orderedItems list. Application looks like this:

Attached Image

3. Then I click CLEAR button, which removes data grid view data source if it is not null, and removes all the rows and columns from grid view to simulate application being started in February

Attached Image

4. Then I hit the LOAD button to simulate user opening data that was saved in January. Application looks like this:

Attached Image


The code I posted is just a sample code and would need a bit more tweeking, but I hope you get the point...
Was This Post Helpful? 0
  • +
  • -

#5 upndown4par  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-March 10

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 13 March 2010 - 05:04 PM

Flash,

Just to make sure I understand, once the Load button has been clicked to simulate February and the data has been loaded into the DataGrid, if the user clicks on one of the ComboBoxes, (let's say the Color ComboBox in Row 1, which says, "Green"), because you have set the DataSource to OrderedItems, "Green" would be the only choice available in that ComboBox, right?

Ron
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 15 March 2010 - 02:12 PM

No... Combobox should have all items available, but the Green color item selected.
Was This Post Helpful? 0
  • +
  • -

#7 Pieter001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-July 12

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 24 July 2012 - 10:46 AM

View PostFlashM, on 10 March 2010 - 02:25 PM, said:

Hi Ron,

Using combobox columns in datagridview can be quite complicated... I made a few simple test and here is what I came up with:


FlashM,
This is an old post, but your code helped me a lot. Your code works great by saving entries in the combobox that was entered and reloading using dataGridView1.DataSource = orderedItems; However, I want to load the datagridview with initial values. I went about doing it by replicating the object structure in orderedItems and then trying to load it with the new object. This does not work and I have now way of explaining it. I include a sightly modified version of your code. In particular, I have added a class InitialItems() that replicate the object that you create in buttonSave_Click(). I create an object of InitialItems() in your buttonload_Click() and then apply dataGridView1.DataSource = initialItems.OrderedItems; (lines 90 - 97 in the code) I have no idea why this gives an error, while saving and then loading an object (the way you do it), works. If you can look at this I would appreciate it very much. I include the code below (mostly what you did, with modifications as described.) Thanks for your help.

Attached Image

Attached Image

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xiApi.NET;

namespace DataGridViewEx
{
    public partial class Form1 : Form
    {

        private IList<Buff_Policy> bufPolicyPrms;
        private IList<Cms_Mode> cmsModePrms;
        private IList<OrderItem> orderedItems;

        public Form1()
        {
            InitializeComponent();
            InitCustomComponents();            
        }

        private void InitCustomComponents()
        {            
            BuffPolicyPrms();
            CmsModePrms();
            InitGridView();           
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn quantityColumn = new DataGridViewTextBoxColumn();
            quantityColumn.DataPropertyName = "Quantity";
            quantityColumn.Name = "Quantity";
            quantityColumn.HeaderText = "Quantity";            

            DataGridViewComboBoxColumn bpColumn = new DataGridViewComboBoxColumn();
            bpColumn.DataPropertyName = "Buff_Policy";
            bpColumn.Name = "Buff_Policy"; //Name to reference column
            bpColumn.HeaderText = "Buff_Policy";  //Name of column header
            bpColumn.DataSource = bufPolicyPrms;
            bpColumn.DisplayMember = "PrmName";
            bpColumn.ValueMember = "This";

            DataGridViewComboBoxColumn cmColumn = new DataGridViewComboBoxColumn();
            cmColumn.DataPropertyName = "Cms_Mode";
            cmColumn.Name = "Cms_Mode";
            cmColumn.HeaderText = "Cms_Mode";
            cmColumn.DataSource = cmsModePrms;
            cmColumn.DisplayMember = "PrmName";
            cmColumn.ValueMember = "This";

            dataGridView1.Columns.Add(quantityColumn);            
            dataGridView1.Columns.Add(bpColumn);
            dataGridView1.Columns.Add(cmColumn);

            //InitialItems initialItems = new InitialItems();
            

            //dataGridView1.DataSource = initialItems.OrderedItems;
        }       

        private void BuffPolicyPrms()
        {
            bufPolicyPrms = new List<Buff_Policy>();
            bufPolicyPrms.Add(new Buff_Policy(1, "Safe", BUFF_POLICY.SAFE));
            bufPolicyPrms.Add(new Buff_Policy(2, "Unsafe", BUFF_POLICY.UNSAFE));            
        }

        private void CmsModePrms()
        {
            cmsModePrms = new List<Cms_Mode>();
            cmsModePrms.Add(new Cms_Mode(1, "Disable", CMS_MODE.DIS));
            cmsModePrms.Add(new Cms_Mode(2, "Enable", CMS_MODE.EN));
            cmsModePrms.Add(new Cms_Mode(3, "Fast", CMS_MODE.EN_FAST));
        }       

        #region Events

        private void buttonload_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Columns.Count <= 0)
            {
                InitGridView();
            }
            
            //Original saved object that works
            //dataGridView1.DataSource = orderedItems;

            //Recreated object with same structure that does not work
            //Same as orderedItems above by choosing 1, "Safe", "Disable" in first row 
            //and 2, "Unsafe", "Enable" in second row
            InitialItems initialItems = new InitialItems();
            dataGridView1.DataSource = initialItems.OrderedItems;
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            orderedItems = new List<OrderItem>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    OrderItem itemOrdered = new OrderItem();
                    itemOrdered.Quantity = Convert.ToInt32(row.Cells["Quantity"].Value);                   
                    itemOrdered.Buff_Policy = row.Cells["Buff_Policy"].Value as Buff_Policy;
                    itemOrdered.Cms_Mode = row.Cells["Cms_Mode"].Value as Cms_Mode; //Save rows with name "Cms_Mode"
                    orderedItems.Add(itemOrdered);
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
        }

        #endregion
               
    }

    public class Buff_Policy
    {
        public int Id { get; set; }        
        public string PrmName { get; private set; }
        public int Prm { get; private set; }
        public Buff_Policy This { get; private set; }        

        public Buff_Policy() 
        {
        }

        public Buff_Policy(int id, string prmName, int prm)
        {
            this.Id = id;
            this.PrmName = prmName;
            this.Prm = prm;            
            This = this;
        }
    }

    public class Cms_Mode
    {
        public int Id { get; set; }
        public string PrmName { get; private set; }
        public int Prm { get; private set; }
        public Cms_Mode This { get; private set; }  

        public Cms_Mode()
        {
        }

        public Cms_Mode(int id, string prmName, int prm)
        {
            this.Id = id;
            this.PrmName = prmName;
            this.Prm = prm;
            This = this;
        }
    }    

    public class OrderItem
    {
        public int Quantity { get; set; }        
        public Buff_Policy Buff_Policy { get; set; }
        public Cms_Mode Cms_Mode { get; set; }

        public OrderItem() { }

        public OrderItem(int quantity, Buff_Policy bpPrm, Cms_Mode cmPrm)
        {
            Quantity = quantity;           
            Buff_Policy = bpPrm;
            Cms_Mode = cmPrm;
        }  
    }

    public class InitialItems
    {
         //public IList<OrderItem> OrderedItems {get; set; }
        //public List<OrderItem> OrderedItems = new List<OrderItem>();
        public IList<OrderItem> OrderedItems;
         //private OrderItem itemOrdered = new OrderItem();
         private string[] bp = { "Safe", "Unsafe" };
         private int[] bpParm = { 1, 0 };
         private string[] cm = { "Disable", "Enable" };
         private int[] cmParm = { 0, 1 };

        public InitialItems()
        {
            OrderedItems = new List<OrderItem>();

            for(int i = 0; i < 2; i++)
            {
                OrderItem itemOrdered = new OrderItem();
                itemOrdered.Quantity = i;
                itemOrdered.Buff_Policy = new Buff_Policy(i+1, bp[i], bpParm[i]);
                itemOrdered.Cms_Mode = new Cms_Mode(i+1, cm[i], cmParm[i]);
                OrderedItems.Add(itemOrdered);
            }
        }
    }

}



Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 24 July 2012 - 11:50 AM

Sorry for contributing to the necro post, but I feel compelled to answer.

The reason for the invalid value exceptions is because on lines 204-205 you are creating new instances, whereas lines 111-112 using existing instances.

Basically:
var x = new Buff_Policy(1, "Safe", BUFF_POLICY.SAFE);
var y = new Buff_Policy(1, "Safe", BUFF_POLICY.SAFE);
Debug.Assert(x != y);



(You maybe able to make them equal by overriding the Equals() and GetHashCode() methods and the other set of cascading overrides that you need to do based on.)
Was This Post Helpful? 0
  • +
  • -

#9 Pieter001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-July 12

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 24 July 2012 - 12:06 PM

View PostSkydiver, on 24 July 2012 - 11:50 AM, said:

Sorry for contributing to the necro post, but I feel compelled to answer.

The reason for the invalid value exceptions is because on lines 204-205 you are creating new instances, whereas lines 111-112 using existing instances.


Thank you for your quick reply - it makes sense. I still ahve the problem that I want to initialze the combobox column with values created externally and not from saved values. How would I do that? Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 24 July 2012 - 12:41 PM

Pull out references to the existing instances in bufPolicyPrms and cmsPolicyPrms instead of creating new instances.
Was This Post Helpful? 0
  • +
  • -

#11 Pieter001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-July 12

Re: Set SelectedItem in DataGridViewComboBoxCell

Posted 24 July 2012 - 09:23 PM

View PostSkydiver, on 24 July 2012 - 12:41 PM, said:

Pull out references to the existing instances in bufPolicyPrms and cmsPolicyPrms instead of creating new instances.


Thank you very much. Below, super inelegant code that shows that it works correctly as you suggested!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xiApi.NET;

namespace DataGridViewEx
{
    public partial class Form1 : Form
    {

        private IList<Buff_Policy> bufPolicyPrms;
        public IList<Cms_Mode> cmsModePrms { get; set; }
        public IList<OrderItem> orderedItems { get; set; }
        public int Quantity { get; set; }

        public Form1()
        {
            InitializeComponent();
            InitCustomComponents();            
        }

        private void InitCustomComponents()
        {            
            BuffPolicyPrms();
            CmsModePrms();
            InitGridView();           
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn quantityColumn = new DataGridViewTextBoxColumn();
            quantityColumn.DataPropertyName = "Quantity";
            quantityColumn.Name = "Quantity";
            quantityColumn.HeaderText = "Quantity";            

            DataGridViewComboBoxColumn bpColumn = new DataGridViewComboBoxColumn();
            bpColumn.DataPropertyName = "Buff_Policy";
            bpColumn.Name = "Buff_Policy"; //Name to reference column
            bpColumn.HeaderText = "Buff_Policy";  //Name of column header
            bpColumn.DataSource = bufPolicyPrms;
            bpColumn.DisplayMember = "PrmName";
            bpColumn.ValueMember = "This";

            DataGridViewComboBoxColumn cmColumn = new DataGridViewComboBoxColumn();
            cmColumn.DataPropertyName = "Cms_Mode";
            cmColumn.Name = "Cms_Mode";
            cmColumn.HeaderText = "Cms_Mode";
            cmColumn.DataSource = cmsModePrms;
            cmColumn.DisplayMember = "PrmName";
            cmColumn.ValueMember = "This";

            dataGridView1.Columns.Add(quantityColumn);            
            dataGridView1.Columns.Add(bpColumn);
            dataGridView1.Columns.Add(cmColumn);

            //InitialItems initialItems = new InitialItems();
            

            //dataGridView1.DataSource = initialItems.OrderedItems;
        }       

        private void BuffPolicyPrms()
        {
            Buff_Policy bpParams;
        
            bufPolicyPrms = new List<Buff_Policy>();
            bpParams = new Buff_Policy(1, "Safe", BUFF_POLICY.SAFE);
            bufPolicyPrms.Add(bpParams);
            bpParams = new Buff_Policy(2, "Unsafe", BUFF_POLICY.UNSAFE);
            bufPolicyPrms.Add(bpParams);            
        }

        private void CmsModePrms()
        {
            Cms_Mode cmParams;

            cmsModePrms = new List<Cms_Mode>();
            cmParams = new Cms_Mode(1, "Disable", CMS_MODE.DIS);
            cmsModePrms.Add(cmParams);
            cmParams = new Cms_Mode(2, "Enable", CMS_MODE.EN);
            cmsModePrms.Add(cmParams);
            cmParams = new Cms_Mode(3, "Fast", CMS_MODE.EN_FAST);
            cmsModePrms.Add(cmParams);
        }       

        #region Events

        private void buttonload_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Columns.Count <= 0)
            {
                InitGridView();
            }
            
            //Original saved object that works
            //dataGridView1.DataSource = orderedItems;

            //Recreated object with same structure that does not work
            //Same as orderedItems above by choosing 1, "Safe", "Disable" in first row 
            //and 2, "Unsafe", "Enable" in second row
            InitialItems initialItems = new InitialItems(Quantity, bufPolicyPrms, cmsModePrms);
            dataGridView1.DataSource = initialItems.OrderedItems;
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            orderedItems = new List<OrderItem>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    OrderItem itemOrdered = new OrderItem();
                    itemOrdered.Quantity = Convert.ToInt32(row.Cells["Quantity"].Value);                   
                    itemOrdered.Buff_Policy = row.Cells["Buff_Policy"].Value as Buff_Policy;
                    itemOrdered.Cms_Mode = row.Cells["Cms_Mode"].Value as Cms_Mode; //Save rows with name "Cms_Mode"
                    orderedItems.Add(itemOrdered);
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
        }

        #endregion
               
    }

    public class Buff_Policy
    {
        public int Id { get; set; }        
        public string PrmName { get; set; }
        public int Prm { get; set; }
        public Buff_Policy This { get; private set; }        

        public Buff_Policy() 
        {
        }

        public Buff_Policy(int id, string prmName, int prm)
        {
            this.Id = id;
            this.PrmName = prmName;
            this.Prm = prm;            
            This = this;
        }
    }

    public class Cms_Mode
    {
        public int Id { get; set; }
        public string PrmName { get; set; }
        public int Prm { get; set; }
        public Cms_Mode This { get; private set; }  

        public Cms_Mode()
        {
        }

        public Cms_Mode(int id, string prmName, int prm)
        {
            this.Id = id;
            this.PrmName = prmName;
            this.Prm = prm;
            This = this;
        }
    }    

    public class OrderItem
    {
        public int Quantity { get; set; }        
        public Buff_Policy Buff_Policy { get; set; }
        public Cms_Mode Cms_Mode { get; set; }

        public OrderItem() { }

        public OrderItem(int quantity, Buff_Policy bpPrm, Cms_Mode cmPrm)
        {
            Quantity = quantity;           
            Buff_Policy = bpPrm;
            Cms_Mode = cmPrm;
        }  
    }

    public class InitialItems
    {
         //public IList<OrderItem> OrderedItems {get; set; }
        //public List<OrderItem> OrderedItems = new List<OrderItem>();
        public IList<OrderItem> OrderedItems;
         //private OrderItem itemOrdered = new OrderItem();
         private string[] bpl = { "Unsafe", "Safe" };
         private int[] bpParm = { 1, 0 };
         private string[] cml = { "Disable", "Fast" };
         private int[] cmParm = { 0, 1 };

        public InitialItems(int quantity, IList<Buff_Policy> bp, IList<Cms_Mode> cm)
        {
            OrderedItems = new List<OrderItem>();

            for(int i = 0; i < 2; i++)
            {
                OrderItem itemOrdered = new OrderItem();

                quantity = cmParm[i];
                itemOrdered.Quantity = quantity;
                
                bp[i].PrmName = bpl[i];
                bp[i].Prm = bpParm[i];
                itemOrdered.Buff_Policy = bp[i];

                cm[i].PrmName = cml[i];
                cm[i].Prm = cmParm[i];
                itemOrdered.Cms_Mode = cm[i];
                OrderedItems.Add(itemOrdered);
            }
        }
    }

}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1