1

私の質問にたどり着くには、最初に少し説明をする必要がありますので、ご容赦ください。

このアプリケーションには 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 で、サブ フォームの特定の行に加えられた変更をどのように反映できますか?

例 - 1 つの行ヘッダーをクリックすると、サブフォームが開き、詳細が読み込まれます。名前を変更します。サブフォームを閉じると、その変更された値がメインで更新されますDataGridview

これを行う方法について誰かが提案できますか?

を参照としてサブフォームに渡そうとしDataRowましたが、うまくいきませんでした。

4

1 に答える 1

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();
    }
}
于 2013-01-02T14:15:09.720 に答える