反映DataGridView中的已修改字段

时间:2013-01-02 10:32:53

标签: c# datagridview datatable updates datarow

要回答我的问题,我首先需要做一些解释,所以请耐心等待。

此应用程序有两种形式。在主要表单中,我有一个DataGridView。它显示数据库表中的数据。其DataSource设置为DataTable个对象。这是主要表格的代码。

using System;
using System.Data;
using DataAccess;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private SqlDataAccess _dataAccess = new SqlDataAccess(); //SqlDataAccess is a class written to handle database related operations
        private DataTable _dataTable = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            string query = @"SELECT * FROM fEmployee";
            _dataTable = _dataAccess.GetDataTable(query, null);
            dgvEmployees.DataSource = _dataTable;
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //Convert the current selected row in the DataGridView to a DataRow
            DataRowView currentDataRowView = (DataRowView)dgvEmployees.CurrentRow.DataBoundItem;
            DataRow dataRow = currentDataRowView.Row;

            Form2 f = new Form2(dataRow);
            f.ShowDialog();           
        }
    }
}

点击DataGridView的行标题后,系统会显示一个子表单。此子表单用作修改所选行的字段值的位置。包含所选行的字段的DataRow对象将发送到子表单的重载构造函数。在该表单的Load事件中,DataRow中包含的数据将显示在子表单中的多个文本框中。

子表格的代码。

using System;
using System.Data;

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form
    {
        private DataRow _employeeDetails = null;
        private bool _isDirty = false;

        public Form2(DataRow empDetails)
        {
            InitializeComponent();

            _employeeDetails = empDetails;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            txtFName.Text = _employeeDetails["FirstName"].ToString();
            txtLName.Text = _employeeDetails["LastName"].ToString();
            txtAddress.Text = _employeeDetails["Address"].ToString();
            txtCity.Text = _employeeDetails["City"].ToString();
            txtPostalCode.Text = _employeeDetails["PostalCode"].ToString();
            txtCountry.Text = _employeeDetails["Country"].ToString();
            dtpDOB.Value = Convert.ToDateTime(_employeeDetails["DOB"]);
            txtPhone.Text = _employeeDetails["Phone"].ToString();
            txtEmail.Text = _employeeDetails["Email"].ToString();
            dtpDOJ.Value = Convert.ToDateTime(_employeeDetails["DOJ"]);
            txtBasicSalary.Text = _employeeDetails["BasicSalary"].ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }
    }
}

在子表单中,用户可以通过文本框更改值。

现在我的问题:如何在主窗体的DataGridView中反映子窗体中对该特定行所做的更改?

示例 - 我单击一个行标题,它打开子表单并加载详细信息。我改变了名字。当我关闭子表单时,应该在主DataGridview中更新该修改后的值。

有人可以就如何做到这一点提出一些建议吗?

我尝试将DataRow传递给子表单作为参考,但是没有用。

1 个答案:

答案 0 :(得分:0)

尝试测试这种方法。您必须使用DataRow[]来更新所选DataRow的数据。请试着弄明白并了解一下。

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    public BindingSource bs = new BindingSource();
    public DataRow[] mainDataRow;
    private DataTable employee = new DataTable();

    private void MainForm_Load(object sender, EventArgs e)
    {
        employee.Columns.Add("Id");
        employee.Columns.Add("LastName");
        employee.Columns.Add("FirstName");
        employee.Columns.Add("MiddleName");

        object[] emp1 = { "1", "Some1a", "Some1b", "Some1c" };
        object[] emp2 = { "2", "Some2a", "Some2b", "Some2c" };
        object[] emp3 = { "3", "Some3a", "Some3b", "Some3c" };
        object[] emp4 = { "4", "Some4a", "Some4b", "Some4c" };
        employee.Rows.Add(emp1);
        employee.Rows.Add(emp2);
        employee.Rows.Add(emp3);
        employee.Rows.Add(emp4);

        bs.DataSource = employee;
        dataGridView1.DataSource = bs;

    }

    private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        //Convert the current selected row in the DataGridView to a DataRow
        DataRowView currentDataRowView = (DataRowView)dataGridView1.CurrentRow.DataBoundItem;
        mainDataRow = employee.Select("Id='"+ currentDataRowView[0].ToString() + "'"); //get the primary key id
        using (var f = new Form2{ dataRow = mainDataRow, Owner = this })
        {
            f.ShowDialog();        
        }
    }
}

然后在Form2

public partial class Form2: Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public DataRow[] dataRow;
    private void Form2_Load(object sender, EventArgs e)
    {
        var select = dataRow[0];
        lastNameTextBox.Text = select[1].ToString();
        firstNameTextBox.Text = select[2].ToString();
        middleNameTextBox.Text = select[3].ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MainForm m = this.Owner as MainForm;
        var updated = dataRow[0];
        updated[1] = lastNameTextBox.Text;
        updated[2] = firstNameTextBox.Text;
        updated[3] = middleNameTextBox.Text;
        m.mainDataRow[0] = updated;
        Close();
    }
}