...

View Full Version : [C#] Write out a query result into a TableLayoutPanel



tomharto
08-22-2012, 02:54 PM
I'm trying to run a query, to select a few things from a database and write them out in a 2 column table.

So far I have this.


string MyConString = "SERVER=" + "DATABASE=" + "UID=" + "PASSWORD=";

MySqlConnection connection = new MySqlConnection(MyConString);

MySqlCommand command = connection.CreateCommand();

MySqlDataReader Reader;

command.CommandText = "SELECT `jobname` FROM `joblist` ORDER BY `ID` ASC LIMIT 10";

connection.Open();

Reader = command.ExecuteReader();

while (Reader.Read())
{


int count = 0;
for (int i = 0; i < Reader.FieldCount; i++)
{


Label label1 = new Label();
Label label2 = new Label();

label1.Text = Reader.GetValue(i).ToString();
label2.Text = @"Some text";

tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel1.Controls.Add(label1, 0, count);
tableLayoutPanel1.Controls.Add(label2, 1, count);
Console.WriteLine(count);
count++;
}

}

connection.Close();
However count and i never gets increased, so rather than being like

job1 - some text
job2 - some text
job3 - some text

The table looks like

job1 - job2
job3 - some text
some text - some text

What can I do to fix this?

alykins
08-23-2012, 02:49 PM
I looked @ that class- it seems from MSDN documentation that that control is more for laying out other controls rather than trying to display data (it references that you can add controls to it and it allows them to be manipulated at runtime). If you are trying to bind data to an actual grid you should use a datagridview. what you will do then is @ your reader, you will load a datatable with the reader using datatable.load(datareader) method. then you can bind the datatable to the gridview.

tomharto
08-24-2012, 12:39 PM
I looked @ that class- it seems from MSDN documentation that that control is more for laying out other controls rather than trying to display data (it references that you can add controls to it and it allows them to be manipulated at runtime). If you are trying to bind data to an actual grid you should use a datagridview. what you will do then is @ your reader, you will load a datatable with the reader using datatable.load(datareader) method. then you can bind the datatable to the gridview.

Thanks for that :). I'll go take a look at datagridview and datatables.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum