Change value inside datagridview cell

ankit goel 746 Reputation points
2022-09-17T12:53:35.657+00:00

I have a DataGridview which is showing two columns column a and column b. columns b is showing balance of parties in + and - and 0. I want to programmatically reverse the signs of each balance in every cell in the column b. how to achieve that please suggest. I am using the below code to query data.
using (OdbcConnection con = new OdbcConnection(source))
{
string query = "Select $Name, $ClosingBalance from Ledger where $$IsDr:$ClosingBalance order by $ClosingBalance DESC";
using (OdbcDataAdapter dadapter = new OdbcDataAdapter(query, con))
{
DataTable table = new DataTable();
dadapter.Fill(table);
this. dataGridView1.DataSource = table.
dataGridView1.Columns[0].HeaderText = "Name of Party";
dataGridView1.Columns[1].HeaderText = "Balance of Party";
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
clearGrid(dataGridView1);
}
}

            }  
        }  
              
        }  

    // code for removing null values only   
    private void clearGrid(DataGridView view)  
    {  
        for (int i = 1; i < view.RowCount - 1; i++)  

        {  
            if (view.Rows[i].Cells[0].Value.ToString() == "" || view.Rows[i].Cells[1].Value.ToString() == "")  

            {  

                view.Rows.RemoveAt(i);  
                i--;  
            }  

        }  
    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,198 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2022-09-17T19:51:30.623+00:00

    If the Balance of Party column ($ClosingBalance) is numeric, then try adding this line:

    dataGridView1.Columns[1].DefaultCellStyle.Format = "-0.00;+0.00;0";


0 additional answers

Sort by: Most helpful