zoukankan      html  css  js  c++  java
  • 实时更新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;
    }
    }

  • 相关阅读:
    header头参数 确定该文件类型
    phpexcel 使用说明
    杂七杂八 各种小知识
    php 后知后觉
    限制SSH登录失败次数
    DES和AES密码之间的区别 & 对称加密算法DES、3DES和AES 原理总结
    加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用
    加密算法学习总结---DES-CBC加密算法 & 分组加密的四种模式
    Linux下进程间通信方式——共享内存
    fork()+pipe() --> 父子进程间通过管道通信 Linux系统编程pipe()
  • 原文地址:https://www.cnblogs.com/z5337/p/3441156.html
Copyright © 2011-2022 走看看