How to use form elements in library methods and connect System.Windows.Forms? You need to put the method below into the library.

OpenFileDialog openFile = new OpenFileDialog(); string filePath = ""; openFile.ShowDialog(); filePath = openFile.FileName; dataTable.ReadXml(filePath); for (int i = 0; i < dataGridView1.ColumnCount; ++i) { dataTable.Columns.Add(new DataColumn(dataGridView1.Columns[i].Name)); dataGridView1.Columns[i].DataPropertyName = dataGridView1.Columns[i].Name; } 
  • 2
    The essence of the question is not clear, specify the requirements, give examples or attempts to implement - yolosora
  • That's right, you need to connect the System.Windows.Forms.dll library to References. - Alexander Petrov

1 answer 1

Required elements must be passed as method arguments in the library being created.

 public void MyMethod(DataTable dataTable, DataGridView dataGridView) { var openFile = new OpenFileDialog(); if (openFile.ShowDialog() != DialogResult.OK) return; string filePath = openFile.FileName; dataTable.ReadXml(filePath); for (int i = 0; i < dataGridView1.ColumnCount; ++i) { dataTable.Columns.Add(new DataColumn(dataGridView1.Columns[i].Name)); dataGridView.Columns[i].DataPropertyName = dataGridView1.Columns[i].Name; } }