locked
Retrieve data on a Windows form as labels and textboxes. RRS feed

  • Question

  • Hello,

    I have a table as following Table1{AccNo, AccDescription, AccValue, AccMonth, AccYear}. For each month I have about 50 rows that i want to be shown in a form. I need the AccAccount and AccDescription shown as label and AccValue as textbox.

    I made the selection based on month and year, but how can i make the data shown like i mentioned? (I don't want them to appear as datagrid though)

    Is this possible?

    Thank you in advance.

    • Moved by CoolDadTx Thursday, March 22, 2012 1:48 PM Winforms related (From:Visual C# General)
    Thursday, March 22, 2012 12:15 PM

Answers

  • I have a table as following Table1{AccNo, AccDescription, AccValue, AccMonth, AccYear}. For each month I have about 50 rows that i want to be shown in a form. I need the AccAccount and AccDescription shown as label and AccValue as textbox.

    So you have to Create controls for each row? I means if there is 50 rows, the code has to create 50 contorls of label, and 50 textBoxes. And allign them accordingly (one under another).

    Am I right?

    Is so here is my code I did:

     public partial class Form1 : Form
        {
            Label[] lbs;
            TextBox[] tbs;      
            public Form1()
            {
                InitializeComponent();
    
                DataTable table = new DataTable(); //you will it up...
                //I will create columns manually:
                table.Columns.AddRange(new DataColumn[] { new DataColumn("AccNumber", typeof(string)), new DataColumn("AccDescription", typeof(string)) });
                //I will create 5 rows for example:
                for (int i = 0; i < 5; i++)
                    table.Rows.Add("AccNumbe " + i, "AccDesc " + i);
    
                //now that we have fill datatable, we can dinamically create controls:
                lbs = new Label[table.Rows.Count];
                tbs = new TextBox[table.Rows.Count];            
                //initial location:
                int x = 20, y = 20;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                     //creating new label:
                    lbs[i] = new Label();
                    lbs[i].Name = "label" + (i + 1);
                    lbs[i].AutoSize = true;
                    lbs[i].Location = new Point(x, y);
    
                    //creating new textBox:
                    tbs[i] = new TextBox();
                    tbs[i].Name = "textBox" + (i + 1);
                    tbs[i].Size = new Size(125, 23);
                    tbs[i].Location = new Point(x + 75, y - 3); // textBoxX will be locsted 40px right of labelX
    
                    this.Controls.AddRange(new Control[] { lbs[i], tbs[i] });
                 
                    //passing data to each control:
                    lbs[i].Text = table.Rows[i]["AccNumber"].ToString() + ":";
                    tbs[i].Text = table.Rows[i]["AccDescription"].ToString();
    
                    //setting new position for next pair of controls:
                    y += 30;
                }            
            }
        }

    NOTE: please try only this code which creates 5 individual pairs. See how it works out. The problem would be to LOCATE your controls, because I dount you will have 50 controls one under another (it will become too long in depth), so you will have to create 2 or 3 columns.

    Hope it helps,

    bye


    Mitja

    • Proposed as answer by Bob Wu-MT Tuesday, March 27, 2012 7:31 AM
    • Marked as answer by Mellinda Dealy Friday, March 30, 2012 10:53 AM
    Thursday, March 22, 2012 1:08 PM

All replies

  • It's possible but I'd think again. You could use a listview or listbox rather than labels and textboxes. Although a DGV might really be better. Why not use one?

    Regards David R
    ---------------------------------------------------------------
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, March 22, 2012 12:31 PM
  • I have a table as following Table1{AccNo, AccDescription, AccValue, AccMonth, AccYear}. For each month I have about 50 rows that i want to be shown in a form. I need the AccAccount and AccDescription shown as label and AccValue as textbox.

    So you have to Create controls for each row? I means if there is 50 rows, the code has to create 50 contorls of label, and 50 textBoxes. And allign them accordingly (one under another).

    Am I right?

    Is so here is my code I did:

     public partial class Form1 : Form
        {
            Label[] lbs;
            TextBox[] tbs;      
            public Form1()
            {
                InitializeComponent();
    
                DataTable table = new DataTable(); //you will it up...
                //I will create columns manually:
                table.Columns.AddRange(new DataColumn[] { new DataColumn("AccNumber", typeof(string)), new DataColumn("AccDescription", typeof(string)) });
                //I will create 5 rows for example:
                for (int i = 0; i < 5; i++)
                    table.Rows.Add("AccNumbe " + i, "AccDesc " + i);
    
                //now that we have fill datatable, we can dinamically create controls:
                lbs = new Label[table.Rows.Count];
                tbs = new TextBox[table.Rows.Count];            
                //initial location:
                int x = 20, y = 20;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                     //creating new label:
                    lbs[i] = new Label();
                    lbs[i].Name = "label" + (i + 1);
                    lbs[i].AutoSize = true;
                    lbs[i].Location = new Point(x, y);
    
                    //creating new textBox:
                    tbs[i] = new TextBox();
                    tbs[i].Name = "textBox" + (i + 1);
                    tbs[i].Size = new Size(125, 23);
                    tbs[i].Location = new Point(x + 75, y - 3); // textBoxX will be locsted 40px right of labelX
    
                    this.Controls.AddRange(new Control[] { lbs[i], tbs[i] });
                 
                    //passing data to each control:
                    lbs[i].Text = table.Rows[i]["AccNumber"].ToString() + ":";
                    tbs[i].Text = table.Rows[i]["AccDescription"].ToString();
    
                    //setting new position for next pair of controls:
                    y += 30;
                }            
            }
        }

    NOTE: please try only this code which creates 5 individual pairs. See how it works out. The problem would be to LOCATE your controls, because I dount you will have 50 controls one under another (it will become too long in depth), so you will have to create 2 or 3 columns.

    Hope it helps,

    bye


    Mitja

    • Proposed as answer by Bob Wu-MT Tuesday, March 27, 2012 7:31 AM
    • Marked as answer by Mellinda Dealy Friday, March 30, 2012 10:53 AM
    Thursday, March 22, 2012 1:08 PM
  •  

    @Mitja Bonca Thank you i will try this.

    @Riced I know that setting up dynamically labels and textboxes in probably not the best solution, but i believe DGV is not suitable in my case.

    I give you the whole concept, maybe there is better way to be done.

    I have a table in SQL with Table1 {AccNo, AccDescription, AccValue, AccMonth, AccYear}. For each month I have about 50 - 90 rows. These records must appeared in a groupbox and for each record,  I have additional textboxes in other groupbox that user can add more info.

    If i use DGV i won't be able to separate the different groups of fields, which is important part of the application. The user will view all columns as a table. Please correct me if I am wrong.

    The two solutions i figured were (a)dynamically labels and textboxes and (b) create an SQL Script that imports data of Table1 to Table2{Acc_Month, Acc_Year, Acc_Account,AccDescription1,AccDescription2,....,AccDescription90} and each AccDescription(i) field will get the AccValue of the proper AccDescription. Pretty mess huh? :P

    Well, between the 2 above solutions, i suppose first one is better.

    I'm open though to better solutions because I know there are.

    • Proposed as answer by Bob Wu-MT Tuesday, March 27, 2012 7:31 AM
    Friday, March 23, 2012 8:49 AM
  • Is it possible to use a separate dgv for each group and use an id to keep all in sync?

    Bill

    Tuesday, March 27, 2012 1:16 AM
  • Hi dealwi8me,

    I’m not sure what’s exactly what are you trying to do.

    However, if you trying to group data, you can use ListView, see http://msdn.microsoft.com/en-us/library/ezh1batz.aspx.

    Hi Bill,

    By default, DataGridView doesn’t support group, but there are lots of source online. Here is one of them.

    http://www.codeproject.com/Articles/14388/OutlookGrid-grouping-and-arranging-items-in-Outloo

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 27, 2012 7:40 AM
  • Strange ppl walking around. They ask question (I think because they need some help), and then nothing. Like they have forgotten about asking something at all.

    Hmm..


    Mitja

    Tuesday, March 27, 2012 7:57 AM
  • Hi dealwi8me,
    Would you mind let us know the result?
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 28, 2012 2:53 AM
  • @Mitja Bonca, if you check the threads I've started in the past I always check the answers and thanked everyone for answering. I couldn't answer faster through some personal issues.  I'm not "strange" I just had some rough days. Anyway.... I think your solution is the best for my application.

    Thanks again everyone and my apologies for the late response.

    Friday, March 30, 2012 10:53 AM
  • Great to hear that. I really did check, and you dont lie :)

    I was kidding with saying that, dont take this personally mate.  And Im glad you like my coding.

    best regards

    bye


    Mitja

    Friday, March 30, 2012 6:13 PM