Save data from DataGridView in C# with WinForms

tibrachSkibia

New Member
I've two DataGridView one very simple and one more complex with different column types.My idea was to store store the data to XML and also load from the files at restart.For the simple one it's no problem (dgvAccounts is the DataGridView):\[code\] DataTable dtusers = new DataTable("Users"); DataColumn col1 = new DataColumn("ID"); DataColumn col2 = new DataColumn("Key"); dtUsers.Columns.Add(col1); dtUsers.Columns.Add(col2); if (File.Exists("user.xml")) dtUsers.ReadXml("user.xml"); dgvAccounts.DataSource = dtUsers; .... dtUsers.WriteXml("user.xml"); \[/code\]But on the other DataGridView the DataTable I like to get from the DataSource is ever null (dgvActions is the DataGridView):\[code\] DataGridViewTextBoxColumn dgvcolA5 = new DataGridViewTextBoxColumn(); DataGridViewComboBoxColumn dgvcolA6 = new DataGridViewComboBoxColumn(); dgvActions.Columns.Add(dgvcolA5); dgvActions.Columns.Add(dgvcolA6); DataTable dtActions = (DataTable)dgvActions.DataSource; dtActions.WriteXML("actions.xml");\[/code\]I'd also tried to create a DataTable in advance and add it as dgvActions.DataSource but the result is ever the same.So please can somebody help me to create a DataTable for the DataGridView or suggest any other way to store and load the data?ThanksAndre
 
Back
Top