Заполнение DataGridView из DataTable - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте уважаемые эксперты! Есть следующий код:
Листинг программы
  1. static class FillDataGridView
  2. {
  3. private static DataTable AddInformation(string str)
  4. {
  5. DataTable _table = new DataTable("Table");
  6. string[] _colNames = new[] { "Column 1", "Column 2", "Column 3" };
  7. foreach (string colname in _colNames)
  8. {
  9. DataColumn column = new DataColumn(colname);
  10. column.DataType = Type.GetType("System.String");
  11. _table.Columns.Add(column);
  12. }
  13. DataRow dataRow;
  14. dataRow = _table.NewRow();
  15. dataRow[_colNames[0]] = "str1";
  16. dataRow[_colNames[1]] = "str2";
  17. dataRow[_colNames[2]] = str;
  18. _table.Rows.Add(dataRow);
  19. return _table;
  20. }
  21. public static void FillDataGrid(DataGridView dataGrid, string str)
  22. {
  23. DataTable table = AddInformation(str);
  24. dataGrid.DataSource = table;
  25. }
  26. }
И вызываю я его так:
Листинг программы
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. List<string> testList = new List<string>();
  4. testList.Add("str1");
  5. testList.Add("str2");
  6. testList.Add("str3");
  7. foreach (string str in testList)
  8. {
  9. FillDataGridView.FillDataGrid(dataGridView1, str);
  10. }
  11. }
Но при вызове, данные в DVG переписываются. Как сделать чтобы строка не переписывалась, а добавлялись новые строки в DVG при каждой итерации цикла?

Решение задачи: «Заполнение DataGridView из DataTable»

textual
Листинг программы
  1. internal static class FillDataGridView
  2.     {
  3.         private static DataTable _table;
  4.  
  5.         private static DataTable AddInformation(string str)
  6.         {
  7.             var _colNames = new[] {"Column 1", "Column 2", "Column 3"};
  8.             if (_table == null)
  9.                 CreateTable(_colNames);
  10.  
  11.             DataRow dataRow;
  12.             dataRow = _table.NewRow();
  13.             dataRow[_colNames[0]] = "str1";
  14.             dataRow[_colNames[1]] = "str2";
  15.             dataRow[_colNames[2]] = str;
  16.             _table.Rows.Add(dataRow);
  17.             return _table;
  18.         }
  19.  
  20.         private static void CreateTable(IEnumerable<string> _colNames)
  21.         {
  22.             _table = new DataTable("Table");
  23.             foreach (string colname in _colNames)
  24.             {
  25.                 var column = new DataColumn(colname);
  26.                 column.DataType = Type.GetType("System.String");
  27.                 _table.Columns.Add(column);
  28.             }
  29.         }
  30.  
  31.         public static void FillDataGrid(DataGridView dataGrid, string str)
  32.         {
  33.             DataTable table = AddInformation(str);
  34.             dataGrid.DataSource = table;
  35.         }
  36.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4.214 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут