Como crear un datatable programaticamente

El problema que tenia es el siguiente, se denomina carrera via sms. Una vez que se inicia la carrera los usuarios deberian constestar en el menor tiempo posible las preguntas que se le enviaba via sms. Y el sistema debera ir mostrando los primeros 5 con mayor puntaje a cada segundo. Para ello agregue un gridview a mi formulario y enlaze a un datable el grid.
Aca les muestro el codigo.
' En la declaracion
DataTable table = new DataTable();

'En el load del form
DataColumn col1 = new DataColumn("lugar");
DataColumn col2 = new DataColumn("nrocupon");
DataColumn col3 = new DataColumn("telefono");
DataColumn col4 = new DataColumn("segundos");
DataColumn col5 = new DataColumn("telargo");

col1.DataType = System.Type.GetType("System.Int32");
col2.DataType = System.Type.GetType("System.Int32");
col3.DataType = System.Type.GetType("System.String");
col4.DataType = System.Type.GetType("System.Int32");
col5.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
table.Columns.Add(col5);



Esto en una funcion, como veran cargo la grilla de a uno para que de la impresion de que se esta jugando una carrera.
table.Clear();
int cuenta = 0;

gridControl2.DataSource = table;
ocon6.Open();
string sql = "SELECT TOP (5) (select top 1 nrocupon from detalle B where B.idservicio=149 and A.id=B.idprincipal and nrocupon is not null order by nrocupon desc) as nrocupones,celular,DATEDIFF(s, fecha,";
sql = sql + "(SELECT Fecha FROM Carrera)) AS segundo FROM Maestro A WHERE (id_operadora = 2) AND (id_servicio = 149) ORDER BY cupones DESC, segundo";
SqlCommand sqlcomando = new SqlCommand(sql, ocon6);
reader = sqlcomando.ExecuteReader();
while (reader.Read())
{
cuenta++;
DataRow row = table.NewRow();
nrocupon = Convert.ToInt32(reader[0]);
telefono = reader[1].ToString();
telefono = telefono.Replace("+", "");
if (telefono.StartsWith("595"))
{
telefono = "0" + telefono.Substring(3, 9);
}
if (telefono.StartsWith("99"))
{
telefono = "0" + telefono;
}

segundos = Convert.ToInt32(reader[2]);
if (segundos < 0)
segundos = segundos * -1;

System.Threading.Thread.Sleep(100);

Application.DoEvents();
row[0] = cuenta;
row[1] = nrocupon;


row[2] = telefono.Substring(0, telefono.Length - 2) + "**";
row[3] = segundos;
row[4] = telefono;
fecha = DateTime.Now;
table.Rows.Add(row);
gridControl2.DataSource = table;


}

ocon6.Close();

Bueno, espero que a alguien le ayude
Saludos
Adalberto Montania

Comentarios