I have simple datagridview that saves each line as a record to XML as they are entered. I use the CellEndEdit event to call the WriteXml() of the gridview which should save the coulmns as nodes in the XML. But, it does not do so for the first record!!
When I create another record and then do some mouse clicks navigation or press the movefirst, movelast buttons in the binded navigator, then it saves to the XML! It obviously is not finding the data to be saved for the first record - any help would be greatly appreciated.
Okay! The code does exactly this - there is a datagridview thats has few columns like Name, Description and say DateTime field. This gridview is bound to a BindingSource object that is in turn bound to a Dataset dsTodo. This dataset maps to a table called todo which has the columns stated above as its columns as well.
And the gridview is has a binding navigator as well. Now when the user enters a description in the grid view and presses Enter, I want it to save to XML. It does save, but not for the first record!!
private void dgvwTodoList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
dsTodo.WriteXml(m_sTodoXmlPath); //path where xml file is stored
So, the user can create a new item by pressing the + sign in the binding navigator control - its a predesigned control that comes with Microsoft pack so I have not coded anything for it.
When the user creates few records . then its saved perfectly, but if its just one or two, then it does NOT save at all. The reason I find is that when you click around in the gridview and use the Move Next, Previous buttons it saves perfectly. But, when its just one record, it does NOT save!!
Okay, I narrowed down the problem and fixed it - with a weird side effect.
Calling bindingsource1.EndEdit() before calling WriteXml() will make sure the rows are saved and they dont disappear as many coders have faced using this. Search in Google - Microsoft gives a vague explanation for it in Social MSDN site but that doesnt work anyway.
The weird sideffect is once the code is executed like this:
dsTodo.WriteXml(m_sTodoXmlPath); //Dataset for ToDo table writes to the XML path
In the gridview, when I try to type in the new row , it acts like the "INSERT" key has been pressed - meaning the chars flip - whatever I type replaces the characters in there - so it lets me type only 1 character anytime as that gets replaced when I type the next!!