Resolved Datagridview Lookup value from one cell in specific column and display in another?

dv2020

Active member
Joined
Dec 18, 2020
Messages
30
Programming Experience
1-3
Hi All,

Trying to adjust my code to
a) When a user selects a value from the combo box in the data grid, it copies a value from a different column into another
b) When the data grid refreshes, looks up each value and copies the look up values from a different column into another (for each row)
c) when the copied value comes over into the new column, it keeps its format which is a %.

The below code partially works when I call it from an event (e.g CellClick), although removes the % formating when the new value is copied in.

Furth more, when I call it after the data grid has loaded it does not work at all.

Current Code:
  for (int i = 0; i < datagridProSpeedMap.Rows.Count; i++)
            {
                DataGridViewRow row = datagridProSpeedMap.Rows[i];

                if (row.Cells["MAP_SPEED"].Value.ToString() == "LEAD") // If the user selects LEAD from the combo box in the DG
                {
                   row.Cells["RSS_SPEED"].Value = row.Cells["JRL"].Value.ToString(); // get the value from column JRL and place it into column RSS_SPEED.
                   row.Cells["HSS_SPEED"].Value = row.Cells["HRL"].Value.ToString(); // get the value from column HRL and place it into column HSS_SPEED.
                }
                  if (row.Cells["MAP_SPEED"].Value.ToString() == "ON-PACE")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRONP"].Value.ToString(); // get the value from column JRONP and place it into column RSS_SPEED.
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRONP"].Value.ToString(); // get the value from column HRONP and place it into column RSS_SPEED.
                }
  }


any ideas ?

Thanks

Dv
 
Solution
Format specifiers like "p0" are only valid for numeric data. If you are putting string values into cells in those columns then no formatting will be applied. DO NOT call ToString on anything unless you expressly need a string. If you want to format numbers then use actual numbers, not strings containing numeric digit characters.
Hi jmcilhinney,

I made a image with same data of the data grid, see attachment.

Below is the code.

  1. When the Data grid is finished loaded, I also try to call proSpeedMapTableStats(); altough it does not work, i believe because the user is not interacting with the grid
  2. When i call proSpeedMapTableStats(); under a datagrid event, it works.

Full Code:
 public void proSpeedMapTableStats()
        {
            try
            {

                 for (int i = 0; i < datagridProSpeedMap.Rows.Count; i++)
            {
                DataGridViewRow row = datagridProSpeedMap.Rows[i];


                if (row.Cells["MAP_SPEED"].Value.ToString() == "LEAD")
                {
                   row.Cells["RSS_SPEED"].Value = row.Cells["JRL"].Value.ToString();
                   row.Cells["HSS_SPEED"].Value = row.Cells["HRL"].Value.ToString();
                }
                  if (row.Cells["MAP_SPEED"].Value.ToString() == "ON-PACE")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRONP"].Value.ToString();
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRONP"].Value.ToString();
                }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "OFF-PACE")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JROFP"].Value.ToString();
                    row.Cells["HSS_SPEED"].Value = row.Cells["HROFP"].Value.ToString();
                    }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "MID")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRMD"].Value.ToString();
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRMD"].Value.ToString();
                }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "OFF-MID")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JROMD"].Value.ToString();
                    row.Cells["HSS_SPEED"].Value = row.Cells["HROMD"].Value.ToString();
                    }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "BACK")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRBK"].Value.ToString();
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRBK"].Value.ToString();
                    }

                }
              
            }
            catch (Exception eEx)
            {
                ErrorLog.Input("ProSpeedMapSelect()" + eEx.Message);
            }
        

        }


Thanks

Dv
 

Attachments

  • dgtable.png
    dgtable.png
    41.1 KB · Views: 31
Let me re-phrase @jmcilhinney request for sample data: Can you show us the raw data before it goes into the DataGridView? I am guessing that he is indirectly asking where does the '%' symbol come from? Is it in the raw data? Is it something that you add into the cell data? Is it something that you have on the column or cell formatting properties or an event that you override?
 
Hello

Attached is the raw data, the percentages are in decimal format. e.g 0.11 is 11%.

When i first load the data grid, the follow code is used to displays the % which works.

C#:
                datagridProSpeedMap.Columns["RSS_SPEED"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JRL"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JRONP"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JROFP"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JRMD"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JROMD"].DefaultCellStyle.Format = "p0";
                datagridProSpeedMap.Columns["JRBK"].DefaultCellStyle.Format = "p0";


The issue is, when the code runs that copies the value from one of the other cells, to the RSS_SPEED column, the formating gets lost and the cell displays the 0.11 value, and not the 11%.

I try and re-apply the datagridProSpeedMap.Columns["RSS_SPEED"].DefaultCellStyle.Format = "p0"; after the copy, and it still wont format?
 

Attachments

  • dataraw.PNG
    dataraw.PNG
    10.3 KB · Views: 15
Format specifiers like "p0" are only valid for numeric data. If you are putting string values into cells in those columns then no formatting will be applied. DO NOT call ToString on anything unless you expressly need a string. If you want to format numbers then use actual numbers, not strings containing numeric digit characters.
 
Solution
Thank you, updated the code and now working.

C#:
 if (row.Cells["MAP_SPEED"].Value.ToString() == "LEAD")
                {
                   row.Cells["RSS_SPEED"].Value = row.Cells["JRL"].Value;
                   row.Cells["HSS_SPEED"].Value = row.Cells["HRL"].Value;
                }
                  if (row.Cells["MAP_SPEED"].Value.ToString() == "ON-PACE")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRONP"].Value;
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRONP"].Value;
                }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "OFF-PACE")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JROFP"].Value;
                    row.Cells["HSS_SPEED"].Value = row.Cells["HROFP"].Value;
                    }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "MID")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRMD"].Value;
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRMD"].Value;
                }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "OFF-MID")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JROMD"].Value;
                    row.Cells["HSS_SPEED"].Value = row.Cells["HROMD"].Value;
                    }

                if (row.Cells["MAP_SPEED"].Value.ToString() == "BACK")
                {
                    row.Cells["RSS_SPEED"].Value = row.Cells["JRBK"].Value;
                    row.Cells["HSS_SPEED"].Value = row.Cells["HRBK"].Value;
                    }

                }
 
Back
Top Bottom