【问题标题】:Datagirdview and tabcontrol issueDatagridview 和选项卡控件问题
【发布时间】:2013-07-28 13:18:19
【问题描述】:

我在我的 winforms .net 4 应用程序中遇到了一个奇怪的行为。我有一个带有两个标签页的 tabcontrol 的表单,用户在 tabpage1 上选择数据并单击 GO 按钮,有一个 dataGridView 控件绑定到用户选择的结果(数据表)。设置 datagridview 的数据源后,我在网格数据源的顶部(0 索引)添加一行,然后在该行(datagirdview.rows[0])上应用一些格式。

我可以看到我的格式应用于调试器中的行,但是一旦选项卡选择代码运行,我的行格式(isFrozen、BackColor 等)就消失了。

当我先选择标签页,然后绑定网格和格式化的设置数据源时,它工作正常。

只有新添加的行丢失格式,我有一个类似的应用程序,我正在添加这样的行,但它工作正常,在当前应用程序中我使用 backgroundWorker 并从 RunWorkerCompleted 运行此代码,而在以前我没有使用 backGroundWorker 的应用程序。

下面是代码

 void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!e.Cancelled && e.Error == null)
        {
            if (((DataTable)e.Result).Rows.Count > 0)
            {

                //tabControl1.SelectTab(tabPage2); if I call from here then row formatting retains
                grdDistProcessing.DataSource = ((DataTable)e.Result);
                formatGrid();
                loadStoresGrid();
                AddTotalsRowInEnd();
                SetTotalsOfTotalRow();
                tabControl1.SelectTab(tabPage2);
            }
        }

        this.tsStatus.Text = string.Empty;
    }

这里是 AddTotalsRowInEnd 方法:

 private void AddTotalsRowInEnd()
    {
        Font f = new System.Drawing.Font("Arial", 8, FontStyle.Bold);
        DataRow dr = ((DataTable)grdDistProcessing.DataSource).NewRow();
        dr.ItemArray = ((DataTable)grdDistProcessing.DataSource).Rows[0].ItemArray;
        dr["Itemlookupcode"] = "Grand Totals";
        dr["Size"] = "";
        dr["COLORS"] = "";
        dr["DESCRIPTIONS"] = "";

        ((DataTable)grdDistProcessing.DataSource).Rows.InsertAt(dr, 0);
        grdDistProcessing.Rows[0].Frozen = true;
        grdDistProcessing.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood;
        grdDistProcessing.Rows[0].DefaultCellStyle.ForeColor = Color.Black;
        grdDistProcessing.Rows[0].DefaultCellStyle.Font = f;
        grdDistProcessing.Rows[0].ReadOnly = true;
        grdDistProcessing.Refresh();
    }

这是我的工作:

void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            BackgroundWorker bWorkder = sender as BackgroundWorker;
            DistVariablesTransfer dtr = e.Argument as DistVariablesTransfer;
            bWorkder.ReportProgress(10);
            cProcess pro = new cProcess();
            e.Result = pro.loadDistribution(dtr.pWarehouseID, dtr.pStores, dtr.pStyle, dtr.pColor, dtr.pSize, dtr.pDateFrom, dtr.pDateTo, dtr.pIncOrdQtyForSrc, dtr.PCheckDestinationTranferOut);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

【问题讨论】:

  • 我也有同样的问题。在我的应用程序中,我有五个选项卡。应用程序中的前四个选项卡中的每一个都包含数据网格视图。前三个工作完美无缺。但是,第四个选项卡中的 datagridview 存在与上述相同的问题。第一次加载它缺少设置的背景颜色,但在所有后续加载中都会显示格式。打开双缓冲没有帮助。如果我找到解决方案,将在此处发布。

标签: c# winforms .net-4.0 datagridview


【解决方案1】:

而不是做:

grdDistProcessing.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood;
grdDistProcessing.Rows[0].DefaultCellStyle.ForeColor = Color.Black;

对 grdDistProcessing 使用 CellFormatting 事件(显示类别),如下所示:

private void grdDistProcessing_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   e.CellStyle.BackColor = Color.BurlyWood;
   e.CellStyle.ForeColor = Color.Black;

}

它的渲染速度也应该更快。

【讨论】:

  • 两年了,我换了公司,但那个应用程序仍在生产中:) 我不得不先选择标签,因为当时我没有找到任何解决方案。
猜你喜欢
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多