separate form for editing one detail row

  • Thread starter Thread starter AlanChong
  • Start date Start date
A

AlanChong

Guest
Hi everyone,

Thanks for advance. I have a form with a master-detail structure. Pressing btnNew or btnEdit enable users to type in all textboxes for all master table fields. For detail rows, another set of buttons is provided for editing because it's not allowed to type directly in the DataGridView. When pressing btnItemNew or btnItemEdit, a form with textboxes for a row of the details appears for editing that specific row. The followings are what I am doing now. I want to find a perfect way to achieve that. Any suggestions ?

private void btnItemNew_Click(object sender, EventArgs e) // launch a form for adding
{
DataRow row = ItemTable.NewRow();
ItemTable.Rows.Add(row);
bindingSourceItem.Position = bindingSourceItem.Count - 1; // move to last row(the newly added row)
var form = new EditItemForm(ConnectionString, bindingSourceItem.Current as DataRowView); // for binding
if (form.ShowDialog() != DialogResult.OK) //
ItemTable.Rows.RemoveAt(ItemTable.Rows.Count-1);
form.Dispose();
}

private void btnItemEdit_Click(object sender, EventArgs e) // launch a form for editing
{
var form = new EditItemForm(ConnectionString, bindingSourceItem.Current as DataRowView);
form.ShowDialog();
form.Dispose();
}

// detail form

public partial class EditItemForm : Form
{
string CnnStr;
DataRowView drv;
DataRow row;

public EditItemForm(string TheCnnStr, DataRowView Thedrv)
{
InitializeComponent();
CnnStr = TheCnnStr;
drv = Thedrv;
row = drv.Row;
bindingSource1.DataSource = drv;
// a binding function somewhere else for binding all textboxes on panel1 to bindingSource1
FormHelp.BindControls(panel1, bindingSource1);
}

private void btnOK_Click(object sender, EventArgs e)
{
// doing nothing because texts in textboxes go directly to DataRow
DialogResult = DialogResult.OK;
}

private void btnCancel_Click(object sender, EventArgs e)
{
drv.Row.RejectChanges(); // the only way I can think of
DialogResult = DialogResult.Cancel;
}
}

Continue reading...
 
Back
Top