Reputation: 41
Same code does not function same for toolstrip button as it does for a normal button.
Appl. Details;
A datagridview consist of 6 columns in total. 6th column is a checkbox column to select 2 rows. And after pressing toolstrip bar button, program reads selected rows - [0] cell index values. this happens normally and program actually gets 2 row values when event is coming from normal button, but when event is fired from toolstrip button, list is only populated with 1 element even though 2 rows are selected.
code:
private void btnReportPrint_Click(object sender, EventArgs e)
{
var checkedRows = from DataGridViewRow r in gridMeasD.Rows
where Convert.ToBoolean(r.Cells[5].Value) == true
select r;
List<string> measIdList = new List<string>(2);
foreach (var row in checkedRows)
{
measIdList.Add(row.Cells["clm_MeasID"].Value.ToString());
}
if (measIdList.Count > 2)
{
MessageBox.Show("Please only select 2 different measurements to print out a report !", "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if(measIdList.Count == 2)
{
string val1 = measIdList[0];
string val2 = measIdList[1];
if (!((val1[0] == 'F' && val2[0] == 'C') || (val1[0] == 'C' && val2[0] == 'F')))
{
MessageBox.Show("Please only select 2 different measurements to print out a report !", "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if ((val1[0] == 'F' && val2[0] == 'C') || (val1[0] == 'C' && val2[0] == 'F'))
{
MessageBox.Show("Report printing!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
measIdList.Clear();
}
}
I am looking for an explanation because it doesn't make any sense.
Upvotes: 0
Views: 115
Reputation: 74710
My suspicion is (and I haven't checked) that clicking on a toolstrip button somehow doesn't cause the DGV to end its editing of the currect cell in the same way that clicking a regular button does, which means a process flow of:
Explicitly telling the DGV to end its editing of the current cell, via dgv.EndEdit()
within the event handler, before the Cells collection is enumerated, should help
Upvotes: 1