1

I have a function

public DataSet Select(string sql)
{
    //Get data source from database to DataSet
}

in class DataDriverExample and in form_load, I bind to the datagridview.

public BindingSource bs = new BindingSource()
public void form_load()
{
    DataSource ds = DataDriverExample.Select("Select * from XYZ");
    bs.DataSource = ds;
    dataGridView.DataSoure = bs;
    dataGridView.DataMember = "TableResult";
    txtAbc.Bindings.Add("text",bs,"TableResult.Col1");
}

it worked, but I have Button Add (which adds a new record to the database)

public void btnAdd_click()
{
    DataDriverExample.Insert("Insert into XYZ values (\"Test\",\"Hello\",\"Bla\")");
    DataSource ds = DataDriverExample.Select("Select * from XYZ");
    bs.DataSource = ds;
    bs.ResetBinding(true);
}

In my database it now has new record, but I get the error Cannot bind to the property or column on the DataSource Sorry for my english not good. Anyone can help me. Tks

Mark G
  • 557
  • 1
  • 6
  • 17

1 Answers1

1

To Clean up your code, create another method for your binding something like:

 private void LoadData()
{
    bs.ResetBinding(true);
    txtAbc.DataBindings.Clear()
    DataSource ds = DataDriverExample.Select("Select * from XYZ");
    bs.DataSource = ds;
    dataGridView.DataSoure = bs;
    dataGridView.DataMember = "TableResult";
    txtAbc.Bindings.Add("text",bs,"TableResult.Col1");
 }

Then just call LoadData(); on your formload and add method

    public BindingSource bs = new BindingSource()
   public void form_load()
   {
   LoadData();
   }

Add:

public void btnAdd_click()
{
    DataDriverExample.Insert("Insert into XYZ values (\"Test\",\"Hello\",\"Bla\")");
    LoadData();
}

Hope this helps

Regards

BizApps
  • 6,048
  • 9
  • 40
  • 62