[HowTo] Generate a dummy table for binding to a tabular structure.

While designing grids and related styles, I always found the need to bind some dummy data to the grid to see how the grid looks. So, rather than making a database call to get a really organized table, I decided to get one of my own.

The following method generates and returns a data-table to be bound to any tabular structure.

 1: public DataTable GetDummyData(int columnCount, int rowCount, bool useNumeric, int rowLength, int minNumber, int maxNumber)
 2: {
 3: DataTable dt = new DataTable();
 4: dt.Columns.Add(new DataColumn(" ", typeof(string)));
 5:  
 6: for (int c = 0; c < columnCount; c++)
 7: {
 8: string columnName = RandomString(rowLength, false);
 9: if (useNumeric == true)
 10: {
 11: dt.Columns.Add(new DataColumn(columnName, typeof(Int32)));
 12: }
 13: else
 14: {
 15: dt.Columns.Add(new DataColumn(columnName, typeof(string)));
 16: }
 17: }
 18: for (int j = 1; j <= rowCount; j++)
 19: {
 20: DataRow dr = dt.NewRow();
 21: dr[0] = RandomString(4, false);
 22: for (int k = 1; k <= columnCount; k++)
 23: {
 24: if (useNumeric == true)
 25: {
 26: dr[k] = RandomNumber(minNumber, maxNumber);
 27: }
 28: else
 29: {
 30: dr[k] = RandomString(rowLength, true);
 31: }
 32: }
 33: dt.Rows.Add(dr);
 34: }

      
 35: return dt;
 36: }

Disclaimer : Don’t use it for production. This is just to test on your local machines.

Leave a Reply