更改数据源后,数据表列会出现故障

时间:2010-04-08 16:41:28

标签: c# datagridview

这是一个古怪的问题所以我会尽力描述最好的问题。 我有一个DataGridView,显示合同清单和有关它们的各种信息。有三种视图模式:合同批准,预生产和生产。每种模式都有自己的一组需要显示的列。

我一直在做的是每个合约款式都有三个单选按钮。他们所有人都在这个功能上更改了他们的支票

private void rbContracts_CheckedChanged(object sender, EventArgs e)
{
    dgvContracts.Columns.Clear();
    if (((RadioButton)sender).Checked == true)
    {
        if (sender == rbPreProduction)
        {
            dgvContracts.Columns.AddRange(searchSettings.GetPreProductionColumns());
            this.contractsBindingSource.DataMember = "Preproduction";
            this.preproductionTableAdapter.Fill(this.searchDialogDataSet.Preproduction);
        }
        else if (sender == rbProduction)
        {
            dgvContracts.Columns.AddRange(searchSettings.GetProductionColumns());
            this.contractsBindingSource.DataMember = "Production";
            this.productionTableAdapter.Fill(this.searchDialogDataSet.Production);

        }
        else if (sender == rbContracts)
        {
            dgvContracts.Columns.AddRange(searchSettings.GetContractsColumns());
            this.contractsBindingSource.DataMember = "Contracts";
            this.contractsTableAdapter.Fill(this.searchDialogDataSet.Contracts);
        }
    }
}

这是GetxxxColumns函数

public DataGridViewColumn[] GetPreProductionColumns()
{
    this.dgvTxtPreAccount.Visible = DgvTxtPreAccountVisable;
    this.dgvTxtPreImpromedAccNum.Visible = DgvTxtPreImpromedAccNumVisable;
    this.dgvTxtPreCreateDate.Visible = DgvTxtPreCreateDateVisable;
    this.dgvTxtPreCurrentSoftware.Visible = DgvTxtPreCurrentSoftwareVisable;
    this.dgvTxtPreConversionRequired.Visible = DgvTxtPreConversionRequiredVisable;
    this.dgvTxtPreConversionLevel.Visible = DgvTxtPreConversionLevelVisable;
    this.dgvTxtPreProgrammer.Visible = DgvTxtPreProgrammerVisable;
    this.dgvCbxPreEdge.Visible = DgvCbxPreEdgeVisable;
    this.dgvCbxPreEducationRequired.Visible = DgvCbxPreEducationRequiredVisable;
    this.dgvTxtPreTargetMonth.Visible = DgvTxtPreTargetMonthVisable;
    this.dgvCbxPreEdgeDatesDate.Visible = DgvCbxPreEdgeDatesDateVisable;
    this.dgvTxtPreStartDate.Visible = DgvTxtPreStartDateVisable;
    this.dgvTxtPreUserName.Visible = DgvTxtPreUserNameVisable;
    this.dgvCbxPreProductionId.Visible = DgvCbxPreProductionIdVisable;
    return new System.Windows.Forms.DataGridViewColumn[] {
                                                this.dgvTxtPreAccount,
                                                this.dgvTxtPreImpromedAccNum,
                                                this.dgvTxtPreCreateDate,
                                                this.dgvTxtPreCurrentSoftware,
                                                this.dgvTxtPreConversionRequired,
                                                this.dgvTxtPreConversionLevel,
                                                this.dgvTxtPreProgrammer,
                                                this.dgvCbxPreEdge,
                                                this.dgvCbxPreEducationRequired,
                                                this.dgvTxtPreTargetMonth,
                                                this.dgvCbxPreEdgeDatesDate,
                                                this.dgvTxtPreStartDate,
                                                this.dgvTxtPreUserName,
                                                this.dgvCbxPreProductionId,
                                                this.dgvTxtCmnHold,
                                                this.dgvTxtCmnConcern,
                                                this.dgvTxtCmnAccuracyStatus,
                                                this.dgvTxtCmnEconomicStatus,
                                                this.dgvTxtCmnSoftwareStatus,
                                                this.dgvTxtCmnServiceStatus,
                                                this.dgvTxtCmnHardwareStatus,
                                                this.dgvTxtCmnAncillaryStatus,
                                                this.dgvTxtCmnFlowStatus,
                                                this.dgvTxtCmnImpromedAccountNum,
                                                this.dgvTxtCmnOpportunityId};
}
public DataGridViewColumn[] GetProductionColumns()
{
    this.dgvcTxtProAccount.Visible = DgvTxtProAccountVisable;
    this.dgvTxtProImpromedAccNum.Visible = DgvTxtProImpromedAccNumVisable;
    this.dgvTxtProCreateDate.Visible = DgvTxtProCreateDateVisable;
    this.dgvTxtProConvRequired.Visible = DgvTxtProConvRequiredVisable;
    this.dgvTxtProEdgeRequired.Visible = DgvTxtProEdgeRequiredVisable;
    this.dgvTxtProStartDate.Visible = DgvTxtProStartDateVisable;
    this.dgvTxtProHardwareRequired.Visible = DgvTxtProHardwareReqiredVisable;
    this.dgvTxtProStandardDate.Visible = DgvTxtProStandardDateVisable;
    this.dgvTxtProSystemScheduleDate.Visible = DgvTxtProSystemScheduleDateVisable;
    this.dgvTxtProHwSystemCompleteDate.Visible = DgvTxtProHwSystemCompleteDateVisable;
    this.dgvTxtProHardwareTechnician.Visible = DgvTxtProHardwareTechnicianVisable;
    return new System.Windows.Forms.DataGridViewColumn[] {
                                    this.dgvcTxtProAccount,
                                    this.dgvTxtProImpromedAccNum,
                                    this.dgvTxtProCreateDate,
                                    this.dgvTxtProConvRequired,
                                    this.dgvTxtProEdgeRequired,
                                    this.dgvTxtProStartDate,
                                    this.dgvTxtProHardwareRequired,
                                    this.dgvTxtProStandardDate,
                                    this.dgvTxtProSystemScheduleDate,
                                    this.dgvTxtProHwSystemCompleteDate,
                                    this.dgvTxtProHardwareTechnician,
                                    this.dgvTxtCmnHold,
                                    this.dgvTxtCmnConcern,
                                    this.dgvTxtCmnAccuracyStatus,
                                    this.dgvTxtCmnEconomicStatus,
                                    this.dgvTxtCmnSoftwareStatus,
                                    this.dgvTxtCmnServiceStatus,
                                    this.dgvTxtCmnHardwareStatus,
                                    this.dgvTxtCmnAncillaryStatus,
                                    this.dgvTxtCmnFlowStatus,
                                    this.dgvTxtCmnImpromedAccountNum,
                                    this.dgvTxtCmnOpportunityId};
}
public DataGridViewColumn[] GetContractsColumns()
{
    this.dgvTxtConAccount.Visible = this.DgvTxtConAccountVisable;
    this.dgvTxtConAccuracyStatus.Visible = this.DgvTxtConAccuracyStatusVisable;
    this.dgvTxtConCreateDate.Visible = this.DgvTxtConCreateDateVisable;
    this.dgvTxtConEconomicStatus.Visible = this.DgvTxtConEconomicStatusVisable;
    this.dgvTxtConHardwareStatus.Visible = this.DgvTxtConHardwareStatusVisable;
    this.dgvTxtConImpromedAccNum.Visible = this.DgvTxtConImpromedAccNumVisable;
    this.dgvTxtConServiceStatus.Visible = this.DgvTxtConServiceStatusVisable;
    this.dgvTxtConSoftwareStatus.Visible = this.DgvTxtConSoftwareStatusVisable;
    this.dgvCbxConPreProductionId.Visible = this.DgvCbxConPreProductionIdVisable;
    this.dgvCbxConProductionId.Visible = this.DgvCbxConProductionVisable;
    return new System.Windows.Forms.DataGridViewColumn[] {
                                    this.dgvTxtConAccount,
                                    this.dgvTxtConImpromedAccNum,
                                    this.dgvTxtConCreateDate,
                                    this.dgvTxtConAccuracyStatus,
                                    this.dgvTxtConEconomicStatus,
                                    this.dgvTxtConSoftwareStatus,
                                    this.dgvTxtConServiceStatus,
                                    this.dgvTxtConHardwareStatus,
                                    this.dgvCbxConPreProductionId,
                                    this.dgvCbxConProductionId,
                                    this.dgvTxtCmnHold,
                                    this.dgvTxtCmnConcern,
                                    this.dgvTxtCmnAccuracyStatus,
                                    this.dgvTxtCmnEconomicStatus,
                                    this.dgvTxtCmnSoftwareStatus,
                                    this.dgvTxtCmnServiceStatus,
                                    this.dgvTxtCmnHardwareStatus,
                                    this.dgvTxtCmnAncillaryStatus,
                                    this.dgvTxtCmnFlowStatus,
                                    this.dgvTxtCmnImpromedAccountNum,
                                    this.dgvTxtCmnOpportunityId};
}

问题是当我第一次检查按钮时,一切都显示正常。我选择了另一种观点,一切都很好。但是当我点击第一个视图时,列是乱序的(就像它们的顺序相反,但它们并不完全相同)。这只发生在您点击的第一页,其他两个都很好。您可以在这些初始步骤之后单击关闭并再次单击您想要的次数,您在开始时选择的第一个列表将无序,其他两个将是正确的。

关于可能导致这种情况的任何想法?

EDIT-- 到目前为止我发现的事情: 当我第二次查看第一个选项时,ColumnDisplayIndexChanged会多次(超过200次)发生。如果函数什么都不做,它仍会加载页面,如果我放置一个对话框来显示它被触发(这是很多点击)最终我要么在数据网格视图区域得到一个大的红色X或者它加载正常(取决于页面,我得到一个X用于预生产但其他两个加载正常(消息框仍然显示数百次),当你先选择它们时)

2 个答案:

答案 0 :(得分:0)

我最好的猜测是,如果在返回列范围创建函数后发生了更改,则.XXX.Fill正在更改DisplayIndex值。但是你可以考虑一些事情。

  1. 创建列的范围一次,而不是每次选择不同的视图。
  2. 记忆是个问题吗?如果数据集不大并且将来不应该很大,您可以填充3个单独的容器并将绑定更改为不同的容器,而不是每次都重新填充单个容器。
  3. 我认为我至少只会创建一次列范围而不是每次。

    修改

    private DataGridViewColumns[] PreProducitonColumns {get;set;}
    private DataGridViewColumns[] ProductionColumns {get;set;}
    private DataGridViewColumns[] ContractsColumns {get;set;}
    
    private void Form_Load()
    {
        this.PreProducitonColumns = searchSettings.GetPreProductionColumns();
        this.ProductionColumns = searchSettings.GetProductionColumns();
        this.ContractsColumns = searchSettings.GetContractsColumns();
    }
    
    private void rbContracts_CheckedChanged(object sender, EventArgs e) 
    { 
        dgvContracts.Columns.Clear(); 
        if (((RadioButton)sender).Checked == true) 
        { 
            if (sender == rbPreProduction) 
            { 
                dgvContracts.Columns.AddRange(PreProducitonColumns); 
                this.contractsBindingSource.DataMember = "Preproduction"; 
                this.preproductionTableAdapter.Fill(this.searchDialogDataSet.Preproduction); 
            } 
            else if (sender == rbProduction) 
            { 
                dgvContracts.Columns.AddRange(ProductionColumns); 
                this.contractsBindingSource.DataMember = "Production"; 
                this.productionTableAdapter.Fill(this.searchDialogDataSet.Production); 
    
            } 
            else if (sender == rbContracts) 
            { 
                dgvContracts.Columns.AddRange(ContractsColumns); 
                this.contractsBindingSource.DataMember = "Contracts"; 
                this.contractsTableAdapter.Fill(this.searchDialogDataSet.Contracts); 
            } 
        } 
    } 
    

答案 1 :(得分:0)

我采取了简单的方法。我刚创建了3个DataGridView,并根据单选按钮将它们设置为可见。