none
how to get data from datatable and repeate it according to number of rows RRS feed

  • Question

  • Problem

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

    my datatable as following

    public DataTable GetChecked()
          {
              DataTable table = new DataTable();
              table.Columns.Add("MemberCode", typeof(string));
              table.Columns.Add("MemberImage", typeof(Byte[]));
    
              for (int i = 0; i < dtDisplayDataPayment.Rows.Count; i++)
              {
    
                  bool Ischecked = Convert.ToBoolean(GridFooter.Rows[i].Cells["PrintFlag"].Value);
                  if (Ischecked == true)
                  {
                      DataRow newRow = table.NewRow();
                      newRow["MemberCode"] = Utilities.ObjectConverter.ConvertToString(dtDisplayDataPayment.Rows[i]["MemberCode"]);
    
    
    
                          if (dtDisplayDataPayment.Rows[i]["MemberImage"] != System.DBNull.Value)
                          {
                              byte[] photo_aray = (byte[])dtDisplayDataPayment.Rows[i]["MemberImage"];
                              System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                              Image img = (Image)converter.ConvertFrom(photo_aray);
                              newRow["MemberImage"] = imageToByteArray(img);
    
    
    
                      }
                      table.Rows.Add(newRow);
                  }
              }
    
              return table;
    
    
          }

    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 ?

    Saturday, February 2, 2019 7:27 PM

All replies

  • Hello,

    Look at using either a TableLayoutPanel or FlowLayoutPanel.

    An example of a TableLayoutPanel created in a normal panel. Where the panel has a background color for clarity of this example. You can also simply add the control directly to the form. So to be clear, the Flow vs Table layout controls have a avantages over each other, perhaps FlowLayoutPanel is more to your liking.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var dt = new DataTable();
    
                dt.Columns.Add(new DataColumn() {ColumnName = "MemberCode", DataType = typeof(int)});
    
                dt.Rows.Add(1001);
                dt.Rows.Add(1002);
                dt.Rows.Add(1003);
                dt.Rows.Add(1004);
    
                var table = new TableLayoutPanel {BorderStyle = BorderStyle.None, Height = 55};
    
                for (var rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                {
                    table.Controls.Add(new Label { Text = $"MemberCode : " +
                        $"{dt.Rows[rowIndex].Field<int>("MemberCode")}",
                        Anchor = AnchorStyles.Left, AutoSize = true }, 0, rowIndex);
                }
    
                panel1.Controls.Add(table);
            }
        }
    }

    FlowLayoutPanel where I simply dropped it on the form and sized it.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var dt = new DataTable();
    
                dt.Columns.Add(new DataColumn() {ColumnName = "MemberCode", DataType = typeof(int)});
    
                dt.Rows.Add(1001);
                dt.Rows.Add(1002);
                dt.Rows.Add(1003);
                dt.Rows.Add(1004);
    
    
                for (var rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                {
                    var button = new Label()
                    {
                        AutoSize  = true,
                        Text = $"Member Code : {dt.Rows[rowIndex].Field<int>("MemberCode")}"
                    };
    
                    flowLayoutPanel1.Controls.Add(button);
                }
    
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, February 3, 2019 11:35 PM
    Moderator
  • thank you for reply

    this is very good idea and actually this is what i need

    but if i need to add image to every membercode as below

    membercode : 1001                 membercode :1002

    image                                      image

    membercode:1003                    membercode  :1004

    image                                       image                                                   

      suppose this image is picture box

    if any controls do that no problems

    but how to do that     

    Monday, February 4, 2019 1:44 AM
  • thank you for reply

    this is very good idea and actually this is what i need

    but if i need to add image to every membercode as below

    membercode : 1001                 membercode :1002

    image                                      image

    membercode:1003                    membercode  :1004

    image                                       image                                                   

      suppose this image is picture box

    if any controls do that no problems

    but how to do that     

    You should always provide all details in your initial post so for me the original question has been given a resolution.

    Neither the table or flow layout panels are choices for text and image. If I wanted to display both member code and image it would be with a ListBox and PictureBox where both would be data bound to the DataTable.  The ListBox DisplayMember would be the code field and the PictureBox would be bound to the image field which is a byte array so it will need to be converted from byte array to image. Not as simple as your initial question. I had time earlier to provide some code but not now.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 4, 2019 2:03 AM
    Moderator
  • Hi  ahmedabosalah,

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

    You can try to sort rows in a data table.

    Sorting rows in a data table:


    Then, traverse the data table and display in the data control. 

    Usually, we will use the datagridview winform control to show a list data. It also contains a DataGridViewImageColumn which can help you show a image. 

    If you want to map image dynamically then you'll need to handle the CellFormatting event and set the FormattedValue property in the CellFormatting Event Args to the specific image that you want based upon your DB value.

    How to show images on datagridview for each row or show image on datagridview somehow?

    How to: Display Images in Cells of the Windows Forms DataGridView Control


    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue. If you have a new question you can start a new thread  with all necessary code snippets for anyone else to be able to reproduce your issue.

    Thank you for your understanding.
     

    Best Regards,

    Yong Lu

      

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, February 13, 2019 2:26 AM
    Monday, February 4, 2019 2:52 AM
    Moderator