如何在datagridview中更新和删除行?

时间:2016-05-13 21:40:48

标签: c# datagridview

我如何更新(通过我的弹出窗口)?

// 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();
    }


}

//编辑弹出窗口

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)
    {

    }

}

一些额外的信息。我通过删除和添加行实现了编辑功能,任何建议如何更新?(不只是删除并输入新的) 还有一个问题 - 如果我按下主窗体上的添加按钮并按下弹出窗口上的取消按钮,则会在主窗体上创建新行。我怎么能避免呢? 感谢。

0 个答案:

没有答案