Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用textbox筛选记录时维护复选框的状态_C#_Winforms - Fatal编程技术网

C# 使用textbox筛选记录时维护复选框的状态

C# 使用textbox筛选记录时维护复选框的状态,c#,winforms,C#,Winforms,我正在开发一个C windows应用程序来填充从SQL Server到数据网格视图的记录,每行都有动态复选框功能。我想通过特定行的复选框出于某种目的选择所选行。到目前为止,我成功地实现了我的目标,但我面临一个关于保存已检查状态的小问题 例如,我只想检查那些Name=Max的记录。在该文本框中有一个文本框,我使用like查询调用文本更改事件: try { SqlCommand cmd = null; SqlConnection con = null; Ra

我正在开发一个C windows应用程序来填充从SQL Server到数据网格视图的记录,每行都有动态复选框功能。我想通过特定行的复选框出于某种目的选择所选行。到目前为止,我成功地实现了我的目标,但我面临一个关于保存已检查状态的小问题

例如,我只想检查那些Name=Max的记录。在该文本框中有一个文本框,我使用like查询调用文本更改事件:

 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开头,使用类似的查询,正如我上面提到的代码。所以我只使用dynamic checkbox检查了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;
图像:


我建议您通过缓存选定的行来实现这一点,首先您应该有一个缓存行列表:

List<DataGridViewRow> CachedRows = new List<DataGridViewRow>();
处理程序应检查更改的列是否为复选框,并应如下所示:

dataGridView1.CellValueChanged += view_CellValueChanged;
try
{
    if(e.ColumnIndex == indexOfCheckBoxColumn)
    {
       if((bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == true)
       {
        CachedRows.Add((DataGridViewRow)dataGridView1.Rows[e.RowIndex].Clone());
       }
       else if (CachedRows.Contains(dataGridView1.Rows[e.RowIndex]))//Validate if this works, if not you should associate each row with unique key like for example (id) using a dictionary
       {
          CachedRows.Remove(dataGridView1.Rows[e.RowIndex]);
       }
    }
}
catch(Exception ex)
{
}
然后在过滤器更改后,再次添加缓存行,使代码变为:

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;
        //add folowing
        if (CachedRows.Any())
        {
            dataGridView1.Rows.AddRange(CachedRows.ToArray());
            CachedRows.Clear();
        }
        Make_fields_Colorful();
    }
    catch (Exception exception)
    {
        MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }

谢谢您的帮助,请让我知道在哪里添加此行?dataGridView1.CellValueChanged+=视图\u CellValueChanged;当我添加以下行ifdataGridView1.Rows[e.RowIndex]。Cells[e.ColumnIndex]。Value==true它将给出错误运算符“==”不能应用于“object”和“bool”类型的操作数。我更改ifdataGridView1.Rows[e.RowIndex]。Cells[e.ColumnIndex]。Value==true到if booldataGridView1.Rows[e.RowIndex]。Cells[e.ColumnIndex].Value now Value come null这就是为什么它通过错误对象引用未设置为对象的实例。@MuhammadTanzeelars尝试添加dataGridView1.CellValueChanged+=view\U CellValueChanged在初始化dataGridView1=new dataGridView1后,我建议用try catch包装事件处理程序我成功添加事件处理程序,但是当记录添加到cachedRows中时,我遇到了一个错误,其中一些行的索引为-1,这就给出了错误这个问题,我投票将另一行搁置。