0

Sorry if this is a really stupid question but I have been going round and round in circles with this one.

I am using C# with windows forms to create an application and have a method which binds data in a data grid view. Below is a sample of the method:

 private void BindGrid()
    {
        string constring = @"Data Source=.\SQLEXPRESS; Database=DB; 
integrated security=False;User ID=UserID;Password=Password";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SET DATEFIRST 1 Select 
Table1.Value1 From Table1...........", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
        }
    }

I call this method three times, once when I load the application, once when I update data and once from a button unrelated to other methods.

Unfortunately, this method is only working from the button click and not when called the other ways. I have tried resolving this in many ways including refreshing the data source and calling the method from a different button.

Why is this method only working when called from a button click and how can I call it programmatically from other areas of the application?

Works here:

  private void button1_Click(object sender, EventArgs e)
    { 
        BindGrid();
    }

Does not work here:

 private void button3_Click(object sender, EventArgs e)
    {
        if (dataGridView1.SelectedRows.Count > 0)

         {

            MethodAdjustRemaining();
            MethodPackOneSent();
            MethodMessage();
            //MethodUpdateData();
            BindGrid();

        }

        else
        {

            MessageBox.Show("Please select a row");
        }
    }

Many thanks in advance for your help :)

  • 7
    what do you mean by `Don't work`. Does it give unexpected result, exception? where are the details? – L.B Aug 28 '17 at 15:15
  • 1
    Can you show the Form.load method? – Capn Jack Aug 28 '17 at 15:16
  • Form.Load is often too early. Try using Form.Shown – TaW Aug 28 '17 at 15:43
  • Hi @L.B thanks for getting back to me so quickly, when I say it doesn't work just nothing happens. the data I expect to see is not loaded until I click the working button. no exceptions. – Gremlin_42 Aug 28 '17 at 15:44
  • @CapnJack, here is my form load code: private void Form2_Load(object sender, EventArgs e) { //BindGrid(); //BindGrid1(); //MethodSearch(); this.TableAdapter.Fill(this.DataSet.DBTable); this.TableAdapter1.Fill(this.DataSet1.DBTable); BindGrid(); BindGrid1(); } – Gremlin_42 Aug 28 '17 at 15:45
  • @Meg oh god can you format that in an edit of your question lol – Capn Jack Aug 28 '17 at 18:07
  • i have queued an edit where i added the method from the comment. but it is not publicly visible until it is reviewed – Tobias Theel Aug 28 '17 at 18:17
  • @Capn Jack, when ever I format (4 spaces as advised by stackoverflow) it still comes through as above, sorry :( basically on the load all it is doing is a tableadapter.filf and then calling the bindgrid method. – Gremlin_42 Aug 28 '17 at 18:42
  • @TaW, my form_shown event is not firing, do you have a sample of how to invoke this? – Gremlin_42 Aug 30 '17 at 11:29
  • 1
    _my form_shown event is not firing_ Huh? I can't imagine why that would be!? (It is [hooked up](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=14|0.0000#33276161), right?) – TaW Aug 30 '17 at 12:40
  • 1
    Thanks @TaW, worked a treat. – Gremlin_42 Aug 31 '17 at 20:13

0 Answers0