Small example of DataTable turned into a string copied to txt text

  • 2020-05-30 19:52:03
  • OfStack

I wrote a method to turn DataTable into a string


public static string DataTableToString(DataTable dt)
{
string dtstring = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
dtstring =dtstring+ dt.Columns[i].ColumnName + "\t";
}
dtstring =dtstring+ "\r\n";
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
dtstring =dtstring+ dt.Rows[i][j] + "\t";
}
dtstring = dtstring + "\r\n";
}
return dtstring;
}
 

Once it becomes a string, you can copy it


string strTemp = DataTableToString(tt);
if (strTemp.Equals("")) // Judge if it is empty  
return;
Clipboard.Clear();// Clear the contents of the original clipboard  
Clipboard.SetText(strTemp);// Adds text to the clipboard and also Object Type data 

Type 1 txt text Ctrl+V to feel 1 victory:)


Related articles: