Get string values from Datagridview and show as decimals into a chart

Raymond Gilbers 176 Reputation points
2022-06-30T19:15:01.187+00:00

Hello

Is there any good way to get values which are displayed in a DataGridView as strings and take them convert them to decimals and than show into a chart?

I actually do not know where to start to solve this. I'm able to display the data into a DataGridView. I also know that these are all string so before to display anything I would guess that I need to convert the data into decimals (in my case decimals but it needs to be at least a number and not a string).

So my form_Load method has this code:

        private void frmHistoricalChart_Load(object sender, EventArgs e)  
        {  
            //Set up DayaGridView style  
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.LightGray;  
            dataGridView1.EnableHeadersVisualStyles = false;  
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("ABBvoice", 9, FontStyle.Bold);  
            dataGridView1.RowsDefaultCellStyle.Font = new Font("ABBvoice", 8);  
            dataGridView1.AllowUserToAddRows = false;  
  
            //Deserialize  
            _root = HelperXml.DeserializeXMLFileToObject<XmlRoot>(frmMain.ClientFileName);  
            statusLblCompany.ForeColor = Color.Black;  
            statusLblCompany.Text = "Company Name: " + _root.CompanyProfile.CompanyName;  
            statusLblSitename.Text = "Sitename: " + _root.CompanyProfile.SiteName;  
            statusLblMachineTotal.Text = "Machine Total: " + _root.CompanyProfile.MachineTotal.ToString();  
              
  
            //Bind the Machine Profile to a datasource   
            BindingSourceMachineProfiles.DataSource = _root.MachineProfiles.ToList();  
            cmbMachineName.DataSource = BindingSourceMachineProfiles;             
  
            //Bind the Machine Names  
            BindingSourceMachineNames.DataSource = _root.MachineProfiles.ToList().Select(i => i.MachineName);  
  
            //Bind the Machine Measurements  
            BindingSourceMachineMeasurements.DataSource = _root.MachineProfiles.Where(i => i.MachineName == cmbMachineName.SelectedItem.ToString()).Select(i => i.MachineMeasurements).First();  
            dataGridView1.DataSource = BindingSourceMachineMeasurements;  
  
        }  

It loads data from a XML file and by selecting an item at the combobox I'm able to decide which data should be visible on the DataGridView. I think that is not the point. I need to be able to show for example the data (in decimals) from the first column and the second column. But before I can send this to a chart object I think I need to convert them.

How can I do this the best way?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-06-30T19:43:47.567+00:00

    You can

    Convert XML to strongly typed class via image below then load and place into a list. See the following code sample where the Model folder contains classes for the XML and Classes folder to deserialize XML.

    216671-f1.png

    0 comments No comments

  2. Raymond Gilbers 176 Reputation points
    2022-07-11T13:32:26.807+00:00

    Hello,

    First of all my apologies I thought I had given information that I was able to resolve this. At the moment I'm at a offshore platform with limited internet. But I "solved" my issue as follows:

    yHDE = Convert.ToDouble(row.Cells[2].Value, CultureInfo.InvariantCulture);  
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.