Datagrid查看单元格值 [英] Datagrid View Cell Value

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

问题描述

由于某些奇怪的原因,当我选中或取消选中数据网格中的复选框时,无论值

For some odd reason when I check or uncheck the checkbox in the data grid, I still recieved true no matter what for the value

哪个不正确,我仍然收到了真的。如果取消选中该复选框,则该值假定为false。如果选中该复选框,则该值为true。有人可以帮助我解决这个问题。提前致谢。

which is incorrect. If the checkbox is uncheck, the value is suppose to report false. If the checkbox is checked the value is suppose to be true. Can someone assist me with this issue. Thanks in advance.

private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)

        {

$
            if(e.RowIndex> = 0)

            {

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex >= 0)
            {

                    DataGridViewRow row = this.dataGridView1.Rows [e.RowIndex];

                   DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

           }

           }

}

pianoboyCoder

pianoboyCoder

推荐答案

尝试这种方法,其中processColumn是一个DataGridViewCheckBoxColumn

Try this approach where processColumn is a DataGridViewCheckBoxColumn

using System;
using System.Windows.Forms;

namespace SuppressEnter
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            dataGridView1.CellContentClick += DataGridView1_CellContentClick;
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            senderGrid.EndEdit();

            if (e.RowIndex >= 0)
            {
                var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[e.RowIndex].Cells["processColumn"];
                if ((bool)cbxCell.Value)
                {
                    Console.WriteLine("Checked");
                }
                else
                {
                    Console.WriteLine("Unchecked");
                }
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new object[] { true, "A" });
            dataGridView1.Rows.Add(new object[] { true, "B" });
            dataGridView1.Rows.Add(new object[] { false, "C" });
            dataGridView1.Rows.Add(new object[] { true, "D" });
        }
    }
}


这篇关于Datagrid查看单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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