How I can update and delete rows in datagridview?

自闭症网瘾萝莉.ら 提交于 2019-12-25 06:49:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!