C# Programm starts to freeze randomly when edeting one of the gridviews

noname 6 Reputation points
2022-02-06T21:52:16.427+00:00

I made this C# program and sometimes the window freezes when i edit something in the dataGridView.
Can anybody see a mistake in my programm?

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        public int Rowindex = 0;
        public int Columindex = 0;
        public Form1()
        {
            InitializeComponent();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataSetCD1.Clear();
            sqlDataAdapter1.Fill(dataSetCD1, "CD");
            dataSetCD21.Clear();
            sqlDataAdapter2.Fill(dataSetCD21, "CD");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataSetCD31.Clear();
            sqlDataAdapter3.SelectCommand.Parameters["@YEAR"].Value = comboBox1.SelectedValue;
            sqlDataAdapter3.Fill(dataSetCD31, "CD");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows != null)
            {
                foreach(DataGridViewRow dr in dataGridView1.SelectedRows)
                {
                    dataGridView1.Rows.Remove(dr);
                }
                sqlDataAdapter1.Update(dataSetCD1, "CD");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataSetCD1.WriteXml("Test.xml", XmlWriteMode.DiffGram);
        }

        public void clearDatabase()
        {
            sqlConnection1.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM CD WHERE 1 = 1", sqlConnection1);
            cmd.ExecuteNonQuery();
            sqlConnection1.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog df = new OpenFileDialog();
            if(df.ShowDialog() == DialogResult.OK)
            {
                dataSetCD1.Clear();
                clearDatabase();
                dataSetCD1.ReadXml(df.FileName);
                sqlDataAdapter1.Update(dataSetCD1, "CD");
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            sqlDataAdapter1.Update(dataSetCD1, "CD");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Rowindex = e.RowIndex;
            Columindex = e.ColumnIndex;
            if(Columindex == 1)
            {
                axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("TITLE");
            }else if(Columindex == 2)
            {
                axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("ARTIST");
            }
            else if (Columindex == 3)
            {
                axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("COUNTRY");
            }
            else if (Columindex == 4)
            {
                axInkEdit1.Text = dataSetCD1.Tables[0].Rows[Rowindex].Field<String>("COMPANY");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            client.GetData(textBox3.Text, textBox1.Text, textBox4.Text, textBox2.Text, Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown1.Value));
        }

        private void axInkEdit1_Change(object sender, EventArgs e)
        {
            if (Columindex == 1)
            {
                dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("TITLE", axInkEdit1.Text);
            }
            else if (Columindex == 2)
            {
                dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("ARTIST", axInkEdit1.Text);
            }
            else if (Columindex == 3)
            {
                dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("COUNTRY", axInkEdit1.Text);
            }
            else if (Columindex == 4)
            {
                dataSetCD1.Tables[0].Rows[Rowindex].SetField<String>("COMPANY", axInkEdit1.Text);
            }
            sqlDataAdapter1.Update(dataSetCD1, "CD");
        }
    }
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,626 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,628 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,836 Reputation points Microsoft Vendor
    2022-02-07T03:30:55.51+00:00

    Hi @noname ,
    It might be a threading issue.GUI elements are not allowed to be changed from any other than the GUI thread (the thread which created the corresponding window handle).
    If UI controls need to be accessed/modified from another thread, you must ensure that the controls are called in a thread-safe manner. Please use Control.Invoke.
    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invoke?view=windowsdesktop-6.0
    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.begininvoke?view=windowsdesktop-6.0
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly 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