i want to delete a line in datagridview in windows forms

Andriesvc 1 Reputation point
2022-11-29T23:20:08.313+00:00

Hello!

I have a multiline texbox, and would like delete lines if it's contain some string.

Example

Line 1 abababalanlndw

Line 2 aaaaakkk knlnflef baby efqwf

Line 3 fwelfnwaofna

i only want to delete line 2
wich code should i implement in my delete button

thanks in advance

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,924 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,046 Reputation points Microsoft External Staff
    2022-11-30T01:49:15.957+00:00

    @Andriesvc , Welcome to Microsoft Q&A, you could try to convert your textbox's text to a string array.

    Then you could use string. Replace method to remove the line.

    Here is a code example you could refer to.

     private void button1_Click(object sender, EventArgs e)  
            {  
                string[] arr = textBox1.Text.Split('\r');  
                foreach (var item in arr)  
                {  
                    if(item.Contains("baby"))  
                    {  
                        textBox1.Text = textBox1.Text.Replace(item, "");  
                    }  
                }  
            }  
    

    Result:

    265513-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.