Adding Existing data to a new datatable with more data

354 Views Asked by At

lastbudget and lastactual are both Lists

These lists populate 2 columns in an infragistics ultragrid

  • PrevFY
  • Actual_c

So the grid has defined datasource already. These 2 columns I added programmaticly onto the grid which has a datasource.

(dgvBudget is the name of the ultragrid)

int rowIndex = dgvBudget.Rows.Count;


if (half == "Second Half Budget")
{
    for (int i = 6; i < rowIndex; i++)
    {
        var row = dgvBudget.Rows[i];
        row.Cells["PrevFY"].Value = lastbudget[i];
        row.Cells["Actual_c"].Value = lastactual[i];
    }
    for (int i = 0; i < 6; i++)
    {
        var row = dgvBudget.Rows[i];
        row.Cells["PrevFY"].Value = lastactual[i];
        row.Cells["Actual_c"].Value = 0;
    }
}
else
{
    for (int i = 0; i < rowIndex; i++)
    {
        var row = dgvBudget.Rows[i];
        row.Cells["PrevFY"].Value = lastbudget[i];
        row.Cells["Actual_c"].Value = lastactual[i];
    }
}

My goal is to create a new datasource and import the binded data to the the datagridview with the 3 new columns I have created to cut down on MS when executing.

At the moment it is taking 8-10 seconds to execute

private void bringPreviousData()
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    List<string> lastactual = new List<string>();
    List<string> lastbudget = new List<string>();
    Control cmbBudgetCode = csm.GetNativeControlReference("17dd127e-7b02-48e9-a7bb-e98164aea713");
    EpiDataView bhView = (EpiDataView)(oTrans.EpiDataViews["GLBudgetHd"]);
    if (!dgvBudget.DisplayLayout.Bands[0].Columns.Exists("PrevFY"))
    {
        dgvBudget.DisplayLayout.Bands[0].Columns.Add("PrevFY", "Previous FY Budgeted");
        dgvBudget.DisplayLayout.Bands[0].Columns["PrevFY"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Currency;
    }

    if (!dgvBudget.DisplayLayout.Bands[0].Columns.Exists("FiscalMonth"))
    {
        dgvBudget.DisplayLayout.Bands[0].Columns.Add("FiscalMonth", "Month");
        SetColumnReadOnly("FiscalMonth", true);
    }
    else
    {
        string[] monthNames = { "April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March" };
        for (int i = 0; i < monthNames.Length; i++)
        {
            dgvBudget.Rows[i].Cells["FiscalMonth"].Value = monthNames[i];
        }
    }

    string half = cmbBudgetCode.Text;
    lastactual = GetLastActual(half);
    lastbudget = GetLastBudget(half);
    int rowIndex = dgvBudget.Rows.Count;
    if (half == "Second Half Budget")
    {
        for (int i = 6; i < rowIndex; i++)
        {
            var row = dgvBudget.Rows[i];
            row.Cells["PrevFY"].Value = lastbudget[i];
            row.Cells["Actual_c"].Value = lastactual[i];
        }
        for (int i = 0; i < 6; i++)
        {
            var row = dgvBudget.Rows[i];
            row.Cells["PrevFY"].Value = lastactual[i];
            row.Cells["Actual_c"].Value = 0;
        }
    }
    else
    {
        for (int i = 0; i < rowIndex; i++)
        {
            var row = dgvBudget.Rows[i];
            row.Cells["PrevFY"].Value = lastbudget[i];
            row.Cells["Actual_c"].Value = lastactual[i];
        }
    }

    decimal total = 0m;
    foreach (UltraGridRow row in dgvBudget.Rows)
    {
        total += Convert.ToDecimal(row.Cells["PrevFY"].Value);
    }
    if (Config.typeofAccount != "Overtime")
    {
        if (GetAcctType(bhView.CurrentDataRow["SegValue1"].ToString()))
        {
            nbrPrevStatTotal.Value = total;
        }
        else
        {
            nbrPrevTotal.Value = total;
        }
    }
    else
    {
        nbrPrevTotal.Value = total;
    }
    stopwatch.Stop();
    MessageBox.Show(stopwatch.ElapsedMilliseconds.ToString());
}

So at the moment what this code is doing is

  1. Grabs 2 lists of data
  2. Then adds 2 columns if they do not exist (prevFy, and FiscalMonth)
  3. Then populates the ultragridview with current datasource plus my added data and columns
  4. Then Sums the values in the cells to show in a text box

Could I please have some help on how I can speed this up because right now it works it does, it's just so sloooooooooooow thank you!

0

There are 0 best solutions below