如何从datatable获取数据并根据行数重复它 [英] how to get data from datatable and repeate it according to number of rows

查看:105
本文介绍了如何从datatable获取数据并根据行数重复它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何从数据表中获取数据并根据行数重复数据?

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

我的数据表如下:

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;


      }

我需要的是在表单上为MemberCode生成标签并根据记录数重复。

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

假设数据表有4条来自MemberCode 1001到1004的记录,那么它将如下所示

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

窗口形式showdata

window form showdata

MemberCode: 1001&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;会员代码:1002

MemberCode : 1001                            Membercode : 1002

会员代码:1003  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  会员代码:1004

MemberCode :1003                             Membercode : 1004

以便如何通过csharp windows在visual studio 2015上进行设计?

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

推荐答案

您好,

看看使用TableLayoutPanel或FlowLayoutPanel。

Look at using either a TableLayoutPanel or FlowLayoutPanel.

在普通面板中创建的TableLayoutPanel的示例。为了清晰起见,面板具有背景颜色。您也可以直接将控件添加到表单中。所以要明确的是,Flow vs Table布局控件的优势互为
,或许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:" +
"MemberCode : " +


" {dt.Rows [rowIndex] .Field< int>(" MemberCode")}",
Anchor = AnchorStyles.Left,AutoSize = true},0,rowIndex);
}

panel1.Controls.Add(table);
}
}
}
"{dt.Rows[rowIndex].Field<int>("MemberCode")}", Anchor = AnchorStyles.Left, AutoSize = true }, 0, rowIndex); } panel1.Controls.Add(table); } } }

FlowLayoutPanel我把它放在表单上并调整大小。

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 =


这篇关于如何从datatable获取数据并根据行数重复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆