Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
example: if date column equals 2019-7-2 delete all row column date

Sorry for my English

What I have tried:

example: if date column equals 2019-7-2 delete all row column date

Sorry for my English
Posted
Updated 2-Jul-19 5:05am

1 solution

It's best to work from the underlying data rather than the DataGridView itself:
C#
private void butDelete_Click(object sender, EventArgs e)
    {
    if (MyDataGridView.DataSource is DataTable dt)
        {
        List<int> deleteThese = new List<int>();
        DateTime remove = new DateTime(2019, 2, 7);
        int index = 0;
        foreach (DataRow row in dt.Rows)
            {
            if (row["Appointment"] is DateTime apt && apt == remove)
                {
                deleteThese.Add(index);
                }
            index++;
            }
        foreach (int i in deleteThese)
            {
            dt.Rows[i].Delete();
            }
        }
    }
 
Share this answer
 
Comments
Member 14013003 2-Jul-19 12:24pm    
sorry Forgot I want for database mysql
OriginalGriff 3-Jul-19 3:37am    
Yes? And?
BillWoodruff 2-Jul-19 21:46pm    
Hi, you might want to indicate to the reader you are using the newer C# 7's pattern matching feature of the 'is operator.
OriginalGriff 3-Jul-19 3:37am    
I'm kinda getting to like that, after an initial reluctance. The scoping is a good idea, but it does still make it hard to see a definition of "dt", I must admit.
And being able to say this:

if (row["Appointment"] is DateTime apt && apt == remove)

Is handy.
BillWoodruff 3-Jul-19 4:14am    
Hi, I am slowly getting "in the zone" with the new features, although I have to make a conscious effort to recall them. I find it interesting that even in the latest version of VS 2019 Ent, and using the latest ReSharper: DGV.DataSource is string dt ... is not flagged pre-compilation.

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