1

In my C# .Net Framework 4.8 Windows Forms project I have a DataTable object which is regularly updated. Interestingly if one particular cell ("LTP") is updated, 2 more empty rows will be added automatically.

public void UpdateTable(List<SummaryRow> SummaryRows, List<Contract> ContractsList)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString()+":"+"UpdateTable called");
        if (SummaryRows.Count != 0)
        {
            SummaryGridView.SuspendLayout();
            for (int index = 0; index < ContractsList.Count; index++)
            {

                if (SummaryTable.Rows[index].Field<int>("PedingBuyOrders") != SummaryRows[index].PedingBuyOrders) { SummaryTable.Rows[index].SetField("PedingBuyOrders", SummaryRows[index].PedingBuyOrders); }
                if (SummaryTable.Rows[index].Field<int>("PedingSellOrders") != SummaryRows[index].PedingSellOrders) { SummaryTable.Rows[index].SetField("PedingSellOrders", SummaryRows[index].PedingSellOrders); }
                if (SummaryTable.Rows[index].Field<int>("PedingBuyQty") != SummaryRows[index].PedingBuyQty) { SummaryTable.Rows[index].SetField("PedingBuyQty", SummaryRows[index].PedingBuyQty); }
                if (SummaryTable.Rows[index].Field<int>("PedingSellQty") != SummaryRows[index].PedingSellQty) { SummaryTable.Rows[index].SetField("PedingSellQty", SummaryRows[index].PedingSellQty); }
                if (SummaryTable.Rows[index].Field<int>("FilledBuyOrders") != SummaryRows[index].FilledBuyOrders) { SummaryTable.Rows[index].SetField("FilledBuyOrders", SummaryRows[index].FilledBuyOrders); }
                if (SummaryTable.Rows[index].Field<int>("FilledSellOrders") != SummaryRows[index].FilledSellOrders) { SummaryTable.Rows[index].SetField("FilledSellOrders", SummaryRows[index].FilledSellOrders); }
                if (SummaryTable.Rows[index].Field<int>("FilledBuyQty") != SummaryRows[index].FilledBuyQty) { SummaryTable.Rows[index].SetField("FilledBuyQty", SummaryRows[index].FilledBuyQty); }
                if (SummaryTable.Rows[index].Field<int>("FilledSellQty") != SummaryRows[index].FilledSellQty) { SummaryTable.Rows[index].SetField("FilledSellQty", SummaryRows[index].FilledSellQty); }
                if (SummaryTable.Rows[index].Field<float>("Pnl") != SummaryRows[index].Pnl) { SummaryTable.Rows[index].SetField("Pnl", SummaryRows[index].Pnl); }
                if (SummaryTable.Rows[index].Field<float>("CashFlow") != SummaryRows[index].CashFlow){SummaryTable.Rows[index].SetField("CashFlow", SummaryRows[index].CashFlow);}

                Console.WriteLine(DateTime.Now.ToLongTimeString() + ":" + "UpdateTable called(before LTP update). Num of SummaryTable rows:" + SummaryTable.Rows.Count);
                if (SummaryTable.Rows[index].Field<float>("LTP") != SummaryRows[index].LTP) { SummaryTable.Rows[index].SetField("LTP", SummaryRows[index].LTP); }
                Console.WriteLine(DateTime.Now.ToLongTimeString() + ":" + "UpdateTable called(after LTP update). Num of SummaryTable rows:" + SummaryTable.Rows.Count);
            }
            SummaryGridView.ResumeLayout();
        }
    }

This is how my console looks like: enter image description here

This is how my full Forms code looks like: https://pastebin.com/hvnnTtxe

This is how the table looks like after the first update: enter image description here

Does anyone have any idea why am I having these empty rows?

Update1: I think it has to do something with threading. The UpdateTable is called from a separate thread. When I change my app to a single threaded application, there is no issue. I guess the reason why it appears only if the "LTP" column is changed is because that is the column thats changing multiple times in a second.

Update2: If AllowUserToAddRows property is disabled, it works perfectly. I am not clicking on that particular window(I don't even have time to click on that since it add those extra rows so quickly) This is how the GridView properties set: http://prntscr.com/qiynf8 For me it looks like the the GridView can add rows to the DataTable. Can anyone explan me why is it doing this?

4
  • 1
    can you try to add a .Trim() to the SummaryRows[index].LTP ? Try again. I think there are weird characters in that column that adds new lines Dec 30, 2019 at 17:32
  • @PauloAlves Field<float>("LTP") -> it is a float just like SummaryRows[index].LTP, so I guess technically there is no possibility that weird characters are added.
    – user10466538
    Dec 30, 2019 at 17:38
  • indeed, didn't saw that. Dec 30, 2019 at 17:41
  • Please provide a full working example minimal reproducible example. The pastebin link only provides partial code and does not provide a reproducible example.
    – Barns
    Dec 30, 2019 at 19:27

1 Answer 1

0

I think the order of set field is problem.

please move this code

    if (SummaryTable.Rows[index].Field<float>("CashFlow") != SummaryRows[index].CashFlow){SummaryTable.Rows[index].SetField("CashFlow", SummaryRows[index].CashFlow);}

below the

Console.WriteLine(DateTime.Now.ToLongTimeString() + ":" + "UpdateTable 
called(after LTP update). Num of SummaryTable rows:" + SummaryTable.Rows.Count);
8
  • In my case ContractsList.Count should always equal to SummaryRows.Count, but this is not relevant.
    – user10466538
    Dec 30, 2019 at 17:32
  • ok. then would you test the code with 2 summaries, and share the console log again?
    – ysk silver
    Dec 30, 2019 at 17:56
  • Here you go: pastebin.com/R8wG7cYg Result:prntscr.com/qhmaz3
    – user10466538
    Dec 30, 2019 at 18:10
  • this console log is when ContractsList.Count == 1, right? I need console log where ContractsList ==2. So I wanna you add more data to ContractsList and test
    – ysk silver
    Dec 30, 2019 at 18:19
  • I sent you the full Console log. It gets from 1 to 3 immediately, this is why I just don't get it. I have spent the whole day figuring it out. prntscr.com/qhmfoa
    – user10466538
    Dec 30, 2019 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.