Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
let guess datagridview loaded 10 data. When i clicked SelectALL 9 row checked true but first row not checked false.

even if
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;
                }
            }
        }
Posted

1 solution

We can't tell - we can't run your code with your data and see what is happening.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the ToggleSelectAll method, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
At a guess, the first row doesn't contain a DataGridViewCheckBoxCell so the test is not passing - but what it does contain we can't tell.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900