【问题标题】:How to save checked status for checkbox in data grid view c# windows application如何在数据网格视图c#windows应用程序中保存复选框的选中状态
【发布时间】:2019-02-28 05:41:31
【问题描述】:

我正在使用 c# windows 应用程序将记录从 sql server 填充到数据网格视图,每行中都有动态复选框工具。我想通过该特定行的复选框出于某种目的选择选定的行。直到现在我成功地实现了我的目标。但是我在保存检查状态时遇到了一个小问题,例如,我只想检查那些 Name = Max 我在该文本框中有一个文本框的记录,我用类似 Query 调用 text 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;

【问题讨论】:

标签: c# windows checkbox save


【解决方案1】:

为了保持复选框选择,您需要保存每一行的复选框状态。

第 1 步

Record 表中添加新列。 例如。 CheckState of BIT(这里是BIT存储布尔值的SQL Server列类型)

第 2 步

现在添加代码以利用DataGridViewCurrentCellDirtyStateChanged 事件拦截复选框状态更改。 下面是示例代码;

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();
        }
    }

第 3 步

创建SQL Server Stored Procedure 以保存更改

例如。

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

【讨论】:

  • 尊敬的先生,谢谢您的代码,我是 C# 新手,请告诉我如何在数据库表中保存复选框状态更改。以及如何通过相应地传递 checkBoxState 和 sKeyValue 来保存更改
  • bool checkVal = (Boolean)CustomersList.CurrentCell.EditedFormattedValue;在上面的行中,CustomersList 是什么?
  • 抱歉改成dataGridView1
  • 对不起,我不能帮你做代码。不要放弃并让自己失望,尝试阅读有关编程的文章/资源,您将能够掌握编码C# TutorialC# Fundamentals for Absolute Beginners 的窍门。祝你好运!
  • 赞成鼓励问题作者站起来。这个问题被重复问了一遍,其他一些可怜的草皮也在帮助解决同样的问题。
猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多