Solved

Datagridview index does not change when sorted on header click

Posted on 2014-10-21
5
380 Views
Last Modified: 2014-10-27
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

0
Comment
Question by:r3nder
  • 4
5 Comments
 
LVL 6

Author Comment

by:r3nder
ID: 40395748
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

0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40395751
use the datagrid.refresh option to resort /redraw/recalculate indexes..
0
 
LVL 6

Author Comment

by:r3nder
ID: 40395757
sorry that didn't help, I wish it had :D
0
 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 40397577
gave up
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40405984
moved in another direction
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now