0

I have a datagrid bound to a list of objects. Users can add a new row below where the cursor is ( In code I create a new object and insert it in the list at the appropiate position).

Imagine that the datagrid has 4 rows

If the cursor is positioned in row number 4, then the row gets added, however, if the cursor is position in any of the other rows (1,2 or 3) then I get this exception:

System.Windows.Markup.XamlParseException occurred Message="Root element is missing." Source="PresentationFramework" LineNumber=0 LinePosition=0 StackTrace: at System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException) InnerException: System.Xml.XmlException Message="Root element is missing." Source="System.Xml" LineNumber=0 LinePosition=0 SourceUri="" StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Windows.Markup.XmlCompatibilityReader.Read() at System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode) InnerException:

NOTE: when the app first loads, if i first add a row (by being in the last row), then i am also able to add a row from any of the other rows. However, if i first try to add a row from row numbers 1,2,3 then it fails!

Any help will be greatly appreciated. I am totally lost. I doubt anyone else has experienced this, but maybe you know what can be causing this or how I could debug it, as I do not know where to start :(

        private void OnAddRowBelowCursor(DataGrid datagrid)
    {
        try
        {
            int index = datagrid.SelectedIndex;
            MyObject newObj = new MyObject();
            ObjectList.Insert(index + 1, newObj);
            Logging.log.Info("Appended object row below the cursor...");
        }
        catch (Exception ex)
        {
            Logging.log.Error("Error appending row below cursor. Reason: " + ex.ToString());
        }
    }



    private void OnAppendRowToBottom()
    {
        try
        {
            MyObject newObj = new MyObject();
            ObjectList.Add(newObj);
            Logging.log.Info("Appended object row to bottom...");
        }
        catch (Exception ex)
        {
            Logging.log.Error("Error appending row to the bottom of the table. Reason: " + ex.ToString());
        }
    }

I have also notice that adding a row to the bottom does not fail

Thanks

okidoki
  • 21
  • 5

2 Answers2

0

Looks like the data Your are loading is badly formatted. I may sugest You such a solution, which will allow you to parse any XML or HTML source even if it's fails the validation

Piotr Salaciak
  • 1,653
  • 1
  • 15
  • 28
  • Hi Piort, if the data I am loading is badly formatted, why it works some time and not others? Thanks – okidoki Mar 11 '11 at 15:14
  • How is the property AllowUserToInsertRow set for the datagrid? (or something like that). Maybe the last row index is the "new row", which is somehow differently treated. – Piotr Salaciak Mar 11 '11 at 15:26
  • I have set this to false CanUserAddRows="False" – okidoki Mar 17 '11 at 14:52
  • I have tracked down the problem to the richtextbox from the wpftoolkit library. If i replace this with a richtextbox then it works ok – okidoki Mar 17 '11 at 16:29
  • I was using the XAMLFormatter with the RTB, when creating a new row, I wasn't converting my empty string to XAML fortmat. Why it only fail when I was adding it below the cursor and not at the end, I still do not know. – okidoki Mar 18 '11 at 08:20
0

I was using the RTB from the extended library with an XAMLFormatter.

When creating a new row, I wasn't converting my empty string to XAML fortmat. Why it only fails when I was adding it below the cursor and not at the end, I still do not know. But it is fixed

okidoki
  • 21
  • 5