Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

how to get data from data table and repeat it according to number of rows ?

my datatable name is GetChecked

what i need is to generate label for MemberCode on form and repeted based on number of records .

Suppose datatable have 4 records from MemberCode 1001 to 1004 then it will be as below

window form showdata

MemberCode : 1001                             Membercode : 1002

MemberCode : 1003                             Membercode : 1004


so that how to make design above by csharp windows form visual studio 2015 ?

What I have tried:

public DataTable GetChecked()
      {
          DataTable table = new DataTable();
          table.Columns.Add("MemberCode", typeof(string));
          

          for (int i = 0; i < dtDisplayDataPayment.Rows.Count; i++)
          {

              
                  DataRow newRow = table.NewRow();
                  newRow["MemberCode"] = Utilities.ObjectConverter.ConvertToString(dtDisplayDataPayment.Rows[i]["MemberCode"]);



                    
                  table.Rows.Add(newRow);
              }
          

          return table;


      }
Posted
Updated 4-Feb-19 1:38am
v2

See example here: c# - How to create many labels and textboxes dynamically depending on the value of an integer variable? - Stack Overflow[^]

Instead of using this.Controls.Add() which places all labels directly on the form, I would advise to use a Panel or GroupBox as a container:
C#
this.GroupBox1.Controls.Add(labels[i]);
 
Share this answer
 
If i understand you well...

C#
DataTable dt = GetChecked();
int h = 24;
for(int i=0; i<dt.Rows.Count; i++)
{
    DataRow r = dt.Rows[i];

    Label lbl = new Label();
    lbl.Name = string.Format("Label", i);
    lbl.Location = new System.Drawing.Point(20, h*i+8);
    lbl.Width = 120;
    lbl.Height = h;
    lbl.Parent = this; //or this.GroupBox1;
    lbl.Text = r["MemberCode"].ToString();
}


BUT...

I'd suggest to use DataGridView control[^] for such of requirement for set of reson. One of them is a easy way to bind and display data:
C#
this.DataGridView1.DataSource = GetChecked();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900