Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I have two datagridviews-- Datagridview1 and datagridview3 (datagridview2 i use for something else)
And one button remove row on the Form.
if i have multiple rows in datagridview3 and hit the button the rows are removing one by one.
if the datagridview3 is empty you will be asked to save a file.
That works well.
So far so good.
But if i use the datagridview1 and i have also rows in it.
And i use the button remove row the rows are also removing one by one.
until the datagridview1 is completely empty.
Now i get also the question to save the file.
And that's is what i don't want with datagridview1.
I want this option only working on datagridview3.
regards

What I have tried:

C#
private void Bt_Remove_Row_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView3.SelectedRows)
            {
                dataGridView3.Rows.RemoveAt(row.Index);
            }
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                dataGridView1.Rows.RemoveAt(row.Index);
            }

            if (dataGridView1.Rows.Count == 0)

            {
                Bt_Remove_Row.Enabled = false;
                Bt_Print.Enabled = false;
                Bt_Clear_List.Enabled = false;

                if (dataGridView3.Rows.Count == 0)

                {
                    Bt_Remove_Row.Enabled = false;
                    Bt_Print.Enabled = false;
                    Bt_Clear_List.Enabled = false;
                    Bt_Borrowd.Enabled = false;
                    Bt_Save_Borrowd.Enabled = false;

                    DialogResult dialogresult = MessageBox.Show("WEET U HET ZEKER", "DE LIJST UITGELEEND GEREEDSCHAP WORD LEEG GEMAAKT", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                    if (dialogresult == DialogResult.Yes)
                    {
                        File.WriteAllText(UitleenBestand, String.Empty);
                        MessageBox.Show("BESTAND UITGELEEND GEREEDSCHAP IS AAN GEMAAKT", "BESTAND!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Lb_Uitleen.Text = ("");
                    }
                    if (dialogresult == DialogResult.No)
                        Bt_Borrowd.Enabled = true;
                }
                else
                {
                    Bt_Remove_Row.Enabled = true;
                    Bt_Clear_List.Enabled = true;
                    Bt_Print.Enabled = true;
                }
            }
        }
Posted
Updated 23-Nov-21 7:58am
v2

1 solution

1) on button press: remove selected rows in both dataGridView1 and dataGridView3

2) if dataGridView3 has #0 rows: show 'save dialog

private void Bt_Remove_Row_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView3.SelectedRows)
    {
        dataGridView3.Rows.RemoveAt(row.Index);
    }


    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        dataGridView1.Rows.RemoveAt(row.Index);
    }

    if (dataGridView3.Rows.Count == 0)
    {
        Bt_Remove_Row.Enabled = false;
        Bt_Print.Enabled = false;
        Bt_Clear_List.Enabled = false;
        Bt_Borrowd.Enabled = false;
        Bt_Save_Borrowd.Enabled = false;

        DialogResult dialogresult = MessageBox.Show("WEET U HET ZEKER", "DE LIJST UITGELEEND GEREEDSCHAP WORD LEEG GEMAAKT", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

        if (dialogresult == DialogResult.Yes)
        {
            File.WriteAllText(UitleenBestand, String.Empty);
            MessageBox.Show("BESTAND UITGELEEND GEREEDSCHAP IS AAN GEMAAKT", "BESTAND!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Lb_Uitleen.Text = ("");
        }

        if (dialogresult == DialogResult.No)
            Bt_Borrowd.Enabled = true;
    }
    else
    {
        Bt_Remove_Row.Enabled = true;
        Bt_Clear_List.Enabled = true;
        Bt_Print.Enabled = true;
    }   
}
 
Share this answer
 
v3
Comments
[no name] 23-Nov-21 12:03pm    
hello
Thanks fo you answer.
but this is not what i want.
i want also delete rows in datagridview1.
i just don't want the file to be saved when datagridview1 is empty.
BillWoodruff 23-Nov-21 12:21pm    
See revised code above.
[no name] 23-Nov-21 12:50pm    
hello
If i use code above.
And i press the button remove row.
Then comes already at one removed row the question do you want to save the file.
In my code the question provide if de datagrid1 is empty.
BillWoodruff 23-Nov-21 23:01pm    
The revised code triggers the Dialog only when dataGridView3 has #0 rows. I that not what you asked for ?
[no name] 24-Nov-21 11:53am    
Hello
No what i mean is.
I have two data grids 1 and 2
I can fill this with data whit a button.
I datagrid1 is treu datagrid3 is false and vica-versa.
I only have 1 button remove row for both data grids.
I want to be able to empty these datagrids separately from line to line.
Until they are empty.
And then I don't want to be asked at datagrid1 if i want save the file.
With datagrid3 I want to be able to save the file.
in my code this now happens on both datagrids that i am asked tho save the file.
I hope you understand what I mean.
Tanks for helping me.

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