建立了一个project:  temp,添加了一个datagridview1
添加了一个类: design.cs打算:在design.cs里设置datagridview1的表格样式(行数、列数、列宽、行高......)namespace temp
{
    class design
    {
       (该怎么写呢?)   
    }
}

解决方案 »

  1.   

    datagridview1的modifer设置成public将datagridview1所在的form实例传给designdatagridview1所在类class Form1
    {
        void somefunc()
        {
            design d = new design(this);
            .....
        }
    }
    class design
    {
        Form1 f1;
        public design(Form1 f)
       {
           f1 = f;
       }   void func()
       {
          f1.datagridview1.xxxx = xxxx;
       }
    }
      

  2.   

    Form1.cs的代码:namespace temp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                 这里该怎么写呢?
            }
        }
    }
    Design.cs的代码:
    namespace temp
    {
        class design
        {
            这里该怎么写呢?
        }
    }
      

  3.   

            DataGridView dgv;        private void Form1_Load(object sender, EventArgs e)
            {
                dgv  = new DataGridView();
                dgv.Location = new Point(0, 0);
                dgv.Size = new Size(300, 200);          
                this.Controls.Add(dgv);
                Design d = new Design(this.dgv);
            }    class Design
        {
            DataGridView  dgv = null;
            public Design(DataGridView _dgv)
            {
                dgv = _dgv;
                func();
            }        public void func()
            {
                if (dgv == null)
                    return;
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
            }
        }
      

  4.   

        class Design
        {
            DataGridView  dgv = null;
            public Design(DataGridView _dgv)
            {
                dgv = _dgv;
                func();
            }        public void func()
            {
                if (dgv == null)
                    return;
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
                dgv.Columns.Add("1", "1");
            }
        }
      

  5.   

    如何用 radiobutton_checkedchanged事件来 调用类呢?
      

  6.   

    class Design
      {
      DataGridView dgv = null;
      public Design(DataGridView _dgv)
      {
      dgv = _dgv;
      func();
      }  public void func()
      {
      if (dgv == null)
      return;
      dgv.Columns.Add("1", "1");
      dgv.Columns.Add("1", "1");
      dgv.Columns.Add("1", "1");
      dgv.Columns.Add("1", "1");
      }
      

  7.   

    Form1.cs的代码:namespace class_demo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }                private void Form1_Load(object sender, EventArgs e)
            {            
            }        private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton1.Checked)
                {                
                    class_demo.Style.GridStyle.GridStyle_01();
                }
                else
                {
                    flex.Clear();
                }
            }        private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton2.Checked)
                {
                    class_demo.Style.GridStyle.GridStyle_02();
                }
                else
                {
                    flex.Clear();
                }
            }        private void radioButton3_CheckedChanged(object sender, EventArgs e)
            {
                if (radioButton3.Checked)
                {
                    class_demo.Style.GridStyle.GridStyle_03();
                }
                else
                {
                    flex.Clear();
                }
            }
        }
    }
    GridStyle.cs的代码:
    namespace class_demo.Style
    {
        class GridStyle
        {
            public static Form1 f = new Form1();
            public static void GridStyle_01()
            {            f.flex.Cols.Count = 5;
                f.flex.Rows.Count = 5;
                f.flex.Cols.DefaultSize = 30;
                f.flex.Rows.DefaultSize = 30;
                CellRange rng = f.flex.GetCellRange(0, 0, 0, 4);
                rng.StyleNew.BackColor = Color.Red;        }
            public static void GridStyle_02()
            {
                f.flex.Cols.Count = 7;
                f.flex.Rows.Count = 7;
                f.flex.Cols.DefaultSize = 30;
                f.flex.Rows.DefaultSize = 30;
                CellRange rng = f.flex.GetCellRange(1, 0, 1, 6);
                rng.StyleNew.BackColor = Color.Blue;
            }
            public static void GridStyle_03()
            {
                f.flex.Cols.Count = 9;
                f.flex.Rows.Count = 9;
                f.flex.Cols.DefaultSize = 30;
                f.flex.Rows.DefaultSize = 30;
                CellRange rng = f.flex.GetCellRange(2, 0, 2, 8);
                rng.StyleNew.BackColor = Color.Green;
            }
        }
    }
    哪里错了呢?
      

  8.   

    我碉堡了,楼上的啊,你连最简单的对象知识点都搞不懂啊
    GridStyle里定义一个Form1 f
    然后又在外面CheckedChanged里调用class_demo.Style.GridStyle.GridStyle_01();
    这里面涉及到两个Form1对象了,一个是f,一个CheckedChanged所在的对象晕,说得我也搞不清了
      

  9.   

    【Form1.cs 的代码】 已将名为flex的表格的modifiers设置成Publicnamespace grid_class
    {
       public partial class Form1 : Form
       {
       public Form1()
       {
       InitializeComponent();
       }   private void Form1_Load(object sender, EventArgs e)
       {   }   private void radioButton1_CheckedChanged(object sender, EventArgs e)
       {
       if (radioButton1.Checked)
       {
       style.GridStyle_01();
       }
       else
       {
       flex.Clear();
       }
       }   private void radioButton2_CheckedChanged(object sender, EventArgs e)
       {
       if (radioButton2.Checked)
       {
       style.GridStyle_02();
       }
       else
       {
       flex.Clear();
       }
       }   private void radioButton3_CheckedChanged(object sender, EventArgs e)
       {
       if (radioButton3.Checked)
       {
       style.GridStyle_03();
       }
       else
       {
       flex.Clear();
       }
       }   
      }
    }【添加了一个style.cs】
    namespace grid_class
    {
       class style
       {
       public static Form1 f = new Form1();   public static void GridStyle_01()
       {   f.flex.Cols.Count = 5;
       f.flex.Rows.Count = 5;
       f.flex.Cols.DefaultSize = 30;
       f.flex.Rows.DefaultSize = 30;
       CellRange rng = f.flex.GetCellRange(0, 0, 0, 4);
       rng.StyleNew.BackColor = Color.Red;   }
       public static void GridStyle_02()
       {
       f.flex.Cols.Count = 7;
       f.flex.Rows.Count = 7;
       f.flex.Cols.DefaultSize = 30;
       f.flex.Rows.DefaultSize = 30;
       CellRange rng = f.flex.GetCellRange(1, 0, 1, 6);
       rng.StyleNew.BackColor = Color.Blue;
       }
       public static void GridStyle_03()
       {
       f.flex.Cols.Count = 9;
       f.flex.Rows.Count = 9;
       f.flex.Cols.DefaultSize = 30;
       f.flex.Rows.DefaultSize = 30;
       CellRange rng = f.flex.GetCellRange(2, 0, 2, 8);
       rng.StyleNew.BackColor = Color.Green;
       }
       }
    }
    但是么有效果,错在哪里呢?