columns.Clear不起作用

时间:2011-02-08 22:16:12

标签: c# winforms datagridview

这是我的UserDynamicSetsControl的代码:

public ReportData ReportData
        {
            get { return reportData; }
            set
            {                               
                reportData = value;

                this.bsDynasets.DataSource = reportData;             

            }
        }

 public TypeOfBuilding TypeOfBuilding
        {
            get { return _typeOfBuilding; }
            set
            {
                _typeOfBuilding= value;

                dataGridView1.Columns.Clear();



                if (this.TypeOfBuilding== TypeOfBuilding.House)
                {
                    CreateColumnsForHouses();

                }
                else if (this.TypeOfBuilding== TypeOfBuilding.Flat)
                {
                    CreateColumnsForFlats();

                }

                if (this.bsDynasets.DataSource != null)
                    this.bsDynasets.Filter = "TypeOfBuilding= " + (int)_typeOfBuilding;


            }
        }




public voidCreateColumnsForFlats()
{
 this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ColumnsA,ColumnB};
}

public voidCreateColumnsForFlats()
{
 this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ColumnsC,ColumnD};
}

首先设置ReportData,然后设置TypeOfBuilding。看来,在我更改TypeOfBuilding后,所有列都将清除,并且将添加一个新列。相反,它保留旧列并添加新列。

执行后:

 this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ColumnsC,ColumnD};

有两列。

致电

之前
  dataGridView1.Columns.Clear();

它显示正确的列数(即4,6,8等)。我在哪里弄错了?

1 个答案:

答案 0 :(得分:2)

false上将.AutoGenerateColumns设置为dataGridView1