Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Below code is if checkbox selection will check with count and it is more than 1 than it will call update method or else it shows message box as "Please select at least one check box".But if after selection checkbox too no values has been edited in any cells in gridview than it also gives messgae like "No records updated". I am struggling here How can I determine whether any cells value has changed or not after selecting checkbox so that base on that we can put a validation message. I am using Winform here .Please suggest.

What I have tried:

private void btnUpdate_Click(object sender, EventArgs e)
      {
          bool Msgalert = false;
          int total = dataGridView1.Rows.Cast<DataGridViewRow>().Where(p => Convert.ToBoolean(p.Cells["chkbox"].Value) == true).Count();

          DataGridViewRow row = new DataGridViewRow();
          if(total > 0 )
          {
              //if(Convert.ToString(row.Cells["polNoDataGridViewTextBoxColumn"].Value) == true||
              //    Convert.ToBoolean(row.Cells["lUBDataGridViewTextBoxColumn"].Value) == true||
              //    Convert.ToBoolean(row.Cells["EffDtDataGridViewTextBoxColumn"].Value) == true||
              //    Convert.ToBoolean(row.Cells["ExpDtDataGridViewTextBoxColumn"].Value) == true
              //    )
                  Update();
          }
          else if (total == 0)
          {
              MessageBox.Instance.ShowMessageBox("Please Select at least one checkbox");
Posted
Updated 14-Jun-22 20:00pm

1 solution

You could actually do this in a much simpler way; you could simply loop through the DataGridView's rows and iterate an integer variable every time you find a row which value for the field in question is equivalent to 1 (or larger than zero, for clarity) (long as you have set said column's true and false values to 1 and 0 respectively). Then, if that integer is equal to zero, show your message - or else update your values.

int checkedRowTotal = 0;
foreach (DataGridViewRow dataRow in DataGridView1.Rows) {
   int checkedValue = dataRow.Cells["chkbox"].Value;
   if (checkedValue < 0) checkedRowTotal += 1;
};

if (checkedRowTotal == 0) {
  MessageBox.Instance.ShowMessageBox("Please Select at least one checkbox");
  return;
} else {
  Update();
};


I have not tested this code so you'll need to debug it, but I think that would solve your issue if I understand it correctly. Also keep in mind that in order for the checkbox value to actually change, you're going to need to update your data source in the CellValueChanged event of the DataGridView - could I see the code for that?
 
Share this answer
 
v2
Comments
Thirumadhi T Johnson 15-Jun-22 2:38am    
Hi @Fydobas there is no cellvaluechanged event in my code ,I have event cellClick,selectionChanged,CellBeginEdit,Scroll,CellValidating
Thirumadhi T Johnson 15-Jun-22 2:38am    
in your code where can I write the validation message like 'No records are updated'
Fydobas 15-Jun-22 3:00am    
Oh that part was not clarified on your question - either that or I misread. The way your question was worded, it seemed as if the "No records are updated" message was being fired even though it shouldn't.

That part is a little more complicated. Ideally, you would have a CellValueChanged event, in which event you would iterate a class level (rather than function level) counter variable just like the example I showed you above - and, in addition, you would have to check if the new cell value is, each time, not equal to the old value; and you will have to do that according to your own app structure, I can't show you here without knowing how your DataGridView is populated and how you handle your events.

After you do that, on the "click" function, you would have to check the second counter you added in the previous step - if that's equal to 0, you show your message.
Thirumadhi T Johnson 15-Jun-22 7:52am    
Hi I have 4 columns along with checkbox @Fydobas below is the Update Method
public void Update()
{
int tbl_pol__id = 0;
string LUB = string.Empty;
string POL_No = string.Empty;
DateTime Effdate;
DateTime Expdate;

//if (MessageBox.Show("Would like to update then click yes!!",
// "Input Policy", MessageBoxButtons.YesNo) ==
// System.Windows.Forms.DialogResult.Yes)
MessageBox.Instance.ShowMessageBox("Would like to update then click yes!", "Yes", "No");
if (MessageBox.Instance.IsDialogResultOK)
{
foreach (DataGridViewRow dgvrow in dataGridView1.SelectedRows)
{

if (Convert.ToBoolean(dgvrow.Cells["chkbox"].Selected) == true)
{

LUB = dgvrow.Cells["luBDataGridViewTextBoxColumn"].Value.ToString();
POL_No = dgvrow.Cells["polNoDataGridViewTextBoxColumn"].Value.ToString();
Effdate = Convert.ToDateTime(dgvrow.Cells["EffDtDataGridViewTextBoxColumn"].Value);
Expdate = Convert.ToDateTime(dgvrow.Cells["ExpDtDataGridViewTextBoxColumn"].Value);
tbl_pol__id = Convert.ToInt32(dgvrow.Cells["TBL_POL_ID"].Value);


collServClient.UpdateInputPolicyData(tbl_pol__id, LUB, POL_No, Effdate, Expdate);

}

}

MessageBox.Instance.ShowMessageBox(ConstantsUI.C_Close);
Globals.ThisWorkbook.Close(0);
}
}


and the same update method I am calling from
private void btnUpdate_Click(object sender, EventArgs e)
{
bool Msgalert = false;
int total = dataGridView1.Rows.Cast<datagridviewrow>().Where(p => Convert.ToBoolean(p.Cells["chkbox"].Value) == true).Count();

if (total > 0 )
{

Update();
}
else if (total == 0)
{
MessageBox.Instance.ShowMessageBox("Please Select at least one checkbox");
Msgalert = MessageBox.Instance.IsDialogResultOK;
}
}

So can you please help me now with given codes how to achieve my query if no cells value has changed than shows Messagebox like "No records updated "
Thirumadhi T Johnson 15-Jun-22 9:07am    
Hi @Fydobas Yes I have cellavalue changed evenet but it is in the name of cellvalue click
Here is the code for this event

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true)) //0 is the column number of checkbox
{
row.Selected = true;
BtnCancel.Enabled = false;
row.DefaultCellStyle.SelectionBackColor = Color.LightSlateGray;

}
else
row.Selected = false;
BtnCancel.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
}

switch(dataGridView1.Columns[e.ColumnIndex].Name)
{
case "polEffDtDataGridViewTextBoxColumn":
_Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height);
dtp.Location = new Point(_Rectangle.X, _Rectangle.Y);
dtp.Visible = true;
break;
case "polExpDtDataGridViewTextBoxColumn":
_Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height);
dtp.Location = new Point(_Rectangle.X, _Rectangle.Y);
dtp.Visible = true;
break;

}
}

Looking forward for your help:) Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900