[英]How to save checked status for checkbox in data grid view c# windows application
我在C#Windows应用程序中工作,从SQL服务器到数据网格视图填充记录,每行都有动态复选框功能。 我想通过该特定行的复选框为某些目的选择选定的行。 到现在为止,我成功实现了目标。 但是我在保存已检查状态方面遇到了一个小问题,示例我只想检查那些名称= Max的记录,我在该文本框中有一个文本框,我用类似Query的方式调用了chnage事件
按名称过滤的代码:
try
{
SqlCommand cmd = null;
SqlConnection con = null; Ranks rank = new Ranks();
con = new SqlConnection(cs.DBcon);
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "Select * from Records where Name like @Name order by Pno";
cmd.Parameters.AddWithValue("@Name", "%" + FilterByNameTextbox.Text.Trim() + "%");
SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter1.Fill(dt);
dataGridView1.DataSource = dt;
Make_fields_Colorful();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
如果我在名称过滤器中输入Max,它将返回3条记录,其中名称以max开头,使用类似我上面提到的查询的查询。 所以我只使用动态复选框检查3条记录中的2条记录,直到现在我的代码都可以完美运行。 现在,我想检查名称从Ali开始的记录,现在当我在名称过滤器中通过名称文本框写ali时,它将返回行,其中名称类似于ali,但问题出在这里,它将删除我之前检查过的记录,所以我将如何保存max和ali的行的检查记录:
在每行中添加动态复选框的代码
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.Name = "checkBoxColumn";
checkBoxColumn.DataPropertyName = "Report";
checkBoxColumn.HeaderText = "Report";
dataGridView1.Columns.Insert(10, checkBoxColumn);
dataGridView1.RowTemplate.Height = 100;
dataGridView1.Columns[10].Width = 50;
为了坚持使用复选框选择,您需要为每一行保存复选框状态。
第1步
在Record
表中添加新列。 例如。 CheckState of BIT
(此处是存储布尔值的BIT
SQL Server列类型)
第2步
现在添加代码,以利用DataGridView
的CurrentCellDirtyStateChanged
事件来拦截复选框状态更改。 下面是示例代码;
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
bool checkVal = (Boolean)dataGridView1.CurrentCell.EditedFormattedValue; // Check box state value
int checkBoxState = checkVal ? 1 : 0; // 1 - TRUE, 0 - FALSE
// Now save the check box state change in the database table.
// You would need a key field to distinguish the record in the DB table
// As per the code you had provided you could do something similar to below, assuming that the Pno column is the key field/ unique
int keyValue = (int)dataGridView1.CurrentRow.Cells["Pno"].Value;
// Save the changes by passing checkBoxState and keyValue accordingly
SetNewUploadFileForGrantAppID(checkBoxState, keyValue);
}
}
调用SQL Server存储过程的函数。
private void SetNewUploadFileForGrantAppID(int pIntRecordKeyValue, int pIntCheckBoxStatus)
{
SqlConnection connection = new SqlConnection(cs.DBcon);
try
{
SqlCommand sqlCommand = connection.CreateCommand();
sqlCommand.CommandText = "dbo.usp_SetRecordCheckStatus";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@pIntRecordKeyValue", pIntRecordKeyValue);
sqlCommand.Parameters.AddWithValue("@pIntCheckBoxStatus", pIntCheckBoxStatus);
connection.Open();
sqlCommand.ExecuteScalar();
}
catch (Exception ex)
{
//LogError(ex);
}
finally
{
connection.Close();
connection.Dispose();
}
}
第三步
创建SQL Server存储过程以保存更改
例如。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_SetRecordCheckStatus]
@pIntRecordKeyValue INT,
@pIntCheckBoxStatus INT
AS
BEGIN
SET NOCOUNT ON;
UPDATE dbo.Record
SET CheckState = @pIntCheckBoxStatus
WHERE Pno = @pIntCheckBoxStatus
END
GO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.