Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one DataGridView and it contains five columns i.e. Name | Apr | May | Jun | July.
DataGridView represent values as

Name  |Apr | May | Jun | July 
Fee1  |100 | 100 | 100 | 100
Fee2  |500 | 500 | 500 | 500
Fee3  |700 | 700 | 700 | 700


note: this values come from a sql table

Now, my question is if I fill Rs.600 in a textBox and click on a button then I want to represent my DataGridView like


Name  |Apr | May| Jun| July 
Fee1  |100 |  0 |  0 |  0
Fee2  |500 |  0 |  0 |  0
Fee3  |  0 |  0 |  0 |  0



if I fill Rs. 1500 in a textBox and click on a button then I want to represent my DataGridView like

Name  |Apr | May| Jun| July 
Fee1  |100 |100 |  0 |  0
Fee2  |500 |100 |  0 |  0
Fee3  |700 |  0 |  0 |  0


similarly for all columns I want plz do help...........
Posted
Updated 7-Aug-16 8:04am
v2
Comments
Karthik_Mahalingam 7-Aug-16 2:48am    
does the gridview has always 3 rows only?
Member 12245539 7-Aug-16 3:35am    
No it will increase as per requirement
Karthik_Mahalingam 7-Aug-16 3:37am    
ok.
Karthik_Mahalingam 7-Aug-16 9:20am    
are the columns fixed?
Member 12245539 7-Aug-16 9:26am    
yes

1 solution

Try this

C#
private void button1_Click(object sender, EventArgs e)
       {
           int amount = Convert.ToInt32(textBox1.Text.Trim());
           for (int i = 1; i < dgv.Columns.Count; i++)
           {
               foreach (DataGridViewRow row in dgv.Rows)
               {
                   int value = Convert.ToInt32(row.Cells[i].Value);
                   if (amount >= value)
                       amount = amount - value;
                   else if (amount == 0)
                       row.Cells[i].Value = amount;
                   else
                   {
                       row.Cells[i].Value = amount;
                       amount = 0;
                   }
               }
           }
       }


       private void Form3_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Name");
           dt.Columns.Add("Apr");
           dt.Columns.Add("May");
           dt.Columns.Add("Jun");
           dt.Columns.Add("July");
           dt.Rows.Add("Fee1", 100, 100, 100, 100);
           dt.Rows.Add("Fee2", 500, 500, 500, 500);
           dt.Rows.Add("Fee3", 700, 700, 700, 700);
           dgv.DataSource = dt;
           dgv.AllowUserToAddRows = false;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900