Passing Data between forms

J

Jason

Hello

I've got a very simple C# app, that has a datagrid, a text box, and a button
which when clicked opens a second form...
Form2 frm2 = new Form2();

frm2.Show();

When I place a datagrid, onto form1 I point to a DB called Test DB and then
my text box, I modify the databinding property to my bindingSource which in
my case is called inventoryBindingSourse. What I'm interested in doing is
when I click the button on form1 is to pass the current row to the second
form and display the entire row in form2. That sounds simple but I don't
know how to grab the current row, pass that to my second form and display
that data into text boxes.

Any idea's or example would be appreciated.
Jason
 
D

Dave Sexton

Hi Jason,

You can access the current item through the Current property of the
BindingSource object, which will return a DataRowView object when bound to a
DataSet or DataTable. The return Type is System.Object so you'll have to cast
it.

You can pass the object to Form2 in a couple of ways:

class Form2 : Form
{
// Use a public property with a private backing field:
private DataRowView dataSource;

public DataRowView DataSource
{
get { return dataSource; }
set
{
dataSource = value;

// bind a textbox to the DataRowView
textBox1.DataBindings.Add("Text", value, "Column1");
}
}

// Or you can use a constructor that takes an argument
// (one or both options may be used)
public Form2(DataRowView dataSource)
{
// assign the property value (normally, you could just set the field
// directly but since we want some logic in the "setter" of the
property
// to be executed (code below) we're going to set the property
instead)
DataSource = dataSource;
}
}

Make sure you only set the property once otherwise you'll need more code to
prevent Add being called twice for the Text property. For that reason, the
constructor approach is probably your best option. Here's an example:

void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2((DataRowView) bindingSource1.Current);
}

If you use the code above you can remove the Form2.DataSource property
altogether and perform the binding to textBox1 in the Form2 constructor.
 
J

junk

Jason,
Two ways of accomplishing your task.....
1. If you have an index key (primary key) in the source from the database.
Simply grab the key in the selected row and pass it to the second form. Then
in the second form grab the records from the database using the passed key.

with DataViewGrid properties:
this.dgInv.MultiSelect = false;
this.dgInv.ReadOnly = true;

long iPassThisKey = 0;
private void dgInv_DblClick() {
if(this.dgInv.SelectedRows.Count > 0) { // if the user double clicks on
the row
iPassThisKey = this.dgInv.SelectedRows[0].Cells["InvKey"].Value;
} else {
if(this.dgInv.SelectedCells.Count > 0) { //if the user double clicks
on a cell in the row
iPassThisKey =
this.dgInv.Rows[this.dgInv.SelectedCells[0].RowIndex].Cells["InvKey"].Value;
}
}

OR

The are many ways to pass this data onto the second form. This is just one
example, collect the data into a delimited format and passing it all at
once. You will have to unpack it in the second form. I would only do this if
you only have a couple of values your passing to the second form. IMHO use
the method above....

if (this.dgInv.RowCount > 0) {
DataGridViewCellCollection dgCells = new this.dgInv.Rows[0].Cells;
StringBuilder sbCellValues = new StringBuilder();
for ( int i = 0; i<dgCells.Count; i++){ //iterate through the collection
if ( i = (dgCells.Count-1) ){ // is the last cell in the row
sbCellValues.Append( dgCells.Value.ToString() );
} else {
sbCellValues.AppendFormat("{0},", dgCells.Value.ToString();
}
}
 
J

Jason

Dave

On my form1 I've got a button, that when a user clicks it, I want it to
open the second form showing the data of the current row.
So in my button1_Click sub I've added a line to get cuurrent row, then show
the form.

Does this look correct?


//form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2((DataRowView) bindingSource1.Current);
frm2.Show();
}


public Form2(DataRowView DataSource)
{
textBox1.DataBindings.Add("Text", DataSource, "Column1");
textBox2.DataBindings.Add("Text", DataSource, "Column2");
textBox3.DataBindings.Add("Text", DataSource, "Column3");
}
 
D

Dave Sexton

Hi Jason,

That looks great, but the parameter should be in lower camel case:

public Form2(DataRowView dataSource) { ... }

I recommend choosing a more descriptive name if you can, such as,
"personDataRow". If you're using a strong-typed DataSet then use the Typed
DataRow as the parameter instead of DataRowView.

Realize also that any changes to "dataSource" in the grid on Form1 will be
visible on Form2 while it's open. If that behavior isn't desirable then
create a new DataRow that is a copy of the DataRowView.Row object and bind to
that instead.
 
J

Jason

Thanks for the help Dave, that works great.

Now, if I decided not to open form2 when a button is clicked, but instead
when a row, cell is double clicked, how would I accomplish that?
 
D

Dave Sexton

Hi Jason,

If you're using the DataGridView control and VS 2005:

1. Select the control in the designer and view the properties window.
2. Click the lightning bolt icon on the top to list the events of the control.
3. Click the left-most icon as well, if it isn't highlighted already, to
display properties by category.
4. Take a look at the events in the "Mouse" category

You'll probably find either the CellMouseDoubleClick or CellDoubleClick event
to be of use.
 
J

Jason

Thanks again.


Dave Sexton said:
Hi Jason,

If you're using the DataGridView control and VS 2005:

1. Select the control in the designer and view the properties window.
2. Click the lightning bolt icon on the top to list the events of the
control.
3. Click the left-most icon as well, if it isn't highlighted already, to
display properties by category.
4. Take a look at the events in the "Mouse" category

You'll probably find either the CellMouseDoubleClick or CellDoubleClick
event to be of use.
 

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