如何调整datagridview窗口中的值C# [英] How to adjust values in datagridview windows C#

查看:87
本文介绍了如何调整datagridview窗口中的值C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,它包含五列,即Name |四月|五月|君| 7月。

DataGridView代表值为



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





注意:此值来自sql表



现在,我的问题是,如果我在textBox中填写Rs.600并单击一个按钮,那么我想代表我的DataGridView,如







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







如果我填写卢比。在文本框中1500并单击按钮然后我想表示我的DataGridView像






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





类似于我想要的所有列plz做帮助...........



similarly for all columns I want plz do help...........

推荐答案

试试这个



Try this

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

这篇关于如何调整datagridview窗口中的值C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆