Datagridview中的复选框 [英] Checkbox inside a Datagridview

查看:104
本文介绍了Datagridview中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview中有复选框,当用户选择一些复选框并单击按钮时应该计算价格我该如何实现?

I have checkboxes inside datagridview and when the user selects some checkboxes and click the button the price should be calculated how can i achieve this?

推荐答案

复选框有一个酒店IsChecked属性。您可以使用它来计算您的定价值。

对于那些检查的复选框,您只能为它们执行价格计算。 U可以使用此属性来区分已选中和未选中的复选框。
Checkbox have a property IsChecked property. U can use it to calculate your pricing values.
For those check boxes which are check u can perform price calculation for them only. U can use this property to differentiate between check boxes which are checked and which are not.


DataGridView包含DataGridViewRows集合。集合中的每一行都是 DataGridViewRow [ ^ ]。



要获得所选行的总和,您需要遍历行集合:

DataGridView contains DataGridViewRows collection. Each row in the collection is DataGridViewRow[^].

To get the total sum of selected rows, you need to iterate through the collection of rows:
    private void button1_Click(object sender, EventArgs e)
    {
        DataGridViewCell dc = null;
        DataGridViewCheckBoxCell cbx = null;
        double mySum = 0;
        string s = string.Empty;
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            dc = dr.Cells[2];
            cbx = (DataGridViewCheckBoxCell)dc;
            if (Convert.ToBoolean(cbx.Value))
            {
                mySum += double.Parse(dr.Cells[0].Value.ToString());
            }
        }
        s = String.Format("Total sum: {0}", mySum.ToString());
        MessageBox.Show(s);
    }
}


这篇关于Datagridview中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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