Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datagridview index does not change when sorted on header click

Posted on 2014-10-21
5
Medium Priority
?
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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…
Six Sigma Control Plans

688 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