Home > Mobile >  how can i change specific datagridview cell value?
how can i change specific datagridview cell value?

Time:10-12

I have the data from database, but one attribute on database record is bit (boolean). For example

name, surname, date, itHave
A      B     2020    1//
C      D     2021    0

The form application side, data is ok. I can see the records. But i want to change 1 and 0 as Yes or No.

I tried number(the count of datagridview)

for( i=0; i< number; i  ){

   if (dataGridView1.Rows[i].Cells[3].Value.ToString() =="1"){
       MessageBox.Show("Test");
       dataGridView1.Rows[i].Cells[3].Value = "Yes";
   }

}

MessageBox.Show("Test"); // this is running but it does not happen.

How can i solve it?

CodePudding user response:

Your example look fine and works. I've created initial DataGridView with some 0-1 values at "It Have" column:

enter image description here

Then used your example (edited just a bit by adding a ternary):

for (int i = 0; i < dataGridView1.Rows.Count - 1; i  )
{
    DataGridViewCell cell = dataGridView1.Rows[i].Cells[3];
    cell.Value = cell.Value.ToString() == "1" ? "Yes" : "No";
}

And get Yes/No instead of 1-0 as well:

enter image description here

Note, that your MessageBox.Show("Test"); would block code execution until you close it.

CodePudding user response:

Make your db data in human readable text, you can use anonymous types in LINQ:

var items = mydbitems.Select(x => new 
                                  {
                                     Name = x.name,
                                     Surname = x.surname,
                                     Date = x.date,
                                     itHave = x.itHave == 1 ? "yes" : "no"
                                  };
datagridview.DataSource = items.ToList();
  • Related