Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • Add entry into datagridview cell based on another cell value
AuthorQuestion
Add entry into datagridview cell based on another cell value
Posted on: 29 Jun 2013
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");
}
}

AuthorReply
Vulpes
  • 1
  • 0
accepted
Re: Add entry into datagridview cell based on another cell value
Posted on: 29 Jun 2013   Accepted Answer
Hi Mike,

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;

 

Re: Add entry into datagridview cell based on another cell value
Posted on: 29 Jun 2013  
Thanks Vulpes,

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?

Re: Add entry into datagridview cell based on another cell value
Posted on: 29 Jun 2013  
It will depend on how the combobox column has been set up.

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

Re: Add entry into datagridview cell based on another cell value
Posted on: 01 Jul 2013  
Hi Vulpes,

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?

Re: Add entry into datagridview cell based on another cell value
Posted on: 01 Jul 2013  
Thanks Vulpes, your message box idea helped me sort this and therefore your first answer was correct.

Many thanks.

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.