Pieter001's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
3 (0 per day)
Joined:
24-July 12
Profile Views:
205
Last Active:
User is offline Jul 24 2012 09:18 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   Pieter001 has not set their status

Posts I've Made

  1. In Topic: Set SelectedItem in DataGridViewComboBoxCell

    Posted 24 Jul 2012

    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);
                }
            }
        }
    
    }
    
    
    
    
  2. In Topic: Set SelectedItem in DataGridViewComboBoxCell

    Posted 24 Jul 2012

    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.
  3. In Topic: Set SelectedItem in DataGridViewComboBoxCell

    Posted 24 Jul 2012

    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 File  Untitled-2.jpg (74.04K)
    Number of downloads: 12

    Attached File  Untitled-1.jpg (44.08K)
    Number of downloads: 10

    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);
                }
            }
        }
    
    }
    
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

Pieter001 hasn't added any friends yet.

Comments

Pieter001 has no profile comments yet. Why not say hello?