Clearing a dialog box to create a new record - suspend binding?

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

Hello,

I have a dialog box in which the user edits records. (The main form has a
datagridview which displays all the records. The user normally selects the
desired record and opens a dialog to edit that record.)

There is also a [New Record] button on the dialog box which allows the user
to create a new record.

What steps must I go thorugh, with respect to the binidng, to perform this?
Do I have to suspend binding, then empty the controls, then restore the
binding?

Please clarify all the general the steps for me.

BTW the underlying table is not a child of another table so other IDs are
not involved.

Thank you,
Doug
 
Hi Doug,
What steps must I go thorugh, with respect to the binidng, to perform
this? Do I have to suspend binding, then empty the controls, then restore
the binding?

No, you needn't suspend binding on the main form. If you're using a
DataTable as the data source, you could pass the DataTable and the
BindingContext object from the main form to the dialog and bind the
controls on the dialog to the DataTable. Passing the BindingContext object
is to make the BindingContext of the dialog equal to that of the dialog, so
that the main form would be notified immediately whenever any change is
made in the DataTable within the dialog.

To add a new record in the DataTable, you could call the AddNew method of
CurrencyManager associated with the DataTable.

I will illustrate this with an example. Let's say I have a Form1 and a
Form2. The Form1 has a DataTable and a DataGridView and a Button(the
DataGridView is bound to the DataTable to display all data in the
DataTable). The Form2 has two TextBoxes and two Buttons. In the Button's
click event handler on the Form1, I create a Form2 instance and pass the
DataTable and BindingContext object to it and bind the controls on the
Form2 to the DataTable.

When Form2 is shown, the current row in the DataTable is displayed in the
Form2. I call the line of code
'this.BindingContext[dataTableName].AddNew();' to add a new row into the
DataTable and the controls on the Form2 now are cleared automatically.

The following is the sample code of Form1 and Form2.
public partial class Form1 : Form
{
DataTable dataTable;
private void Form1_Load(object sender, EventArgs e)
{
dataTable = new DataTable();
DataColumn col = new DataColumn("ID", typeof(int));
dataTable.Columns.Add(col);
col = new DataColumn("Name", typeof(string));
dataTable.Columns.Add(col);

DataRow row = dataTable.NewRow();
row[0] = 1;
row[1] = "aa";
dataTable.Rows.Add(row);
row = dataTable.NewRow();
row[0] = 2;
row[1] = "bb";
dataTable.Rows.Add(row);
dataTable.AcceptChanges();

this.dataGridView1.DataSource = dataTable;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.SetDataTable(dataTable,this.BindingContext);
frm2.ShowDialog();
}
}

public partial class Form2 : Form
{
private DataTable dataTable = null;
public void SetDataTable(DataTable dt,BindingContext bc)
{
// make the BindingContext of the Form2 equal to that of the
Form1, so that Form1 would be notified immediately whenever any change is
made in the DataTable within Form2
this.BindingContext = bc;
dataTable = dt;
this.textBox1.DataBindings.Add("Text", dataTable, "ID");
this.textBox2.DataBindings.Add("Text", dataTable, "Name");
}
// add a new row into the DataTable
private void button1_Click(object sender, EventArgs e)
{
if (dataTable != null)
{
this.BindingContext[dataTable].AddNew();
}
}
// save the changes to the DataTable
private void button2_Click(object sender, EventArgs e)
{
if (dataTable != null)
{
this.BindingContext[dataTable].EndCurrentEdit();
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Doug,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top