簡體   English   中英

在DataGridView中反映修改后的字段

[英]Reflecting modified fields in the DataGridView

為了回答我的問題,我需要先做一點解釋,所以請多多包涵。

該應用程序有2種形式。 在主要形式中,我有一個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傳遞給子表單作為參考,但這沒有用。

嘗試測試這種方法。 你必須使用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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM