I'm trying to auto enter todays date in a datagriview cell when a user clicks a value in another cell to "YES"
I'm nearly there but my code below has something stopping it working?
I think it's on this line:
if (content.ToLower().Trim() == "YES")
Below is my example:
if (e.ColumnIndex != 4 || e.ColumnIndex != 4) return;
object obj = suppliersDataGridView.Rows[e.RowIndex].Cells[4].Value;
if (obj != null && !DBNull.Value.Equals(obj))
{
string content = obj.ToString();
if (content.ToLower().Trim() == "YES")
{
suppliersDataGridView.Rows[e.RowIndex].Cells[7].Value = DateTime.Now.ToString("dd-MM-yyyy");
}
}
Loading
VulpesPosted Jun 29, 2013, 12:05 PM
The problem lies with this line:
if (content.ToLower().Trim() == "YES")
It should, of course, be:
if (content.ToUpper().Trim() == "YES")
Also, although it's not doing any harm, you have some duplication in this line:
if (e.ColumnIndex != 4 || e.ColumnIndex != 4) return;
mike DelvottiPosted Jul 1, 2013, 6:53 AM
Many thanks.
mike DelvottiPosted Jul 1, 2013, 4:25 AM
That's strange, when I instert that line nothing happens?
Apologies, I just re typed this and it does throw a message, the messagebox just keeps popping up with 'No' even when you change the dropdown to YES?
VulpesPosted Jun 29, 2013, 12:27 PM
If you insert the highlighted line, what do you see?
if (obj != null && !DBNull.Value.Equals(obj))
{
string content = obj.ToString();
MessageBox.Show(content);
if (content.ToUpper().Trim() == "YES")
{
// etc
mike DelvottiPosted Jun 29, 2013, 12:12 PM
I changed it "ToUpper" and it still doesn't seem to work :-(
If i remove that 'if' statement it works? but just ignores whether it's YES or NO in the cell?
I must add the cell that contains the YES/NO is a ComboBox style cell will this matter?