为什么无法检查 datagridview C# 中的第一行我的代码结果除了第一行之外的所有数据?

编程


让我们猜测datagridview加载了10条数据。 当我单击 SelectALL 9 行时,检查为 true,但第一行未检查为 false。

即使

datagridview loaded only one data then i clicked SelectALL but row not selected

What I have tried:

private void ReportViewer_Load(object sender, EventArgs e)
        {
    grdEmailData.ColumnHeaderMouseClick += grdEmailData_ColumnHeaderMouseClick;
       
 }

private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();

                    string query = "SELECT * FROM email_notification WHERE bank_code = @BankCode AND stmdate = @StmDate " +
                                   "UNION " +
                                   "SELECT * FROM email_notification_arc WHERE bank_code = @BankCode AND stmdate = @StmDate";

                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@BankCode", _fiid); // Assuming _serverName is the bank code
                        command.Parameters.AddWithValue("@StmDate", dtpStmDate.Value.ToString("dd/MM/yyyy")); 

                        SqlDataAdapter adapter = new SqlDataAdapter(command);
                        DataTable dataTable = new DataTable();
                        adapter.Fill(dataTable);


                        DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
                        checkBoxColumn.HeaderText = "SelectALL";
                        checkBoxColumn.Name = "All";
                        grdEmailData.Columns.Insert(0, checkBoxColumn);


                        // Bind the data to the DataGridView
                        grdEmailData.DataSource = dataTable;

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

 private void grdEmailData_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // Check if the clicked cell is in the "All" column header
            if (e.RowIndex == -1 && e.ColumnIndex == grdEmailData.Columns["All"].Index)
            {
                ToggleSelectAll();
            }
        }


        private void ToggleSelectAll()
        {
            // Toggle the select/unselect all logic in the "All" column
            bool selectAll = true;

            foreach (DataGridViewRow row in grdEmailData.Rows)
            {
                
                DataGridViewCheckBoxCell checkBoxCell = row.Cells["All"] as DataGridViewCheckBoxCell;

                if (checkBoxCell != null)
                {
                    checkBoxCell.Value = selectAll;
                }
            }
        }

解决方案1

我们无法判断 – 我们无法使用您的数据运行您的代码并查看发生了什么。

所以,这将取决于你。
幸运的是,您有一个可用的工具可以帮助您了解正在发生的情况:调试器。 如果您不知道如何使用它,那么快速搜索“Visual Studio 调试器”应该可以为您提供所需的信息。

在 ToggleSelectAll 方法的第一行放置一个断点,然后通过调试器运行代码。 然后查看您的代码和数据并找出应该手动发生的情况。 然后单步执行每一行,检查您期望发生的情况是否确实发生。 如果不是,那就是你遇到了问题,你可以回溯(或再次运行它并更仔细地查看)以找出原因。
据猜测,第一行不包含 DataGridViewCheckBoxCell,因此测试未通过 – 但我们无法判断它包含什么。

抱歉,我们无法为您做到这一点 – 您是时候学习一项新的(并且非常非常有用)技能了:调试!

コメント

タイトルとURLをコピーしました