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

        comboFind.Items.Add(comboFind.Text);

      FillList(rows, comboFind.Text);

      bSearchChanged = false;

    }

    else

      DisposeFormFound();

  }

  void ShowGrid(string sel)

  {

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

  }

  void DisposeFormFound()

  {

    if (formFound != null)

      formFound.Dispose();

    sNotFound.Visible = true;

  }

  void FillGrid(DataView view)

  {

    DataGridView grid = new DataGridView();

    grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

    grid.Location = new Point(3, 3);

    grid.Size = activeGrid.Size;

    grid.Name = "grid";

    grid.Size = new Size(496, 171);

    grid.TabIndex = 0;

    grid.ReadOnly = true;

    grid.CellEnter += new DataGridViewCellEventHandler(gridNew_CellEnter);

    grid.Tag = activeGrid;

    if (activeGrid == studsGrid)

      AddStyleStud(grid);

    else

      AddStyleExam(grid);

    grid.DataSource = view;

    ShowSearchResults(grid, comboFind.Text);

  }

  void ShowExam(int id)

  {

    DataRow[] rows = ds.Tables[1].Select("ID=" + id);

    int studID = (int)rows[0][1];

    int activeCol = activeCell.ColumnIndex;

    ShowStud(studID);

    examsGrid.CurrentCell = null;

    Application.DoEvents();  // Not needed ?

    for (int i = 0; i < examsGrid.Rows.Count - 1; i++)

    {

      int examID = (int)examsGrid.Rows[i].Cells[0].Value;

      if (examID == id)

      {

        examsGrid.Rows[i].Selected = true;

        activeGrid = examsGrid;

        activeCell = activeGrid[activeCol, i];

      }

      else

        examsGrid.Rows[i].Selected = false;

    }

  }

  void ShowStud(int id)

  {

    for (int i = 0; i < studsGrid.Rows.Count; i++)

    {

      object o = studsGrid.Rows[i].Cells[0].Value;

      if ((int)o == id)

      {

        studsGrid.Rows[i].Selected = true;

        bsStuds.Position = i;

        Application.DoEvents();

        break;

      }

    }

  }

  void FillList(DataRow[] rows, string text)

  {

    ListView list = new ListView();

    list.View = View.Details;

    list.FullRowSelect = true;

    list.GridLines = true;

    int listWidth = 0;

    foreach (DataGridViewColumn col in activeGrid.Columns)

    {

      int w = col.Width + 4;

      list.Columns.Add(col.HeaderText, w);

      listWidth += w;

    }

    foreach (DataRow row in rows)

    {

      ListViewItem item = new ListViewItem(row["ID"].ToString());

      foreach (DataGridViewColumn col in activeGrid.Columns)

      {

        if (col.HeaderText == "ID")

           continue;

        object data = row[col.HeaderText];

        string val = data.ToString();

        if (col.Name == "Date" && data != DBNull.Value)

           val = ((DateTime)data).ToShortDateString();

        item.SubItems.Add(val);

      }

      list.Items.Add(item);

    }

    list.Width = listWidth + 20;

    list.Height = 100;

    ShowSearchResults(list, text);

  }

  void ShowSearchResults(Control control, string text)

  {

    if (formFound != null)

      formFound.Dispose();

    formFound = new Form();

    Label msg = new Label();

    msg.Location = new Point(3, 3);

    msg.AutoSize = true;

    msg.Font = new Font("Comic Sans MS", 12);

    msg.Text = "Searching for:   '" + text + "' in " +

      activeTable.TableName + "." + searchColumnName;

    msg.ForeColor = Color.DarkGreen;

    msg.BackColor = Color.White;

    msg.BorderStyle = BorderStyle.FixedSingle;

    formFound.Controls.Add(msg);

    formFound.Text = "Search results";

    formFound.MinimizeBox = false;

    formFound.MaximizeBox = false;

    formFound.Owner = this;

    formFound.Width = Math.Max(msg.Width, control.Width) + 14;

    formFound.Height = msg.Height + control.Height + 38;

    control.Location = new Point(3, msg.Height + 5);

    formFound.Controls.Add(control);

    formFound.Visible = true;

    formFound.Location = new Point(Location.X + Width - 60, Location.Y + 100);

  }

}