C#DataGridView复选框检查事件 [英] C# DataGridView Checkbox checked event

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

问题描述

我是c#新手,制作一个预订我的房间应用程序,为旅客提供住宿。此屏幕可以很好地解释我想要实现的目标;



这是一个计算员工每小时工资的软件的.GIF:





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;

我使用此添加了复选框列;

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

每当用户从登录屏幕登录时,新行将被插入到表中, dgv将被更新,具有相应的用户条目。
我不明白如何链接这些复选框与datagridview我尝试celldirtystatechanged但没有什么作用,将正确的方式将行与复选框相关联。

解决方案

您可以处理 DataGridView 的 改变这些细胞。



要点是使用 CommitEdit(DataGridViewDataErrorContexts.Commit) 将当前单元格中的更改提交到数据高速缓存而不结束编辑模式。这样,当您检查此事件中的单元格的值时,它将返回您当前在单击后显示的当前选中或未选中的值:

  private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
//我们使DataGridCheckBoxColumn提交更改单击
//使用注销列索引
if e.ColumnIndex == 4& e.RowIndex> = 0)
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

//检查单元格
的值if((bool)this.dataGridView1.CurrentCell.Value == true)
{
//使用TimeOut的索引column
this.dataGridView1.Rows [e.RowIndex] .Cells [3] .Value = DateTime.Now;

//设置其他列的值
}
else
{
//使用TimeOut列的索引
this.dataGridView1.Rows [ e.RowIndex] .Cells [3] .Value = DBNull.Value;

//设置其他列值
}
}


I am new to c# and making a "book my room" app for a place which provides guest housing to travelers. This screen may explain well what I wish to achieve;

This is a .GIF of a software which calculates hourly pay of an employee:

This is a .GIF of a software which calculates hourly pay of an employee

This Photo is an illustration of what actually I want to build:

Code for displaying my table in DataGridView is:

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. 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.

解决方案

You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there.

The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. 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
    }
}

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

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