Datagirdview and tabcontrol issue

I am experiencing a strange behavior in my winforms .net 4 application. I have a form with a tabcontrol having two tabpages, user selects data on tabpage1 and clicks GO button, there is a dataGridView control which is bound to a results of user selection (a datatable). After I set datasource of datagridview, I am adding a row at top(0 index) of my grid's datasource, then I am applying some formatting on that row (datagirdview.rows[0]).

I can see my formatting applied to the row in debugger, but as soon as tab selection code runs, my row formatting(isFrozen, BackColor etc) is gone.

When I selects the tabpage first and bind set datasource of gird and formatting afterwards, it works fine.

only newly added row is loosing formatting, I have a similar application in which I am adding a row like this, but it is working fine, in current application I am using a backgroundWorker and running this code from RunWorkerCompleted, while in previous application I am not using backGroundWorker.

Below is the code

 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;
    }

Here is AddTotalsRowInEnd Method:

 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();
    }

and Here is my DoWork:

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);
        }

    }

Answers


Instead of doing:

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

Use the CellFormatting event (Display category) for grdDistProcessing, like this:

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

}

It should render faster too.


Need Your Help

Group a data in Unix shell or Group by in Oracle

sql oracle shell unix

What is the best and the most optimized way to group by data on multiple columns ?