Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Folks

Another issue I have as I am working on winform task where I have 5 columns and 1st column is for checkbox .The issue is here If there are more than 10 rows/records than I am unable to select checkbox properly it just moves up and down and do not let me to mark check properly .I can select checkbox for 1st row but for second row and onwards facing difficulty . And also if I start selecting from bottom I can select untill 3rd row again it does not let me .If I have less than 10rows like 8 or 9 rows than I can select checkbox  from top ,middile or bottom easily .Please help me whta to check and how .It is very peculiar for me to fix it .

What I have tried:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true)) //0 is the column number of checkbox
                {
                    row.Selected = true;
                    BtnCancel.Enabled = false;
                    row.DefaultCellStyle.SelectionBackColor = Color.LightSlateGray;

                }
                else
                    row.Selected = false;
                BtnCancel.Enabled = true;
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }


    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0) return;
            dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
        }


  private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
            { return; }
            else { cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; }
           
            // For this demo we will make exception for checkBoxColumn itself.
            // So it is always editable.
            if (e.ColumnIndex != chkbox.Index)
            {

                object rawValue = dataGridView1[chkbox.Index, e.RowIndex].Value;
                bool value = rawValue is bool ? (bool)rawValue : false;
                // But if checkBoxColumn is checked then editing not allowed
                if (!value)
                {
                    e.Cancel = true;
                }
            }
            }
Posted
Updated 16-Jun-22 19:35pm
v2

Without your data and your code running as you have it, we can't really tell when is going on at all.

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 CellClick handler 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.

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
 
Change:
else
    row.Selected = false;
BtnCancel.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;

Into:
else
{
    row.Selected = false;
    BtnCancel.Enabled = true;
    btnUpdate.Enabled = true;
    btnDelete.Enabled = true;
}
 
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