Введение в ADO.NET. Источник данных - XML-файл. Отображение данных связанной таблицы, страница 21

        new object[] {12, 3, ".NET Framework", true, new DateTime(2006,1,19), 5},

        new object[] {13, 4, "Mathematics", true, new DateTime(2006,1,5), 5},

        new object[] {14, 4, "Physics", true, new DateTime(2006,1,10), 5},

        new object[] {15, 4, "English", true, new DateTime(2006,1,14), 5},

        new object[] {16, 4, ".NET Framework", true, new DateTime(2006,1,20), 5},

        new object[] {17, 5, "Mathematics", true, new DateTime(2006,1,6), 5},

        new object[] {18, 5, "Physics", true, new DateTime(2006,1,10), 5},

        new object[] {19, 5, "English", true, new DateTime(2006,1,17), 5},

        new object[] {20, 5, ".NET Framework", true, new DateTime(2006,1,21), 5},

        new object[] {21, 6, "Mathematics", true, new DateTime(2006,1,6), 5},

        new object[] {22, 6, "Physics", true, new DateTime(2006,1,10), 5},

        new object[] {23, 6, "English", true, new DateTime(2006,1,16), 5},

        new object[] {24, 6, ".NET Framework", true, new DateTime(2006,1,20), 5},

    };

    foreach (object[] o in exams)

      ds.Tables[1].Rows.Add(o);

  }

  void SetPrimaryColumn(DataTable table)

  {

    DataColumn dc = new DataColumn("ID", typeof(int));

    dc.AutoIncrement = true;

    dc.AutoIncrementSeed = 1;

    dc.ReadOnly = true;

    table.Columns.Add(dc);

    table.PrimaryKey = new DataColumn[] { dc };

  }

  void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

  {

    switch (e.ClickedItem.Text)

    {

      case "Open": Open(FileDlg(true)); break;

      case "Save": Save(FileDlg(false)); break;

      case "Find Next": Find(); break;

     case "Find List": FindAndShowList(); break;

      case "Find View": FindAndShowGrid(); break;

      case "Average": FilterAverage(); break;

    }

  }

  string FileDlg(bool bOpen)

  {

    FileDialog dlg = bOpen ? (FileDialog)new OpenFileDialog()

      : (FileDialog)new SaveFileDialog();

    dlg.InitialDirectory = Application.StartupPath + "\\..\\..";// .ExecutablePath;

    //Environment.CurrentDirectory;

    dlg.Filter = "People files (*.xml)|*.xml";

    dlg.Title = "Select data file to " + (bOpen ? "open" : "save");

    dlg.FileName = fileName;

    return dlg.ShowDialog() == DialogResult.OK ? dlg.FileName : null;

  }

  string Trim(string sOld)

  {

     // Здесь ваш код

  }

  void Open(string fn)

  {

    if (fn == null)

      return;

    ds.Clear();

    ds.Tables[0].RowChanged -= rowChanged;

    ds.ReadXml(fileName = fn);

    ds.Tables[0].RowChanged += rowChanged;

    Text = "Students Table: " + fn.Substring(fn.LastIndexOf('\\') + 1);

  }

  void Save(string fn)

  {

    if (fn == null || ds.Tables[0].Rows.Count == 0)

      return;

    ds.WriteXml(fileName = fn);

    RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\MySoft\StudentsSet");

    key.OpenSubKey("StudentsSet", true);

    key.SetValue("Last File", fileName);

  }

  void comboFind_KeyDown(object sender, KeyEventArgs e)

  {

    sNotFound.Visible = false;

    if (e.KeyCode == Keys.Enter)

      Find();

  }

  void comboFind_TextUpdate(object sender, EventArgs e)

  {

    btnFindNext.Enabled = Trim(comboFind.Text) != "";

    btnFindList.Enabled = Trim(comboFind.Text) != "";

    bFindView.Enabled = Trim(comboFind.Text) != "";

  }

  void tAverage_TextChanged(object sender, EventArgs e)

  {

    bAverage.Enabled = Trim(tAverageLow.Text) != "" && Trim(tAverageHi.Text) != "";

  }

  void comboFind_SelectedIndexChanged(object sender, EventArgs e)

  {

    btnFindNext.Enabled = Trim(comboFind.Text) != "";

    btnFindList.Enabled = Trim(comboFind.Text) != "";

    bFindView.Enabled = Trim(comboFind.Text) != "";