DataGridView复选框问题 [英] DataGridView Check Box Problem

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

问题描述

大家好,

我遇到C#代码问题.问题是我已经用DataGridView控件开发了一个表单.在每行最后一个单元格的网格视图中,我已定位了一个DataGridViewCheckBoxColumn.现在,当我选中复选框时,我希望在每行的DataGridViewTextBoxColumn中设置一些数据,而当我取消选中复选框时,来自每个文本框列的该行的数据将被清除.我可以在每一行中设置数据以选中该行的复选框,但是当我取消选中该复选框时,数据不会设置为空.您能帮我找到解决这个问题的方法吗?为了您更好的理解,我正在发送代码.请帮助我解决问题.

谢谢.

Hello everyone,

I am facing a problem with C# code. The problem is I have developed a form with DataGridView Control. In that grid view at the last cell of each row I have positioned a DataGridViewCheckBoxColumn. Now I wish to set some data in the DataGridViewTextBoxColumn of each row when I check the checkbox and when I uncheck the checkbox the data from that row of each text box column will be clear. I can set the data in each row to check the check box of that row but when I uncheck that check box the data is not set empty. Will you please help me to find the solution for this problem. For your better understanding I am sending the code. Please help me to fix the problem.

Thanks.

int clmnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (clmnIndex == 8)
            {
                DataGridViewCheckBoxCell chkBox = 
                    (DataGridViewCheckBoxCell)dgvAttendance.Rows[rowIndex].Cells[8];
                
                if (chkBox.Value == chkBox.TrueValue)
                {
                    if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)
                    {                        
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = DateTime.Now.ToString("MMM dd, yyyy");
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = DateTime.Now.ToString("HH:mm:ss");
                        dgvAttendance.Rows[rowIndex].Cells[7].Value = "CheckIn";
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = DateTime.Now.ToString("HH:mm:ss");
                    }
                }
                else if (chkBox.Value == chkBox.FalseValue)
                {
                    if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)
                    {
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[7].Value = string.Empty;
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = string.Empty;
                    }
                }



[edit]将内联代码"替换为代码块"以保留格式-OriginalGriff [/edit]



[edit]"inline code" replaced with "code block" to preserve formatting - OriginalGriff[/edit]

推荐答案

我认为您会发现"null"和"string.Empty".尝试使用string.IsNullOrEmpty(mystring)代替mystring == null,看看是否会改变.
I think you will find there is a difference between "null" and "string.Empty". Try using string.IsNullOrEmpty(mystring) instead of mystring == null and see if that changes things.


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

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