Bindingsource aus Form1 in Form 2 verwenden

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von VB1963.

    Bindingsource aus Form1 in Form 2 verwenden

    Hallo,

    wie ich Vb.net die Bindingsource einer Form auf die Bindingsource einer anderen Form umstöpsle, weiß ich und das klappt auch einwandfrei.

    Allerdings muss ich jetzt auf Befehl von oben ein Programm, das in vb.net einwandfrei läuft, in c# aufbauen.

    Form 1, auszugsweise:

    C#-Quellcode

    1. private void button2_Click(object sender, EventArgs e)
    2. {
    3. Form2 form2 = new Form2(this);
    4. form2.Show();
    5. }


    Form 2:

    C#-Quellcode

    1. public partial class Form2 : Form
    2. {
    3. private Form1 form1;
    4. Form1 Form1 = null;
    5. public Form2(Form1 form1)
    6. {
    7. this.form1 = form1;
    8. InitializeComponent();
    9. }


    Das stellt, wenn ich es richtig verstanden habe, die Felder auf Form 1 in Form2 zur Verfügung.

    Allerdings komme ich auf diese Weise auf Form2 nicht an die Bindingsource auf Form1 ran.

    Füge ich auf Form1 dies hinzu:

    C#-Quellcode

    1. public BindingSource bew_BindingSource = new BindingSource();


    ist die Bindingsource von Form 1 in Form2 erreichbar.

    Aber das führt dann dazu, dass mich der Compiler auf Form1 hier anmeckert:

    C#-Quellcode

    1. this.bew_BindingSource.AddNew();


    mit der Meldung, dass Mehrdeutigkeit zwischen den Bindingsourcen vorliegt.

    An dieser Stelle bräuchte ich jetzt ein wenig Hilfe.

    Vielen Dank,
    Volkmar
    Probier's einfach so:

    C#-Quellcode

    1. ​public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. private void button1_Click(object sender, EventArgs e)
    8. {
    9. Form2 frm = new Form2();
    10. frm.dataTable1BindingSource.DataSource = this.dataSet1;
    11. frm.Show(this);
    12. }
    13. }
    14. }
    Die Bindingsource der Form2 muß ev. per FormsDesigner bei der Modifires-Eigenschaft noch auf Public umgestellt werden...
    Vielen Dank, funktioniert, bis auf einen Punkt:

    C#-Quellcode

    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. Form2 frm = new Form2();
    4. frm.dataTable1BindingSource.DataSource = this.dataSet1;
    5. frm.Show(this);
    6. }


    wie von Dir vorgeschlagen, führt zu einer Null Reference Exception.

    C#-Quellcode

    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. Form2 frm = new Form2(this);
    4. frm.dataTable1BindingSource.DataSource = this.dataSet1;
    5. frm.Show(this);
    6. }


    oder

    C#-Quellcode

    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. Form2 frm = new Form2(this);
    4. frm.dataTable1BindingSource.DataSource = this.dataSet1;
    5. frm.Show();
    6. }


    funktioniert. Dann landen die Eingaben aus Form2 in Form1.

    Gruß, Volkmar