0

I have experience with ASP, but completely new to WinForms. What I'm trying to do is this: I have a CheckedListBox on Form1 that is linked to a binding source that is linked to a SQL database. I have another Form (Form2) that pops up upon clicking on an "Add" button, and then they can add a record. What I would like to have happen is as soon as they Add the record in Form 2, the CheckedListBox in Form1 updates as well. I've tried DataSource and DisplayMember properties but that doesn't seem to do the trick. Can someone point me in the right direction?

If anyone knows of a better way to do this, I'm also all ears. Thanks.

NwkProg
  • 87
  • 1
  • 6

3 Answers3

0

Can you access Form1 from Form2? If you can the easiest way would be to just ad a line in button pressed that checks the check-box

something like form1.checkboxName.Checked = true;

it would help if you showed some of your code :)

Olle89
  • 668
  • 7
  • 22
  • I don't want to make it checked, I just want to make it show up in the list, so then the user can check all that they want. Some code: On Form 1 - chkList1.DataSource = bindingSource1; chkList1.DisplayMember = "CategoryName"; On Form 2 - (on the Add click event using OleDB) cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "Insert Command"; con.Open(); cmd.ExecuteNonQuery(); con.Close(); cmd.Dispose(); this.Close(); Do I have to add it through the BindingSource on Form1 and not straight to the DB? – NwkProg Jun 16 '11 at 14:31
  • very bad practice to access another form's controls from outside. if really something similar is needed, the control should be accessed inside a public property get method of the hosting form. – Davide Piras Jun 17 '11 at 18:17
0

Take a look at a previous answered post.

I directed the person to call the second form with a parameter from the first... Then the second form can utilize it directly. You could do this by passing in the first form itself and have it do almost anything... but in its simplest form, allow you to preserve the form. Then, in the click / add / save / whatever of the second form, reference the first form and set it directly (or indirectly via a public property getter/setter).

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
0

I figured out the answer to my question. I handled everything on form1. I created a method called refreshData, and when I clicked my icon to pop up form2, I waited for the Dialog Result of form2 to be OK, then I called the refreshData method. Hope this helps someone else:

private void pictureBox1_Click(object sender, EventArgs e)
    {
        form2 box = new form2();
        using (box)
        {
            box.ShowDialog();
            if (box.DialogResult == DialogResult.OK)
            {
                refreshData();
            }
            box.Dispose();
        }
    }
    private void refreshData()
{
ADODB.Recordset rs = new ADODB.Recordset();
           ADODB.Connection adoCon = new ADODB.Connection();
           adoCon.Open("put Connection String Here");
           rs.Open("Put Select query Here",adoCon,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockOptimistic);
           DataSet myDS = new DataSet();
           OleDbDataAdapter da = new OleDbDataAdapter();
           da.Fill(myDS, rs,"MyTable");
           chkList1.DataSource = null;
           chkList1.DataSource = myDS.Tables[0];
           chkList1.DisplayMember = "Put Field to Display in CheckList here";
}
NwkProg
  • 87
  • 1
  • 6