Event Handlers

P

Paul

Hi,
I have a tabcontrol and a panel with some textboxes in on my main form. The
tabcontrol is populated will a datagridview and data by a different class
based on a selection made. How do I handle a row selection event to update
the textboxes on the mainform i.e. passing events between classes.

Thanks in advance

Paul
 
N

Nicholas Paldino [.NET/C# MVP]

Paul,

I wouldn't use events to do this. Rather, I would connect to the
binding context that the grid is using, and then set the property on the
textboxes that you want to bind to in the data source for the grid. That
way, the textboxes and controls will automatically update.
 
P

Paul

Hi, thanks for your help so far. I have managed to get the textbox populated
with the first cell in the datagridview but I cannot get it to update when
the selection changes. I either get errors about the datamember having a
null value or its unable to get the childlist. Can you provide me with, or
point me to, an example.

Thanks again.

Paul

Nicholas Paldino said:
Paul,

I wouldn't use events to do this. Rather, I would connect to the
binding context that the grid is using, and then set the property on the
textboxes that you want to bind to in the data source for the grid. That
way, the textboxes and controls will automatically update.


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

Paul said:
Hi,
I have a tabcontrol and a panel with some textboxes in on my main form.
The tabcontrol is populated will a datagridview and data by a different
class based on a selection made. How do I handle a row selection event to
update the textboxes on the mainform i.e. passing events between classes.

Thanks in advance

Paul
 
N

Nicholas Paldino [.NET/C# MVP]

Paul,

Basically, what you want to do is get the DataSource and the DataMember
properties for the grid view. This will give you an object and a member
which you can then pass (you might have to modify the member to include
".<your field>" since the member will probably point to a table if the data
source is a DataSet and you want a specific property, not a whole row) to
the Add method on the ControlDataBindings instance returned by the
DataBindings property on the control you want to data bind.

So basically, something like:

// Get the data source and the data member
object dataSource = grid.DataSource;
string dataMember = grid.DataMember;

// If there is a data member, append the name of the field to get.
dataMember = (string.IsNullOrEmpty(dataMember) ? "" : ".") + "<field to bind
to>"

// Assume it's a textbox you want to bind to.
textbox.DataBindings.Add("Text", dataSource, dataMember);


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

Paul said:
Hi, thanks for your help so far. I have managed to get the textbox
populated with the first cell in the datagridview but I cannot get it to
update when the selection changes. I either get errors about the
datamember having a null value or its unable to get the childlist. Can you
provide me with, or point me to, an example.

Thanks again.

Paul

Nicholas Paldino said:
Paul,

I wouldn't use events to do this. Rather, I would connect to the
binding context that the grid is using, and then set the property on the
textboxes that you want to bind to in the data source for the grid. That
way, the textboxes and controls will automatically update.


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

Paul said:
Hi,
I have a tabcontrol and a panel with some textboxes in on my main form.
The tabcontrol is populated will a datagridview and data by a different
class based on a selection made. How do I handle a row selection event
to update the textboxes on the mainform i.e. passing events between
classes.

Thanks in advance

Paul
 

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

Similar Threads


Top