从datagridviewcheckboxcolumn获取不正确的值? [英] Getting incorrect value from datagridviewcheckboxcolumn?

查看:234
本文介绍了从datagridviewcheckboxcolumn获取不正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview(dgv),其中我将第一列添加为dgvcheckboxcolumn,并且我能够提取其checkboxcell检查的第二列的值。但我得到一个不同类型的错误,当我检查第一行复选框其值未被重新检查,但当我检查任何其他行复选框以前检查行值恢复,但我想提取即时值,即如果我检查第一行的复选框我希望第一行第二列值。



这里是我的代码我知道我做了一些简单的错误,但无法追踪哪里出错,



I have a datagridview(dgv) , in which i adding first column as dgvcheckboxcolumn and i am able to extract value of second column whose checkboxcell checked. but i am getting a different type of error that is "when i check 1st row checkbox its value not retrived but when i check any other row check box previously checked row value retrived" , but i want to extract instant value , i.e if i checked 1st rows's checkbox i want 1st rows 2nd column value.

here is my code i know something simple mistake i did but cant trace where is error ,

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   try
   {
     List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();

     foreach (DataGridViewRow row in dataGridView1.Rows)
     {
        if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
        {
         rows_with_checked_column.Add(row);
         data = row.Cells[1].Value.ToString();
        }
       row.Cells[SerialNo.Name].Value = false;
     }

    ifile.IniWriteValue("Config Section", "INDENT_NO", data);
    txtindentcode.Text = data;
    txtfromstore.Text = fm;
     txttostore.Text = to;
} 

推荐答案

使用如下代码:



Use like below code:

       public Form1()
        {
            InitializeComponent();

            dgvEmployee.DataSource = new Employee().GetSampleData();
            DataGridViewCheckBoxColumn colChk = new DataGridViewCheckBoxColumn();
            colChk.DisplayIndex = 0;
            dgvEmployee.Columns.Add(colChk);
            dgvEmployee.RowHeadersVisible = false;
        }

        private void btnGetChecked_Click(object sender, EventArgs e)
        {
            List<employee> lstEmp = new List<employee>();
            foreach (DataGridViewRow row in dgvEmployee.Rows)
            {
                DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(chk.Value) == true)
                {
                    lstEmp.Add(new Employee { FirstName = row.Cells[1].Value.ToString(), LastName = row.Cells[2].Value.ToString(), Salary =Convert.ToInt32(row.Cells[3].Value.ToString()) });
                }

            }

           
        }

</employee></employee>


这篇关于从datagridviewcheckboxcolumn获取不正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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