C# 在DataGridView中反映修改的字段
为了回答我的问题,我需要先做一点解释,请耐心听我说 本申请表有两种形式。在主窗体中,我有一个DataGridView。它显示数据库表中的数据。其数据源设置为DataTable对象。这是主窗体的代码C# 在DataGridView中反映修改的字段,c#,datagridview,datatable,updates,datarow,C#,Datagridview,Datatable,Updates,Datarow,为了回答我的问题,我需要先做一点解释,请耐心听我说 本申请表有两种形式。在主窗体中,我有一个DataGridView。它显示数据库表中的数据。其数据源设置为DataTable对象。这是主窗体的代码 using System; using System.Data; using DataAccess; namespace WindowsFormsApplication3 { public partial class Form1 : Form { private Sq
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对象被发送到子表单的重载构造函数。在该表单的加载事件中,该数据行中包含的数据将显示在子表单的多个文本框中
子窗体的代码
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中的数据。请试着把它弄清楚,想个办法
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();
}
}
}
然后在表格2中:
在从关闭sub之前,请更新表中的详细信息。调用bind方法,该方法将Datagridview的db中的数据绑定到主窗体中。我也想到了该选项,但我将其作为最后的选择。我正在尝试在完成所有修改后从主菜单更新后端数据库:好的。修改后的数据行被发送回主屏幕,对吗?-创建一个函数并保持数据表中除您正在修改的行以外的所有行可用。删除您正在修改的行。您可以基于主键执行此操作。然后将此修改后的数据行导入现有数据表并在网格中显示。我希望这对您有所帮助。。
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();
}
}