• 实时更新DataGridView 合计值


    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    dataGridView1.DataSourceChanged += new EventHandler(dataGridView1_DataSourceChanged);
    dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pubs;Integrated Security=True");
    using (SqlDataAdapter da = new SqlDataAdapter("select * from jobs", conn))
    {
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    }
    dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
    dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);
    }

    private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
    {
    TextSum();
    }

    private void TextSum()
    {
    //throw new Exception("The method or operation is not implemented.");
    int x1 = 0;
    int x2 = 0;
    foreach (DataGridViewRow dr in dataGridView1.Rows)
    {
    if (!IsNullOrEmpty(dr.Cells["min_lvl"].Value))
    x1 += Convert.ToInt32(dr.Cells["min_lvl"].Value.ToString());

    if (!IsNullOrEmpty(dr.Cells["max_lvl"].Value))
    x2 += Convert.ToInt32(dr.Cells["max_lvl"].Value.ToString());
    }
    textBox1.Text = x1.ToString();
    textBox2.Text = x2.ToString();
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
    dataGridView1_DataSourceChanged(null, null);
    }

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
    dataGridView1_DataSourceChanged(null, null);
    }

    private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
    {
    dataGridView1_DataSourceChanged(null, null);
    }

    private bool IsNullOrEmpty(object value)
    {
    if (value != null)
    return value.ToString() == string.Empty;
    return true;
    }

    private bool IsNullOrWhite(object value)
    {
    if (value != null)
    return value.ToString().Trim() == string.Empty;
    return true;
    }
    }

  • 相关阅读:
    android studio遇到的一系列问题
    flask接口入门实现简单的登录注册(二)
    flask接口入门实现简单的登录注册(一)
    第一节:django环境 模型 视图 后台
    设计模式二:建造者模式
    设计模式一:工厂模式
    mysql系列:创建数据库和用户及赋权
    mysql系列:mysql的数据类型
    mysql系列:数据库范式与mysql引擎
    mysql系列:centos7.6上安装mysql8.0
  • 原文地址:https://www.cnblogs.com/z5337/p/3441156.html
Copyright © 2020-2023  润新知