Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

Datagridview index does not change when sorted on header click

I have a datagridview. When I click on the header and sort it asc or desc  by a cell and then double click a selected row to load that record I find the index for that row is wrong - it loads the record that was originally at that index how can I fix that?
I tried this but that didnt work
      int jobnum1;
                bool jobNum = int.TryParse(GV_DH.CurrentRow.Cells["run"].EditedFormattedValue.ToString(), out jobnum1);
                if (jobNum == true)
                {
                    int rowIndex = -1;
                    foreach (DataGridViewRow row in GV_DH.Rows)
                    {
                        if (Convert.ToInt32(row.Cells["run"].EditedFormattedValue.ToString()) == jobnum1)
                        {
                            rowIndex = row.Index;
                            break;
                        }
                    }
                    GV_DH_IndexChanged(rowIndex);
                }

Open in new window

Avatar of r3nder
r3nder
Flag of United States of America image

ASKER

Here is how I add the items to the datagridview
    for (int i = 0; i < currentDE.Count(); i++)
            {
                //if (i != 0) { GV_DH.Rows.Add(); }
                GV_DH.Rows.Add();
                
                GV_DH.Rows[i].Cells["run"].Value = currentDE[i].RunID;
                if (currentDE[i].JobType == 0)
                {
                    GV_DH.Rows[i].Cells["jobtype"].Value = "weeble";
                }
                else if (currentDE[i].JobType == 1)
                {
                    GV_DH.Rows[i].Cells["jobtype"].Value = "wobble";
                }
                GV_DH.Rows[i].Cells["toolnumber"].Value = currentDE[i].ToolNumber;
                GV_DH.Rows[i].Cells["dhID"].Value = currentDE[i].ID;
                GV_DH.Rows[i].Cells["rundownhole"].Value = currentDE[i].RunDownhole;
                GV_DH.Rows[i].Cells["toolsize"].Value = currentDE[i].ToolSize;
                GV_DH.Rows[i].Cells["collarouterdiameter"].Value = currentDE[i].CollarOuterDiameter;
                GV_DH.Rows[i].Cells["connectionpin"].Value = currentDE[i].ConnectionPin;
                GV_DH.Rows[i].Cells["connectionbox"].Value = currentDE[i].ConnectionBox;
                GV_DH.Rows[i].Cells["subsn"].Value = currentDE[i].CollarSN;
                GV_DH.Rows[i].Cells["crossoversn"].Value = currentDE[i].CrossoverSN;
                GV_DH.Rows[i].Cells["ponysn1"].Value = currentDE[i].Pony1SN;
                GV_DH.Rows[i].Cells["ponysn2"].Value = currentDE[i].Pony2SN;

                if (currentDE[i].StartTime == new DateTime(1, 1, 1, 0, 0, 0))
                {

                }
                else
                {
                    GV_DH.Rows[i].Cells["starttime"].Value = currentDE[i].StartTime;
                }
                if (currentDE[i].EndTime == new DateTime(1, 1, 1, 0, 0, 0))
                {

                }
                else
                {
                    GV_DH.Rows[i].Cells["endtime"].Value = currentDE[i].EndTime;
                }


                GV_DH.Rows[i].Cells["startdepth"].Value = currentDE[i].StartDepth;
                GV_DH.Rows[i].Cells["enddepth"].Value = currentDE[i].EndDepth;
                if (currentDE[i].statuscomplete == true)
                {
                    GV_DH.Rows[i].Cells["runstatus"].Value = "complete";
                }
                else if (currentDE[i].statusfailed == true)
                {
                    GV_DH.Rows[i].Cells["runstatus"].Value = "failed";
                }
                else if (currentDE[i].statuslostinhole == true)
                {
                    GV_DH.Rows[i].Cells["runstatus"].Value = "lost in hole";
                }
                else if (currentDE[i].statusunknown == true)
                {
                    GV_DH.Rows[i].Cells["runstatus"].Value = "unknown";
                }
                GV_DH.Rows[i].Cells["toolfailureid"].Value = currentDE[i].ToolFailure;
                //GV_DH.Rows[i].Cells["toolfailureid"].Value = currentDE[i].ToolFailureID;
                GV_DH.Rows[i].Cells["notes"].Value = currentDE[i].notes;
                GV_DH.Columns["run"].SortMode = DataGridViewColumnSortMode.Automatic;
            } 

Open in new window

Avatar of Haris Dulic
use the datagrid.refresh option to resort /redraw/recalculate indexes..
Avatar of r3nder

ASKER

sorry that didn't help, I wish it had :D
ASKER CERTIFIED SOLUTION
Avatar of r3nder
r3nder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r3nder

ASKER

moved in another direction