Solved

Datagridview index does not change when sorted on header click

Posted on 2014-10-21
5
393 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In SQL Server 2016, can we configure MASKing rules that applies for application level users ? 12 56
Hey!!!! 1 20
SQL Help 27 45
Cygwin - GNU GPL License 1 24
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 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