0

目前我有一个看起来像这样的应用程序:

它从一个 XML 文件中读取数据到一个数据集中,然后设置数据源以适应这个数据网格

当用户单击一行时,Notes 部分中的数据将显示在下面的文本框中

在此处输入图像描述

当用户单击 Notes 按钮时,他们会被带到一个新表单 form2,其中来自 notes 文本框的数据被带到一个新的文本框。我想要做的是能够在表单 2 的 Notes 文本框中输入新文本,然后当用户单击“确定”时,它会保存到数据网格

完全一样:http: //youtu.be/mdMjMObRcSk?t= 28m41s

在此处输入图像描述

到目前为止,我目前拥有的 OK 按钮的代码如下,我收到以下错误,因为我没有在该表单上写任何关于 datagridview1 的内容。

我想知道如何从文本框中获取用户输入并“更新”XML 文件,以便使用新的 Notes 更新数据网格

在此处输入图像描述

我不确定这段代码是否有帮助,但这就是我将datagridview1_cellcontentclick链接到form1下面的文本框的方式,我想我需要重用新表单的最后一行来覆盖数据,但我不确定

if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            //The data in the cells for the Notes Column turns into a string and is copied to the textbox below
            textBox1.Text = row.Cells["somenotes"].Value.ToString();

感谢您的帮助!

4

1 回答 1

1

我认为您的问题与表单之间的联系有关(一个非常基本的问题)。您应该将 form2 视为一个对话框,在 form1 中,您可以这样显示它:

//textBox1 is on your form1
if(form2.ShowDialog(textBox1.Text) == DialogResult.OK){
   dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["somenotes"].Value = form2.Notes;
   //perform your update to xml normally
   //.....
}
//your Form2
public class Form2 : Form {
   public Form2(){
      InitializeComponent();
   }
   public string Notes {get;set;}
   public DialogResult ShowDialog(string initText){
      //suppose textBox is on your form2.
      textBox.Text = initText;
      return ShowDialog();
   }
   private void OKButton_Click(object sender, EventArgs e){
      Notes = textBox.Text;
      DialogResult = DialogResult.OK;
   }
   private void CancelButton_Click(object sender, EventArgs e){
      DialogResult = DialogResult.Cancel;
   }
}
//form2 is defined in your Form1 class.
于 2013-07-21T16:03:33.317 回答