/* * Software to access vehicle information via the OBD-II connector. * * Copyright © 2012 Nils Hammar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Alternative licensing is possible, see the licensing document. * * The above text may not be removed or modified. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Globalization; namespace CanApp.Data { public partial class SimpleData : UserControl { private ILogging iLogging; public SimpleData(ILogging iLogging, string boxTitle, bool interactive) { this.iLogging = iLogging; InitializeComponent(); this.groupBox1.Text = boxTitle; this.saveButton.Visible = interactive; } public void add(string label, string value) { this.add(label, "", value); } public void add(string label, string unit, string value) { if (this.simpleDataDgv.ColumnCount > 0) { DataGridViewRowCollection dgvrc = this.simpleDataDgv.Rows; int n = dgvrc.Add(1); DataGridViewRow dgvr = dgvrc[n]; DataGridViewCellCollection dgvcc = dgvr.Cells; dgvcc["description"].Value = label; dgvcc["unit"].Value = unit; dgvcc["data"].Value = value; this.Height += dgvr.Height; } else { this.iLogging.appendText("this.simpleDataDgv.ColumnCount=" + this.simpleDataDgv.ColumnCount + ", this.simpleDataDgv.CanFocus=" + this.simpleDataDgv.CanFocus + "\r\n"); } } private void saveButton_Click(object sender, EventArgs e) { saveFileDialog1.FileName = ""; saveFileDialog1.DefaultExt = ".csv"; saveFileDialog1.AddExtension = true; saveFileDialog1.ValidateNames = true; saveFileDialog1.Filter = "Excel CSV Format|*.csv"; if (this.saveFileDialog1.ShowDialog() == DialogResult.OK) { Stream fileStream = null; StreamWriter streamWriter = null; try { fileStream = this.saveFileDialog1.OpenFile(); streamWriter = new StreamWriter(fileStream, CanMain.iso_8859_1); string csvSep = ";"; if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ".") { csvSep = ","; } streamWriter.WriteLine("\"Name\"" + csvSep + "\"Unit\"" + csvSep + "\"Value\""); DataGridViewRowCollection dgvrc = this.simpleDataDgv.Rows; foreach (DataGridViewRow dgvr in dgvrc) { DataGridViewCellCollection dgvcc = dgvr.Cells; if (dgvcc["description"].Value != null && dgvcc["unit"].Value != null && dgvcc["data"].Value != null) { string name=(string)dgvcc["description"].Value; streamWriter.Write("\""); streamWriter.Write(name); streamWriter.Write("\"" + csvSep + "\""); string unit = (string)dgvcc["unit"].Value; streamWriter.Write(unit); streamWriter.Write("\"" + csvSep); string val = dgvcc["data"].Value.ToString(); try { Convert.ToDouble(val); // Test if numeric. streamWriter.Write(val); } catch { // Value isn't numeric, print as string. streamWriter.Write("\""); streamWriter.Write(val); streamWriter.Write("\""); } streamWriter.WriteLine(); } } } catch (Exception ex) { MessageBox.Show("Could not open file '" + saveFileDialog1.FileName + "'\r\n\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { if (streamWriter != null) { try { streamWriter.Close(); } catch { } } if (fileStream != null) { try { fileStream.Close(); } catch { } } } } } } }