I am using SAP 9.2 PL03, C# UI and DI API and trying to delete rows from a Grid's DataTable, but every time I call
Grid oGrid = (Grid)oForm.Items.Item(...ItemID).Specific;
oGrid.DataTable.Rows.Remove(2); // Index does not matter, tried 1 too
SAP just stops working (placed a breakpoint just before the call, the it works until I do call it, and I made sure I have existing rows in the DataTable --> I added 10 rows and tried to delete the first and second).

I tried doing oForm.Freeze(true) before the deletion, but without success, and I tried it during and ItemPress and a Form Open event too. The executing Thread right before the call is MTA (Console.WriteLine( System.Threading.Thread.CurrentThread.GetApartmentState() ;)
Edit:
I could catch the exception:
The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
Edit2:
I explicitly ran the code on STA:
Thread thread = new Thread(RunOnSTA);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join();
private void RunOnSTA()
{
try
{
Grid oGrid = (Grid)oForm.Items.Item(....ItemID).Specific;
oGrid.DataTable.Rows.Remove(2);
}
catch (Exception e)
{
Console.Write("asd");
}
}
Still crashes the SAP client.
Should I just give up and implement my own function where I get the DataTable as an XML string, Parse/Delete the row and Load it back from XML?
This kind of works, but messes up checkboxes for me:
public static void RemoveRowFromGridUsingXML(Grid oGrid, int rowIndex)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(oGrid.DataTable.SerializeAsXML(BoDataTableXmlSelect.dxs_DataOnly));
XmlNode node = doc.SelectSingleNode($"/DataTable/Rows/Row[{rowIndex}]");
if (node != null)
{
XmlNode parent = node.ParentNode;
parent.RemoveChild(node);
string newXML = doc.OuterXml;
oGrid.DataTable.LoadSerializedXML(BoDataTableXmlSelect.dxs_DataOnly, newXML);
}
}
Add comment