问题
How I can update (via my popup)?
//MyClass
public partial class Orders : Form
{
BindingList<MyClass> blist = new BindingList<MyClass>();
public Orders()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Add_Click_1(object sender, EventArgs e)
{
AddOrder addO = new AddOrder();
addO.ShowDialog();
this.dataGridView1.DataSource = blist;
blist.Add(addO.mc);
addO.Close();
}
private void Delete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count>0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
else
{
MessageBox.Show("Please select one row");
}
}
private void Edit_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow currRow = dataGridView1.CurrentRow;
EditOrder eddO = new EditOrder(currRow);
eddO.ShowDialog();
dataGridView1.Rows.RemoveAt(currRow.Index);
blist.Add(eddO.mc);
this.dataGridView1.DataSource = blist;
}
else
{
MessageBox.Show("Please select one row");
}
}
}
//AddPopup
public partial class AddOrder : Form
{
public MyClass mc;
public AddOrder()
{
mc = new MyClass();
InitializeComponent();
}
public void SaveTextBoxesToMyObjectOfClass()
{
mc.ID = txtID.Text;
mc.Name = txtName.Text;
mc.Desciption = txtDescription.Text;
mc.DoctorID = txtDoctorID.Text;
mc.DoctorName = txtDoctorName.Text;
}
private void AddOrder_Load(object sender, EventArgs e)
{
}
private void Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void Save_Click(object sender, EventArgs e)
{
SaveTextBoxesToMyObjectOfClass();
Close();
}
}
//Edit popup
public partial class EditOrder : Form
{
public MyClass mc;
public EditOrder()
{
InitializeComponent();
}
public EditOrder(DataGridViewRow row)
{
InitializeComponent();
txtID.Text = row.Cells[0].Value.ToString();
txtName.Text = row.Cells[1].Value.ToString();
txtDescription.Text = row.Cells[2].Value.ToString();
txtDoctorID.Text = row.Cells[3].Value.ToString();
txtDoctorName.Text = row.Cells[4].Value.ToString();
}
public void EditTextBoxesToMyObjectOfClass()
{
mc = new MyClass();
mc.ID = txtID.Text;
mc.Name = txtName.Text;
mc.Desciption = txtDescription.Text;
mc.DoctorID = txtDoctorID.Text;
mc.DoctorName = txtDoctorName.Text;
}
private void button1_Click(object sender, EventArgs e)
{
EditTextBoxesToMyObjectOfClass();
Close();
}
private void button2_Click(object sender, EventArgs e)
{
EditTextBoxesToMyObjectOfClass();
Close();
}
private void EditOrder_Load(object sender, EventArgs e)
{
}
}
Some additional info. I realised edit function via remove and add row, any suggestions how to update it?(not just delete and enter new one) One more question- if i press add button on main form and press cancel button on popup window, new row on main form created. How I can avoid it? Thanks.
来源:https://stackoverflow.com/questions/37219745/how-i-can-update-and-delete-rows-in-datagridview