Autor Beitrag
highlander78
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 62



BeitragVerfasst: So 16.05.10 20:56 
Hallo,

so fülle ich ein DataGridView mit Daten.
Das alles funktioniert aber nur ein einziges mal.
Wenn ich das 2te mal auf den Button drücke um die Daten zu aktualisieren bekomme ich eine Fehlermeldung.

Zitat:
Ungültiger threadübergreifender Vorgang: Der Zugriff auf das Steuerelement dataGridView1 erfolgte von einem anderen Thread als dem Thread, für den es erstellt wurde.


Die Lösung soll Invoke sein...
Ich verstehe das aber nicht wirklich.. wie soll ich das mit meinem Code lösen..


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
public MainForm()
    {
    
      InitializeComponent();
        
    }
    
    void Button1Click(object sender, EventArgs e)
    {
            textBox2.Text = " Hole Daten";
         
            backgroundWorker1.RunWorkerAsync();
  
    }

        private void LoadData()
        {
            dataGridView1.DataSource = bindingSource1;
            textBox2.Text = " Fertig";
            
        }


        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        
            {

                SqlConnection conn1 = new SqlConnection("SERVER = TOSHIBA-PC\\SQLEXPRESS;" +
                                                        "UID=sa;" +
                                                        "PWD=test;" +
                                                        "DATABASE=SiplaceOIS");
                SqlCommand cmd1 = new SqlCommand(" select * from dbo.V_PICKUPERROR6", conn1);
                SqlDataAdapter adapter1 = new SqlDataAdapter();
                adapter1.SelectCommand = cmd1;

                DataSet ds1 = new DataSet();
                adapter1.Fill(ds1);

                DataTable table1 = new DataTable();
                adapter1.Fill(table1);
                bindingSource1.DataSource = table1;

            } 


        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {

            LoadData();
         
        }



Gruss
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: So 16.05.10 21:22 
Hallo,

nur beim zweiten klick auf den Button? Das kann ich mir nicht erklären.

Den Invoke benötigst du weil du mit dem BackgroundWorker ein Thread übergreifenden Zugriff auf ein Control machst (wenn du das RunWorkerCompleted-Event bekommst). Diese müssen synchronisiert werden. Dies geschiet mittels Invoke. Beispiele gibt es dazu genug. Müsstest du an Zeile 17 machen.

Gruß
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 16.05.10 22:08 
user profile icondanielf hat folgendes geschrieben Zum zitierten Posting springen:
Diese müssen synchronisiert werden.
Und dazu ist im einfachsten Falle ein BackgroundWorker da, oder ;) ? Dafür muss aber die Zeile bindingSource1.DataSource = table1; ebenfalls in den Completed-Handler.

_________________
>λ=
highlander78 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 62



BeitragVerfasst: Mo 17.05.10 07:30 
Hallo,

ja aber nur die Zeile bindingSource1.DataSource = table1; in den Completed-Handler funktioniert ja nicht.
Oder soll ich alles aus dem Do-Work-Handler in den Completed-Handler verschieben.
Wie gesagt beim ersten klick auf den Button funtzt alles , nur beim 2ten klick nicht mehr.


Gruss
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Mo 17.05.10 08:35 
Die LoadData in Zeile 42. verschieben?
highlander78 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 62



BeitragVerfasst: Mo 17.05.10 11:07 
Habe das so gelöst , so funktioniert es auf jeden Fall.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
public partial class MainForm : Form
  {
        DataTable table1 = new DataTable();

        public MainForm()
        {
            InitializeComponent();
        }

        void Button1Click(object sender, EventArgs e)
        {
         
            textBox2.Text = " Hole Daten";
          
            backgroundWorker1.RunWorkerAsync();

        }

        private void LoadData()
        {
            bindingSource1.DataSource = table1;
            dataGridView1.DataSource = bindingSource1;
            textBox2.Text = " Fertig";

        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Thread.Sleep(1500);
            SqlConnection conn1 = new SqlConnection("SERVER = TOSHIBA-PC\\SQLEXPRESS;" +
                                                    "UID=sa;" +
                                                    "PWD=wiesel;" +
                                                    "DATABASE=SiplaceOIS");
            SqlCommand cmd1 = new SqlCommand(" select * from dbo.V_PICKUPERROR6", conn1);
            SqlDataAdapter adapter1 = new SqlDataAdapter();
            adapter1.SelectCommand = cmd1;

            DataSet ds1 = new DataSet();
            adapter1.Fill(ds1);
            adapter1.Fill(table1);
           
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
          
            LoadData();

        }
    }
}