这是不完全清楚你想要达到什么。看起来你有两种形式。在窗体(form1)上有一个DataGridView
和一个Button
向网格添加新行。在第二种形式(form2)上有两个文本框和一个组合框以及一个“OK”按钮。我猜测文本框用于允许用户输入新的“名称”和“格式”数据。然后,当用户单击“确定”按钮时,文本框中的数据将以form1的形式添加到DataGridView
的新行中。最后,DataGridView
的每一行上的“编辑”按钮允许用户打开SAME窗体2(用于添加新项目)并使用它来编辑用户点击的行。
我希望我能正确理解这一点。这是可行的,并且有很多方法可以实现这一点。我只能假设有一些原因,你不会直接在网格中直接“添加”或“编辑”这些单元格。 DataGridView
“添加新行”或“编辑单元格”的默认行为已经实现,并且不需要form2就可以做到这一点,因为您可以在没有新的form2的情况下实现相同的功能,并且绝对不需要代码。因此,我将假设您无法直接在网格中编辑单元格的原因。有DataSource
可能是一个原因(见下文)...
下面的答案是实现这一目的的一种方法。如果DataSource
与DataGridView
有关,我必须发出警告。但是,只要下面的代码在网格中添加或编辑单元格,就可以使用这种方法,它通过直接访问网格单元来实现。如果有一个DataSource
连接到网格,这将不起作用。如果有DataSource
,那么当下面的代码添加或编辑项目时,您需要更改代码以对DataSource
执行这些添加/编辑操作,而不是网格本身。
大部分工作可以在Form2
完成。在“添加”或“编辑”情况下,Form2
将需要至少一个来自Form1
的项目...这将是Form1
的DataGridView
。以“添加”方法为例,它需要Form1
的DataGridView
才能添加新行。这可以通过简单地创建Form2
的新构造函数来实现,该构造函数在其签名中采用DataGridView
。下面的代码是你如何从Form1
的btnAdd_Click
事件中调用这个新的构造函数。
private void btnAdd_Click(object sender, EventArgs e) {
Form2 f2 = new Form2(dataGridView1);
f2.ShowDialog();
}
现在我们需要在Form2
中构造这个构造函数。由于在用户按下“添加/确定”按钮之前不会添加新行,因此我们需要保存构造函数传入的Fom1
的DataGridView
。因此创建了全局变量DataGridView
parentDGV
。当构造函数被调用时,我们只需将parentDGV
设置为DataGridView
即可。这将允许访问“添加”方法中的网格。
DataGridView parentDGV;
public Form2(DataGridView dgv) {
parentDGV = dgv;
InitializeComponent();
}
既然我们现在已经进入到Form1
的DataGridView
你可以简单地添加新行。没有错误检查完成,所以文本框可能是空的。以下是Form2
的“添加”按钮点击事件。
private void btnAddToGrid_Click(object sender, EventArgs e) {
parentDGV.Rows.Add(txtName.Text, txtFormat.Text);
}
这应该允许用户在Form1
的DataGridView
从Form2
的添加新的数据行‘添加’按钮。
接下来,我们需要照顾DataGridView
的每一行上的“编辑”按钮。如果您想使用相同的Form2
来编辑行,那么您将需要进行一些更改,因为它目前只会添加行。要使Form2
正常工作,需要知道用户是否单击Form1
上的“编辑”或“添加”按钮。达到此目的的一种方法是为Form2
制作另一个构造函数。这个构造函数显然需要Form1
的DataGridView
,但它也需要点击“编辑”按钮的“行”索引。这会给我们要编辑的行的行索引。我们可以创建一个全局变量“editRowIndex”来允许从Form2
中的其他方法访问。
DataGridView parentDGV;
int editRowIndex;
public Form2(DataGridView dgv, int rowIndex) {
parentDGV = dgv;
editRowIndex = rowIndex;
InitializeComponent();
SetEditForm();
}
的SetEditForm
方法与编辑数据填充文本框使该形式的一些表面的改变,改变了“添加”按钮上的文字为“更新”,更重要的是,改变了按钮“点击”事件处理程序更新网格中的行而不是添加新行。
private void SetEditForm() {
btnAddToGrid.Text = "Update";
this.btnAddToGrid.Click -= this.btnAddToGrid_Click;
this.btnAddToGrid.Click += new System.EventHandler(this.btnAddToGrid_EditClick);
if (parentDGV.Rows[editRowIndex].Cells["AName"].Value != null)
txtName.Text = parentDGV.Rows[editRowIndex].Cells["AName"].Value.ToString();
else
txtName.Text = "";
if (parentDGV.Rows[editRowIndex].Cells["Format"].Value != null)
txtFormat.Text = parentDGV.Rows[editRowIndex].Cells["Format"].Value.ToString();
else
txtFormat.Text = "";
// set combo box value
}
下面是EditClick
事件与Form2
文本框中输入新数据更新网格正确的行。我假设一旦用户完成编辑,表单将关闭。
private void btnAddToGrid_EditClick(object sender, EventArgs e) {
parentDGV.Rows[editRowIndex].Cells["AName"].Value = txtName.Text;
parentDGV.Rows[editRowIndex].Cells["Format"].Value = txtFormat.Text;
this.Close();
}
最后,当用户在Form1
的DataGridview
点击“编辑”按钮,在行之一的最后一部分是照顾。为了解决这个问题,DataGridView
的CellClick
事件是连线的。一旦单击单元格被确定为“编辑”按钮,我们只需使用正确的构造函数创建新的Form2
。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if ((e.RowIndex >= 0) && (!dataGridView1.Rows[e.RowIndex].IsNewRow)) {
if (e.ColumnIndex >= 0) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit") {
Form2 f2 = new Form2(dataGridView1, e.RowIndex);
f2.ShowDialog();
}
}
}
}
我希望这是有道理的。把它放在一起...使用“设计师”... Form1
有一个DataGridView
与两个文本框列(AName,格式)和第三个按钮列(编辑)编辑行通过Form2
。此外,Form1
有一个按钮,通过Form2
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
FillGrid();
}
private void FillGrid() {
dataGridView1.Rows.Add("Cell00Data", "Cell01Data");
dataGridView1.Rows.Add("Cell10Data", "Cell11Data");
dataGridView1.Rows.Add("Cell20Data", "Cell21Data");
dataGridView1.Rows.Add("Cell30Data", "Cell31Data");
}
private void btnAdd_Click(object sender, EventArgs e) {
Form2 f2 = new Form2(dataGridView1);
f2.ShowDialog();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if ((e.RowIndex >= 0) && (!dataGridView1.Rows[e.RowIndex].IsNewRow)) {
if (e.ColumnIndex >= 0) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit") {
Form2 f2 = new Form2(dataGridView1, e.RowIndex);
f2.ShowDialog();
}
}
}
}
窗体2“添加”新行:使用“设计,Form2
中是否有‘名’和‘格式化’,一个Button
两个文本框(txtName
,txtFormat
)将信息“添加”为新项目或“更新”网格中的一行。
DataGridView parentDGV;
int editRowIndex;
// Constructor to Add a new row
public Form2(DataGridView dgv) {
parentDGV = dgv;
InitializeComponent();
}
// Constructor to Edit a row
public Form2(DataGridView dgv, int rowIndex) {
parentDGV = dgv;
editRowIndex = rowIndex;
InitializeComponent();
SetEditForm();
}
// update form for edit mode
private void SetEditForm() {
btnAddToGrid.Text = "Update";
this.btnAddToGrid.Click -= this.btnAddToGrid_Click;
this.btnAddToGrid.Click += new System.EventHandler(this.btnAddToGrid_EditClick);
if (parentDGV.Rows[editRowIndex].Cells["AName"].Value != null)
txtName.Text = parentDGV.Rows[editRowIndex].Cells["AName"].Value.ToString();
else
txtName.Text = "";
if (parentDGV.Rows[editRowIndex].Cells["Format"].Value != null)
txtFormat.Text = parentDGV.Rows[editRowIndex].Cells["Format"].Value.ToString();
else
txtFormat.Text = "";
// set combo box value
}
// Button click event to update form1’s grid
private void btnAddToGrid_EditClick(object sender, EventArgs e) {
parentDGV.Rows[editRowIndex].Cells["AName"].Value = txtName.Text;
parentDGV.Rows[editRowIndex].Cells["Format"].Value = txtFormat.Text;
this.Close();
}
// Button click event to add a row to form1’s grid
private void btnAddToGrid_Click(object sender, EventArgs e) {
parentDGV.Rows.Add(txtName.Text, txtFormat.Text);
}
经典问题。你不想在Form2中调用'var form1 = new Form1();' - 只要你这样做,你就有一个与原始Form1完全分离的Form1实例。您不会在您实例化并显示Form2的地方显示代码,但这是您在用户关闭Form2后想要获取Form2值的位置。 –