简体   繁体   English

C#DataGridView复选框已选中事件

[英]C# DataGridView Checkbox checked event

I want to handle Checked event of CheckBox columns in my DataGridView and perform an operation based on column checked value (true/false). 我想在我的DataGridView处理CheckBox列的Checked事件,并基于列检查值(true / false)执行操作。 I tried to use CellDirtyStateChanged without any success. 我试图使用CellDirtyStateChanged没有任何成功。 In fact I want to detect checked change immediately after the user checks or unchecks the check box. 实际上,我想在用户选中或取消选中复选框后立即检测选中的更改。

Here is a description about my application. 这是关于我的应用程序的描述。 I am new to c# and making a "book my room" app for a place which provides guest housing to travelers. 我是C#的新手,为要为旅客提供住宿的地方制作了“预订我的房间”应用。 This screen may explain well what I wish to achieve; 该屏幕可以很好地说明我希望实现的目标;

This is a .GIF of a software which calculates hourly pay of an employee and this photo is an illustration of what actually I want to build: 这是一个软件.GIF,它可以计算员工的小时工资,这张照片说明了我实际想要构建的内容: 这张照片说明了我实际想要构建的内容

Code for displaying my table in DataGridView is: DataGridView显示我的表的代码是:

OleDbConnection connection = new OleDbConnection();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select id,cusid,cusname,timein,
timeout,duration,amount,remark from entry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

I added the checkbox column using this; 我使用此添加了复选框列;

DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "logout";
checkColumn.HeaderText = "Logout";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
dataGridView1.Columns.Add(checkColumn);

Whenever a user logs in from the login screen a new row will be inserted in the table and hence the dgv will be updated, with corresponding users entry. 每当用户从登录屏幕登录时,都会在表中插入新行,因此dgv将更新,并带有相应的用户条目。 I don't understand how to link those checkboxes with datagridview I tried celldirtystatechanged but nothing works, what would be the right way to associate the row with checkbox. 我不明白如何将那些复选框与我尝试了celldirtystatechanged的datagridview链接,但没有任何效果,将行与复选框相关联的正确方法是什么?

You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there. 您可以处理DataGridView CellContentClick事件,并在其中放置用于更改这些单元格的逻辑。

The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. 关键是使用CommitEdit(DataGridViewDataErrorContexts.Commit)将当前单元格中的更改提交到数据缓存,而无需结束编辑模式。 This way when you check for value of cell in this event, it returns current checked or unchecked value which you see in the cell currently after click: 这样,当您在此事件中检查单元格的值时,它将返回您单击后当前在单元格中看到的当前选中或未选中的值:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    //We make DataGridCheckBoxColumn commit changes with single click
    //use index of logout column
    if(e.ColumnIndex == 4 && e.RowIndex>=0)
        this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

    //Check the value of cell
    if((bool)this.dataGridView1.CurrentCell.Value == true)
    {
        //Use index of TimeOut column
        this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;

        //Set other columns values
    }
    else
    {
        //Use index of TimeOut column
        this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DBNull.Value;

        //Set other columns values
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM