Solved

datagridview sort on header click not "really" sorting it

Posted on 2014-10-06
7
470 Views
Last Modified: 2014-10-06
I am assigning a list to a datagridview like below - but when I select the header to sort asc or desc on any field and I go to edit a cell by double clicking a row or cell to load the record - it is not loading the right record. it is almost like the datagrid sorted visually but not programmatically

 for (int i = 0; i < searchJob.Count(); i++)
            {
                dataGridViewSearch.Rows.Add();
                dataGridViewSearch.Rows[i].Cells["JobID"].Value = searchJob[i].JobID;
                dataGridViewSearch.Rows[i].Cells["Company"].Value = searchJob[i].company;
                dataGridViewSearch.Rows[i].Cells["WellName"].Value = searchJob[i].wellname;
                dataGridViewSearch.Rows[i].Cells["Rig"].Value = searchJob[i].rig;
                dataGridViewSearch.Rows[i].Cells["RigNumber"].Value = searchJob[i].rignum;
                                dataGridViewSearch.Rows[i].Cells["OnLocationDate"].Value = searchJob[i].OnLocationDateTime;
                dataGridViewSearch.Rows[i].Cells["OffLocationDate"].Value = searchJob[i].OffLocationDateTime;
                if (searchJob[i].inprogress || searchJob[i].initiated)
                {
                    dataGridViewSearch.Rows[i].Cells["Days"].Value = (DateTime.Now - searchJob[i].OnLocationDateTime).Days;
                }
                else if (searchJob[i].cancelled || searchJob[i].potential)
                {
                    dataGridViewSearch.Rows[i].Cells["Days"].Value = 0;
                }
                else
                {
                    dataGridViewSearch.Rows[i].Cells["Days"].Value = (searchJob[i].OffLocationDateTime - searchJob[i].OnLocationDateTime).Days;

                }
                if (searchJob[i].inprogress) { dataGridViewSearch.Rows[i].Cells["Status"].Value = "in progress"; }
                else if (searchJob[i].initiated) { dataGridViewSearch.Rows[i].Cells["Status"].Value = "initiated"; }
                else if (searchJob[i].complete) { dataGridViewSearch.Rows[i].Cells["Status"].Value = "complete"; }
                else if (searchJob[i].cancelled) { dataGridViewSearch.Rows[i].Cells["Status"].Value = "cancelled"; }
                else if (searchJob[i].potential) { dataGridViewSearch.Rows[i].Cells["Status"].Value = "potential"; }
  
            }
            Cursor.Current = Cursors.Default;
        }

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
  • 3
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40363830
From what I see that loads the gird but you are asking about selecting a row after sorting.
After this code runs the order of items in searchJob matches that of the grid but does it still do so after a sort?

How do you select the row after sorting / how do you sort the grid ?  (Maybe your problem is you use searchJob instead of the grid when you attempt to edit a record)
0
 
LVL 6

Author Comment

by:r3nder
ID: 40363873
I use the automatic sort when the user clicks the header name
this is my doubleclick method

        private void dataGridViewSearch_DoubleClick(object sender, EventArgs e)
        {
            //qzz
            //int i = dataGridViewSearch.CurrentRow.Index;
            try
            {
                if (dataGridViewSearch.CurrentRow == null)
                {

                }
                else
                {
                    currentJob = searchJob.ElementAt(dataGridViewSearch.CurrentRow.Index);

                    tabsareok = true;
                    OpenJobFile();
                }
            }
            catch
            {

            }
        }

Open in new window

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40363893
>>currentJob = searchJob.ElementAt(dataGridViewSearch.CurrentRow.Index);

As I said, you are using the index in the grid and the element from another collection at the same index AFTER you have changed the ordering in one but not the other.
You need to find a unique identifier for the selected job in the grid then search through the searchJob collection for the record with that same identifier.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 6

Author Comment

by:r3nder
ID: 40363907
so if I wanted the jobID in searchJob (which is common in currentjob) how would I do the search
cuurentjob = searchjob.ElementAt(dataGridViewSearch????
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40364029
loop through the records.
0
 
LVL 6

Author Comment

by:r3nder
ID: 40364085
 int jobnum1;
                    bool jobNum = int.TryParse(dataGridViewSearch.CurrentRow.Cells["JobID"].EditedFormattedValue.ToString(),out jobnum1);
                    if (jobNum == true)
                    {
                        currentJob = searchJob.Find(item => item.JobID == jobnum1);
                    }

Open in new window


Done - thanks Andy
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40364087
Thanks Again Andy
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

730 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