Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    [C#] Write out a query result into a TableLayoutPanel

    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.

    Code:
    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?

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,806
    Thanks
    41
    Thanked 199 Times in 198 Posts
    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.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by alykins View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •