如何在新表单中编辑DataSet?

时间:2012-04-19 08:43:43

标签: c# winforms data-binding

我是.NET和C#的初学者,所以我遇到了一个可能很难解决的问题(?)。

我有一个Xml-File作为DataSource。我设法在DataGridView中显示该数据。双击我打开一个用作编辑掩码的新表单。但是当我点击编辑表单中的“OK” - 按钮时,如何将DataSet条目传递给编辑表单并将其同步回DataGridViews底层DataSource?

这是我到目前为止所做的:

主要表格:

public partial class Main : Form
{
    private DataSet dataSet;
    private EditForm editForm;

    public Main()
    {
        InitializeComponent();
    }

    private void readXmlFile(object sender, EventArgs e)
    {
        // bind DataGridView to Xml file data
        this.dataSet = new DataSet();
        this.dataSet.ReadXml("data.xml");

        DataTableCollection tables = this.dataSet.Tables;
        DataView view1 = new DataView(tables[0]);

        BindingSource source1 = new BindingSource();
        source1.DataSource = view1;
            source1.Filter = "type = 'editable'";
        gridView.DataSource = source1;
    }

    private void gridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView grid = (DataGridView)sender;
        DataRow row = ((DataRowView)grid.Rows[e.RowIndex].DataBoundItem).Row;

        this.editForm = new EditForm(row);
        editForm.ShowDialog();
    }
}

这是我的编辑表格:

public partial class EditForm : Form
{
    DataRow dataRow;

    public EditForm(DataRow dataRow)
    {
        InitializeComponent();

        this.dataRow = dataRow;
            textBoxHeadline.DataBindings.Add("Text", this.dataRow, "headline");
    }

    private void buttonOk_Click(object sender, EventArgs e)
    {
        // update DataGridView and DataSet?
    }
}

0 个答案:

没有答案