Solved

Datagridview index does not change when sorted on header click

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

746 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

9 Experts available now in Live!

Get 1:1 Help Now