r3nder
asked on
datagridview sort on header click not "really" sorting it
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;
}
ASKER
I use the automatic sort when the user clicks the header name
this is my doubleclick method
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
{
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so if I wanted the jobID in searchJob (which is common in currentjob) how would I do the search
cuurentjob = searchjob.ElementAt(dataGr idViewSear ch????
cuurentjob = searchjob.ElementAt(dataGr
loop through the records.
ASKER
int jobnum1;
bool jobNum = int.TryParse(dataGridViewSearch.CurrentRow.Cells["JobID"].EditedFormattedValue.ToString(),out jobnum1);
if (jobNum == true)
{
currentJob = searchJob.Find(item => item.JobID == jobnum1);
}
Done - thanks Andy
ASKER
Thanks Again Andy
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)