none
No difference output with or without clear method in DataBindings

    Question

  • Hello,

    When I comment Clear method of Data binding in my output no difference.

    I can't understand well how Clear method works.

     private void FillGrid(string query = "select * from phonebook")
            {
                //Fill GridView
                sqlCommand.CommandText = query;
                sqlCommand.Connection = sqlconnection;
    
                dataAdapter.SelectCommand = sqlCommand;
                dataAdapter.Fill(dataSet, "phone");
    
                //dataGridView1.DataBindings.Clear();
                dataGridView1.DataBindings.Add("datasource", dataSet, "Phone");
    
               // txtBoxName.DataBindings.Clear();
                txtBoxName.DataBindings.Add("Text", dataSet, "Phone.FirstName");
    
                txtBoxFamily.DataBindings.Add("Text", dataSet, "Phone.lastname");
    
    
            }

    Monday, February 05, 2018 6:12 AM

Answers

  • Hi Arash,

    Its role is reflected in multiple called the method, for example, if you add the Clear() method, you can call this method multiple times from another method, like:

            private void button1_Click(object sender, EventArgs e)
            {
                FillGrid();
            }

    You can always click the button1 without any problem, however, if you did not add the Clear() method, you just can click the button1 once, if you repeat it, then you will get the error like this:

    Please check it.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Monday, February 05, 2018 11:52 AM
    Monday, February 05, 2018 7:30 AM
    Moderator
  • Hi Arash,

    In fact, you do not need to call the Clear() method multiple times in the FillGrid(), you just need to call it once in the first line:

            private void FillGrid(string query = "select * from phonebook")
            {
                txtBoxName.DataBindings.Clear();
                dataGridView1.DataBindings.Clear();
                //Fill GridView
                //...
            }

    The DataBindings.Clear() method will clear the collection of any bindings.

    ControlBindingsCollection.Clear Method ()

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Wednesday, February 07, 2018 5:35 AM
    Tuesday, February 06, 2018 7:28 AM
    Moderator

All replies

  • Hi Arash,

    Its role is reflected in multiple called the method, for example, if you add the Clear() method, you can call this method multiple times from another method, like:

            private void button1_Click(object sender, EventArgs e)
            {
                FillGrid();
            }

    You can always click the button1 without any problem, however, if you did not add the Clear() method, you just can click the button1 once, if you repeat it, then you will get the error like this:

    Please check it.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Monday, February 05, 2018 11:52 AM
    Monday, February 05, 2018 7:30 AM
    Moderator
  • Thank you for your reply,

    We need call Clear method one time the end of each method or after when we use Data Binding method?

    Could you please more explain about how Clear Method work? DataBinding means create a connection(or thread) between Control and Data Source? 

    Monday, February 05, 2018 11:56 AM
  • Hi Arash,

    In fact, you do not need to call the Clear() method multiple times in the FillGrid(), you just need to call it once in the first line:

            private void FillGrid(string query = "select * from phonebook")
            {
                txtBoxName.DataBindings.Clear();
                dataGridView1.DataBindings.Clear();
                //Fill GridView
                //...
            }

    The DataBindings.Clear() method will clear the collection of any bindings.

    ControlBindingsCollection.Clear Method ()

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Wednesday, February 07, 2018 5:35 AM
    Tuesday, February 06, 2018 7:28 AM
    Moderator