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 :)