如何在datagridview中打印给定数据?

时间:2017-03-09 02:20:23

标签: c# datagridview

程序从hrm文件读取数据并解析标题和数据。数据存储在变量中。通过连接字符串testOutput,数据将在控制台中打印。现在如何在datagridview中打印?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_Test.Data
{
    class Params : Data
    {
        float Version;
        int Monitor;
        bool IsSMode = true;
        Dictionary<string, char> ModeOrSMode = new Dictionary<string, char>();
        DateTime Date;
        String StartTime;
        String Length;
        int Interval;
        int Upper1;
        int Lower1;
        int Upper2;
        int Lower2;
        int Upper3;
        int Lower3;
        String Timer1;
        String Timer2;
        String Timer3;
        int ActiveLimit;
        int MaxHR;
        int RestHR;
        int StartDelay;
        int VO2max;
        int Weight;

        public Params(string[] lines)
        {
            ParseAndStoreData(lines);
        }

        protected override void ParseAndStoreData(string[] rawData)
        {
            string tempStore;

            Version = float.Parse(rawData[0].Split('=')[1]) / 100;
            Monitor = int.Parse(rawData[1].Split('=')[1]);
            setModeOrSModeValues(rawData[2].Split('=')[1]);

            tempStore = rawData[3].Split('=')[1];

            Date = new DateTime(int.Parse(tempStore.Substring(0, 4)), int.Parse(tempStore.Substring(4, 2)), int.Parse(tempStore.Substring(6, 2)));
            StartTime = rawData[4].Split('=')[1];
            Length = rawData[5].Split('=')[1];
            Interval = int.Parse(rawData[6].Split('=')[1]);
            Upper1 = int.Parse(rawData[7].Split('=')[1]);
            Lower1 = int.Parse(rawData[8].Split('=')[1]);
            Upper2 = int.Parse(rawData[9].Split('=')[1]);
            Lower2 = int.Parse(rawData[10].Split('=')[1]);
            Upper3 = int.Parse(rawData[11].Split('=')[1]);
            Lower3 = int.Parse(rawData[12].Split('=')[1]);
            Timer1 = rawData[13].Split('=')[1];
            Timer2 = rawData[14].Split('=')[1];
            Timer3 = rawData[15].Split('=')[1];
            ActiveLimit = int.Parse(rawData[16].Split('=')[1]);
            MaxHR = int.Parse(rawData[17].Split('=')[1]);
            RestHR = int.Parse(rawData[18].Split('=')[1]);
            StartDelay = int.Parse(rawData[19].Split('=')[1]);
            VO2max = int.Parse(rawData[20].Split('=')[1]);
            Weight = int.Parse(rawData[21].Split('=')[1]);
        }

        private void setModeOrSModeValues(string values)
        {
            switch (Version.ToString())
            {
                case "1.05":
                    {
                        SetModeValues(values);
                    }
                    break;

                case "1.06":
                    {
                        SetSModeValues(values);
                    }
                    break;

                case "1.07":
                    {
                        SetSModeValuesWithAirPressure(values);
                    }
                    break;

                default:
                    break;
            }
        }

        private void SetModeValues(string values)
        {
            IsSMode = false;

            ModeOrSMode.Add("Cad/Alt", values[0]);
            ModeOrSMode.Add("CC data", values[1]);
            ModeOrSMode.Add("US/Euro unit", values[2]);
        }

        private void SetSModeValues(string values)
        {
            ModeOrSMode.Add("Speed", values[0]);
            ModeOrSMode.Add("Cadenence", values[1]);
            ModeOrSMode.Add("Altitude", values[2]);
            ModeOrSMode.Add("Power", values[3]);
            ModeOrSMode.Add("Power Left Right Balance", values[4]);
            ModeOrSMode.Add("Power Pedalling Index", values[5]);
            ModeOrSMode.Add("HR/CC data", values[6]);
            ModeOrSMode.Add("US/Euro unit", values[7]);
        }

        private void SetSModeValuesWithAirPressure(string values)
        {
            SetSModeValues(values);

            ModeOrSMode.Add("Air Pressure", values[8]);
        }

        public void TestDisplayData()
        {
            string testOutput = "Params Data \n";

            testOutput += "Version: " + Version.ToString() + "\n";
            testOutput += "Monitor: " + Monitor.ToString() + "\n";
            testOutput += ((IsSMode) ? "S" : "") + "Mode :" + "\n";

            foreach (KeyValuePair<string, char> item in ModeOrSMode)
            {
                testOutput += "\t\t" + item.Key + " = " + item.Value + "\n";
            }

            testOutput += "\n";

            testOutput += "Date: " + Date.ToString() + "\n"; // date can be formatted
            testOutput += "Start Time: " + StartTime + "\n";
            testOutput += "Length: " + Length + "\n";
            testOutput += "Interval: " + Interval + "\n";
            testOutput += "Upper1: " + Upper1 + "\n";
            testOutput += "Lower1: " + Lower1 + "\n";
            testOutput += "Upper2: " + Upper2 + "\n";
            testOutput += "Lower2: " + Lower2 + "\n";
            testOutput += "Upper3: " + Upper3 + "\n";
            testOutput += "Lower3: " + Lower3 + "\n";
            testOutput += "Timer1: " + Timer1 + "\n"; // 07:04 can be shown as 7 min and 4 seconds
            testOutput += "Timer2: " + Timer2 + "\n";
            testOutput += "Timer3: " + Timer3 + "\n";
            testOutput += "ActiveLimit: " + ActiveLimit + "\n";
            testOutput += "MaxHR: " + MaxHR + "\n";
            testOutput += "RestHR: " + RestHR + "\n";
            testOutput += "StartDelay: " + StartDelay + "\n";
            testOutput += "Vo2max: " + VO2max + "\n";
            testOutput += "Weight: " + Weight + "\n";

            Console.WriteLine(testOutput); // display in console 


            // how to display above data in datagridview
        }
    }
}

0 个答案:

没有答案