locked
bindingSource Question / Problem RRS feed

  • Question

  • I'm not sure completely how to explain my problem, so if anyone has any questions, please do let me know.

    Basically, I have a form with a dataGridView, which loads from a database.  I have it set up so that I can open another form with detailed information about the selected item in the dataGridView using a bindingSource like so:

    Code Block

                Form2 form = new Form2("Edit", this.bindingSource1);
                form.Show();


    In the Form2 Constructor I have the following:

    Code Block

    public Form2(String Option, BindingSource bs)
            {
                InitializeComponent();
                OptionTextBox.Text = Option;
                if (OptionTextBox.Text == "Edit")
                {
                    try
                    {
                        this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", bs, "#", true));
                    }
                    catch
                    {
                       
                    }
                }


    My problem is that I would like to open Form2 from somewhere else in my application, but this time instead of using a BindingSource, just set the .Text property of textBox3 from the .Text property of a textBox on the form I am opening Form2 from.  VS doesn't seem to allow me to do this, as it is looking for the BindingSource, which isn't there.  I understand that, but am wondering how then I might be able to do what I want...

    If anyone can help me out I would really appreciate that, as I'm not sure at all how I could go about doing this...

    Thank You.



    Tuesday, December 4, 2007 8:17 PM

Answers

  • You can define more that one constructor for Form2.  Make another constructor that takes only one string argument.  Make sure that the additional constructor has the the call to InitializeComponent().  Here is a dummy example for you:

     

    Form2:

     

    Code Block

        public partial class Form2 : Form
        {
            public Form2(string Options, BindingSource bs)
            {
                InitializeComponent();
                //Your original stuff here
            }

            public Form2(string Options)
            {
                InitializeComponent();
                label1.Text = Options;
            }
        }

     

     

     

    In Form1 there are two ways to call Form2:

     

    Code Block

            private void button1_Click(object sender, EventArgs e)
            {
                BindingSource myBindingSource = new BindingSource();
                Form2 f2 = new Form2("Edit", myBindingSource);
                f2.Show();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2(textBox1.Text);
                f2.Show();
            }

     

     

    Tuesday, December 4, 2007 9:05 PM

All replies

  • Oh, I forgot.  I tried adding the following in the constructor:

    String FromText

    in the Catch block:

    textBox3.Text = FromText;

    But that didn't work either.  It is still failing at the try block looking for the bindingSource...  Shouldn't it be moving on to the Catch block?

    I'm confused...
    Tuesday, December 4, 2007 8:24 PM
  • Here's a snippet to filter a bindingsource.

     

    Code Block

    class Class4

    {

    public static void SetBindingSourceFilter(

    BindingSource bs,

    string fieldSortBy,

    string filterAsSorted,

    bool sort,

    bool like)

    {

    string field = SetFieldString(fieldSortBy);

    string filter = SetFilterString(filterAsSorted);

    string direction = SetSortString(sort);

    string condition = SetFilterCondition(like);

    bs.Sort = field + direction;

    bs.Filter = field + condition + filter;

    }

    public static string SetSortString(bool sort)

    {

    string ascending = " ASC"; // Control the sort.

    string descending = " DESC"; // Control the sort.

    string direction = " "; // Default to no sorting.

    switch (sort)

    {

    case true:

    direction = ascending;

    break;

    case false:

    direction = descending;

    break;

    default:

    break;

    }

    return direction;

    }

    public static string SetFilterCondition(bool like)

    {

    // set up filter conditions.

    string condition = " LIKE "; // spaces are important

    return condition;

    }

    public static string SetFieldString(string fieldSortBy)

    {

    // verify field actually exists.

    string field = fieldSortBy;

    return field;

    }

    public static string SetFilterString(string fieldSortBy)

    {

    // this code could build a new string

    // this uses a default wildcard, * , for all values

    string filter = " '*' ";

    return filter;

    }

    }

     

     

    Filter your bindingsource when you open your new form.  Call SetBindingSourceFilter during the Form_Load event.

    Hope this helps.

     

    Rudedog

     

     

    Tuesday, December 4, 2007 9:05 PM
  • You can define more that one constructor for Form2.  Make another constructor that takes only one string argument.  Make sure that the additional constructor has the the call to InitializeComponent().  Here is a dummy example for you:

     

    Form2:

     

    Code Block

        public partial class Form2 : Form
        {
            public Form2(string Options, BindingSource bs)
            {
                InitializeComponent();
                //Your original stuff here
            }

            public Form2(string Options)
            {
                InitializeComponent();
                label1.Text = Options;
            }
        }

     

     

     

    In Form1 there are two ways to call Form2:

     

    Code Block

            private void button1_Click(object sender, EventArgs e)
            {
                BindingSource myBindingSource = new BindingSource();
                Form2 f2 = new Form2("Edit", myBindingSource);
                f2.Show();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2(textBox1.Text);
                f2.Show();
            }

     

     

    Tuesday, December 4, 2007 9:05 PM
  • Thank you so very much for your response.  I had no idea that I could have more than one constructor.  That will help me out tremendously in the future.

    Thank you very much for your help.
    Tuesday, December 4, 2007 9:36 PM