繁体   English   中英

以不同的形式加载/编辑和保存XML数据

[英]Load/Edit and Save XML Data on a different form

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

它将数据从XML文件读取到数据集中,然后将数据源设置为适合该数据网格

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

在此处输入图片说明

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

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

在此处输入图片说明

到目前为止,到目前为止,我具有“确定”按钮的代码如下,并且由于未在该表单上写任何有关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();

感谢您的帮助!

我认为您的问题与表单之间的联系有关(一个非常基本的问题)。 您应该将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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM