question

ankitgoel-6643 avatar image
0 Votes"
ankitgoel-6643 asked ankitgoel-6643 commented

Change value inside datagridview cell

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--;
             }

         }
     }
dotnet-csharpdotnet-adonet
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ankitgoel-6643 commented

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

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


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks it solved my problem of negative and positive signs but can you please solve my another problem . I change the above query to find out the balances above and equal to 1000 but the datagridview is showing the data for above 1000 (+1000 ) and below 1000 (-1000) too .


string query = "Select $Name, $ClosingBalance from Ledger where $ClosingBalance >= 1000 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); // remove blank results
dataGridView1.Columns[1].DefaultCellStyle.Format = "- 0.00;+ 0.00;0.00";
}

     }![242255-mic.png][1]


[1]: /answers/storage/attachments/242255-mic.png

0 Votes 0 ·
mic.png (43.3 KiB)
Viorel-1 avatar image Viorel-1 ankitgoel-6643 ·

If you want to see the balances that are >= 1000 after changing the sign, then maybe try this query:

string query = "Select $Name, $ClosingBalance from Ledger where -$ClosingBalance >= 1000 order by $ClosingBalance DESC";

or

string query = "Select $Name, $ClosingBalance from Ledger where $ClosingBalance < -1000 order by $ClosingBalance DESC";

0 Votes 0 ·

The datagridview is not showing any data . What could be the problem . i have updated my query to
string query = "Select $Name, $ClosingBalance from Ledger where -$ClosingBalance >= 1000 order by $ClosingBalance DESC";

0 Votes 0 ·