Click here to Skip to main content
15,868,349 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
  1  private void ShowDataTableInMessageBox()
  2  {
  3      DataTable dataTable = new DataTable();
  4      // Assuming your DataTable has been populated with data, the following is sample data
  5      dataTable.Columns.Add("Id", typeof(int));
  6      dataTable.Columns.Add("Name", typeof(string));
  7      dataTable.Rows.Add(1, "Alice");
  8      dataTable.Rows.Add(2, "Bob");
  9  
 10      StringBuilder sb = new StringBuilder();
 11  
 12      // Traverse the DataTable and add data to the StringBuilder
 13      foreach (DataRow row in dataTable.Rows)
 14      {
 15          foreach (DataColumn column in dataTable.Columns)
 16          {
 17              sb.Append(column.ColumnName + ": " + row[column] + " ");
 18          }
 19          sb.AppendLine(); // Add a new row so each record appears on its own line
 20      }
 21  
 22      // Show message box
 23      MessageBox.Show(sb.ToString(), "DataTable Values");
 24  }
 25  
 26  private void ShowDictionaryInMessageBox()
 27  {
 28      // Assume this is your dictionary
 29      Dictionary<int, string=""> myDictionary = new Dictionary<int, string="">
 30      {
 31          { 1, "Apple" },
 32          { 2, "Banana" },
 33          { 3, "Cherry" }
 34      };
 35  
 36      StringBuilder sb = new StringBuilder();
 37  
 38      // Iterate through the dictionary and add key-value pairs to StringBuilder
 39      foreach (KeyValuePair<int, string=""> kvp in myDictionary)
 40      {
 41          sb.AppendLine($"Key: {kvp.Key}, Value: {kvp.Value}");
 42      }
 43  
 44      // Show message box
 45      MessageBox.Show(sb.ToString(), "Dictionary Values");
 46  }
 47  
 48  private void ShowObjectArrayInMessageBox()
 49  {
 50      // Let's say this is your 2D array of objects
 51      object[][] data = new object[][]
 52      {
 53          new object[] { "Id", 1 },
 54          new object[] { "Name", "Alice" },
 55          new object[] { "Age", 30 },
 56          // new data
 57          new object[] { "City", "New York" },
 58          new object[] { "Occupation", "Engineer" },
 59          new object[] { "Hobby", "Reading" },
 60  
 61          // New data row with "Id"
 62          new object[] { "Id", 2, "Name", "Bob", "Age", 28 },
 63          new object[] { "Id", 3, "Name", "Charlie", "Age", 35 }
 64      };
 65  
 66      StringBuilder sb = new StringBuilder();
 67  
 68      // Traverse the two-dimensional object array and add data to StringBuilder
 69      foreach (object[] row in data)
 70      {
 71          // Each row represents a row, and each object in the row represents a column value of the row.
 72          foreach (object columnValue in row)
 73          {
 74              sb.Append(columnValue.ToString() + " ");
 75          }
 76          sb.AppendLine(); // Add a new row so each record appears on its own line
 77      }
 78  
 79      // Show message box
 80      MessageBox.Show(sb.ToString(), "Object Array Values");
 81  }


What I have tried:

first time tried as above, and would like to fine tune it.
Posted
Updated yesterday
v2
Comments
Richard Deeming yesterday    
Explain what you mean by "fine tune it".
Dave Kreskowiak yesterday    
That all depends on what you mean by "fine tune". I don't see much to do to this code because it doesn't do anything really useful.

My biggest complaint is each of these methods does TWO things instead of one. Showing a message box should be up to other UI code, not these methods. These methods should only build a string message and return it.

1 solution

I would avoid using StringBuilder in your examples. The String.Join method is very useful for expanding a collection of strings. You can try something along these lines.


C#
public static void Main()
 {
     object[][] data =
     [
     ["Id", 1,"Name", "Alice","Age", 30,"City", "New York","Occupation", "Engineer",
     "Hobby", "Reading"],
     ["Id", 2, "Name", "Bob", "Age", 28],
     ["Id", 3, "Name", "Charlie", "Age", 35]
     ];

     List<string> list = [];
     foreach (object[] row in data)
     {
         var result = string.Join(' ', row);
         list.Add(result);
     }
     Console.WriteLine(string.Join("\r\n", list));
     Console.ReadLine();
 }
 /** Prints:
  Id 1 Name Alice Age 30 City New York Occupation Engineer Hobby Reading
  Id 2 Name Bob Age 28
  Id 3 Name Charlie Age 35 
  **/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900