Refresh databindings WinForm

A

Alex Bibiano

I have a typed dataset in my WinForm and a textbox with databinding to the
dataset (all without code). Now, in a button event, I assign a new dataset
(retrieved from a function) to the dataset a had in the form, but the
textbos isn't refreshed.

Exemple code from my button click event:

dataset1 = helper.GetUser(6);


¿How can I refresh it?
 
A

Alex Bibiano

I post a better codesnip from my code:

public class Form1 : System.Windows.Forms.Form
{
private DatasetUser datasetTest = new DatasetUser();
...

private void InitializeComponent()
{
this.textBox1.DataBindings.Add("Text", this.datasetTest,
"T_USR.USR_ID");
...
}

private void button2_Click(object sender, System.EventArgs e)
{
datasetTest = usu.GetUsuario(6) //Function that return a dataset;
...
//This statment fails, because it dosn't refresh the textBox1.
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

The reason this doesn't work is that the controls don't know that the
data set has actually changed. The TextBox and the data grid have
references to the original value of datasetTest, and can not determine when
the reference to that changes (they hold a different reference to that
object).

What you should do is call the Clear method on the datasetTest field,
and then call Merge, passing in the result from usu.GetUsuario(6). This
will merge the dataset with the existing data set, and the changes should
register with all controls bound to that dataset.

Hope this helps.
 
A

Alex Bibiano

A lot of thanks, this answer my question, but I have another one.

Is it better (performance, memory, ...) that my function return a dataset
and merge it whit the dataset I have in the form, or is it better modify my
function to accept a dataset as parameter and fill it?




Nicholas Paldino said:
Alex,

The reason this doesn't work is that the controls don't know that the
data set has actually changed. The TextBox and the data grid have
references to the original value of datasetTest, and can not determine
when the reference to that changes (they hold a different reference to
that object).

What you should do is call the Clear method on the datasetTest field,
and then call Merge, passing in the result from usu.GetUsuario(6). This
will merge the dataset with the existing data set, and the changes should
register with all controls bound to that dataset.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alex Bibiano said:
I post a better codesnip from my code:

public class Form1 : System.Windows.Forms.Form
{
private DatasetUser datasetTest = new DatasetUser();
...

private void InitializeComponent()
{
this.textBox1.DataBindings.Add("Text", this.datasetTest,
"T_USR.USR_ID");
...
}

private void button2_Click(object sender, System.EventArgs e)
{
datasetTest = usu.GetUsuario(6) //Function that return a dataset;
...
//This statment fails, because it dosn't refresh the textBox1.
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

It would probably be better to accept the data set and fill it. It
should also probably clear the original data set before it filled it as
well. With the merge, you are cylcing through the result set to populate
the data set, and then cycling through it again during the merge. If you
pass it in, you would only have to cycle through once, during the initial
fill.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alex Bibiano said:
A lot of thanks, this answer my question, but I have another one.

Is it better (performance, memory, ...) that my function return a dataset
and merge it whit the dataset I have in the form, or is it better modify
my function to accept a dataset as parameter and fill it?




Nicholas Paldino said:
Alex,

The reason this doesn't work is that the controls don't know that the
data set has actually changed. The TextBox and the data grid have
references to the original value of datasetTest, and can not determine
when the reference to that changes (they hold a different reference to
that object).

What you should do is call the Clear method on the datasetTest field,
and then call Merge, passing in the result from usu.GetUsuario(6). This
will merge the dataset with the existing data set, and the changes should
register with all controls bound to that dataset.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alex Bibiano said:
I post a better codesnip from my code:

public class Form1 : System.Windows.Forms.Form
{
private DatasetUser datasetTest = new DatasetUser();
...

private void InitializeComponent()
{
this.textBox1.DataBindings.Add("Text", this.datasetTest,
"T_USR.USR_ID");
...
}

private void button2_Click(object sender, System.EventArgs e)
{
datasetTest = usu.GetUsuario(6) //Function that return a dataset;
...
//This statment fails, because it dosn't refresh the textBox1.
}
}

"Alex Bibiano" <[email protected]> escribió en el mensaje
I have a typed dataset in my WinForm and a textbox with databinding to
the dataset (all without code). Now, in a button event, I assign a new
dataset (retrieved from a function) to the dataset a had in the form,
but the textbos isn't refreshed.

Exemple code from my button click event:

dataset1 = helper.GetUser(6);


¿How can I refresh it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top