有两个窗体,一个类,窗体里面是propertyGrid控件,我在第一个窗体里面设置了propertyGrid控件的值,然后到第二个窗体的时候值又空了。
看代码。
Customer类: class Customer
    {
        private string _name;
        private int _age;
        private DateTime _dateOfBirth;
        private string _SSN;
        private string _address;
        private string _email;
        private bool _frequentBuyer;        [CategoryAttribute("ID Settings"), DescriptionAttribute("客户名")]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Social Security Number of the customer")]        public string SSN
        {
            get
            {
                return _SSN;
            }
            set
            {
                _SSN = value;
            }
        }
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Address of the customer")]
        public string Address
        {
            get
            {
                return _address;
            }
            set
            {
                _address = value;
            }
        }
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Date of Birth of the Customer (optional)")]
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }
        [CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")]
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
        [CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer has bought more than 10 times, this is set to true")]
        public bool FrequentBuyer
        {
            get { return _frequentBuyer; }
            set { _frequentBuyer = value; }
        }
        [CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }
        public Customer() { }Form1:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Customer bill = new Customer();        private void Form1_Load(object sender, EventArgs e)
        {
           
            propertyGrid1.SelectedObject = bill;
        }        private void button1_Click(object sender, EventArgs e)
        {
            bill.Age = int.Parse(textBox1.Text);
            
            //bill.Age = 50;
            bill.Address = "autocenter";
            bill.DateOfBirth = Convert.ToDateTime("1987-07-02");
            bill.SSN = "123-456-77988";
            bill.Email = "[email protected]";
            bill.Name="js";
            propertyGrid1.SelectedObject = bill;
            
        }        private void button2_Click(object sender, EventArgs e)
        {
            bill.Age = int.Parse(textBox1.Text);
            bill.Age = 50;
            bill.Address = "autocenter";
            bill.DateOfBirth = Convert.ToDateTime("1987-07-02");
            bill.SSN = "123-456-77988";
            bill.Email = "[email protected]";
            bill.Name = "js";
            Form2 f2 = new Form2();
            f2.ShowDialog();
            bill.Age = int.Parse(textBox1.Text);
        }
    }
}Form2:        public Form2()
        {
            InitializeComponent();
        }
        Customer bill2 = new Customer();        private void Form2_Load(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = bill2;
            label1.Text = bill2.Age.ToString();
        }
    }
}

解决方案 »

  1.   


        public static ReportType getType(string name)
        {
            ReportType type = new ReportType();
            if (name!= null)
            {
                SqlConnection conn = new SqlConnection(Constant.ScheduleReport_CONNSTR);
                string sqlStr = Constant.DBType_Select;
                SqlParameter[] paramter = {
                                           new SqlParameter( "@NAME",SqlDbType.VarChar)
                                         };
                paramter[0].Value = name;
                using ( SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr,paramter))
                {
                    if (dr.Read())
                    {
                        type.DB_TYPE = dr["DB_TYPE"].ToString();
                        type.SERVER = dr["SERVER"].ToString();
                        type.QUERY_SQL = dr["QUERY_SQL"].ToString();
                        type.DELETE_SQL = dr["DELETE_SQL"].ToString();
                        type.PENDING_SQL = dr["PENDING_SQL"].ToString();
                        type.RESUME_SQL = dr["RESUME_SQL"].ToString();
                        type.DB_NAME = dr["DB_NAME"].ToString();
                    }            }
           }
           return type;    }
    }    public class ReportType 
        {
            public ReportType()
            {}
          private string _db_type =string.Empty;
          private string  _server= string.Empty;
          private string _query_sql=string.Empty;
          private string _delete_sql=string.Empty;
          private string _pending_sql = string.Empty;
          private string __resume_sql = string.Empty;
           private string _db_name = string.Empty;        public string DB_TYPE 
            {
                get { return _db_type; }
                set { _db_type = value; }
            }
            public string SERVER
            {
                get { return _server; }
                set { _server = value; }
            }
            public string QUERY_SQL
            {
                get { return _query_sql; }
                set { _query_sql = value; }
            }
            public string DELETE_SQL
            {
                get { return _delete_sql; }
                set { _delete_sql = value; }
            }
            public string PENDING_SQL
            {
                get { return _pending_sql; }
                set { _pending_sql = value; }
            }
            public string RESUME_SQL
            {
                get { return __resume_sql; }
                set { __resume_sql = value; }
            }
            public string DB_NAME
            {
                get { return _db_name; }
                set { _db_name = value; }
            }    }
      

  2.   

    Form2:        public Form2(Customer p_Bill) 
            { 
                InitializeComponent(); if(p_Bill==null)
    {
    bill2=new Customer();
    }
    else
    {
    bill2=p_Bill
    }
                        } 
            Customer bill2 ;
    Form1里修改
    Form2 f2 = new Form2(bill);