数据填充方式XML/JSON预览。

一:xml填充:更加方便右击主对象增加明细网格

增加列组合

右击记录集增加字段集合

选中内容行列设置数据字段

例子测试:

using gregn6Lib;
namespace StudentTest
{
    public partial class Form1 : Form
    {
        private GridppReport report = new GridppReport();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            report.Clear();
            report.PrepareLoadData();
            report.LoadFromFile(@"MyStudent.grf");
            report.LoadDataFromXML(GetAllBodyXML());
            report.PrintPreview(true);
        }
        private string GetAllBodyXML()
        {
            XmlDocument bodyDoc = new XmlDocument();
            XmlElement element = bodyDoc.CreateElement("BarCode");
            bodyDoc.AppendChild(element);
            XmlNode rootNode = bodyDoc.SelectSingleNode("BarCode");
            XmlElement xe = null;
            DataTable dtUnique = new DataTable();
            dtUnique.Columns.Add(new DataColumn("StudentAge"));
            dtUnique.Columns.Add(new DataColumn("StudentName"));
            dtUnique.Columns.Add(new DataColumn("StudentClass"));
            DataRow dr = dtUnique.NewRow();
            dr["StudentAge"] = "18";
            dr["StudentName"] = "小明";
            dr["StudentClass"] = "三年二班";
            dtUnique.Rows.Add(dr);
            DataRow dr2 = dtUnique.NewRow();
            dr2["StudentAge"] = "28";
            dr2["StudentName"] = "小明";
            dr2["StudentClass"] = "三年二班";
            dtUnique.Rows.Add(dr2);
            //方式一自己拼接
            //foreach (DataRow dr2 in dtUnique.Rows)
            //{
            //        xe = bodyDoc.CreateElement("Details");
            //        xe.SetAttribute("StudentAge", Convert.ToString(dr["StudentAge"]));
            //        xe.SetAttribute("StudentName", Convert.ToString(dr["StudentName"]));
            //        xe.SetAttribute("StudentClass", Convert.ToString(dr["StudentClass"]));
            //        rootNode.AppendChild(xe);
            //}
            //return bodyDoc.OuterXml;
            //方式二利用dataset更加方便
            DataSet ds = new DataSet();
            ds.Tables.Add(dtUnique);
            return  ds.GetXml();
        }  
    }
}

二:测试二

右击增加明细网格,增加自由表格,增加记录集,条形码中点击文本插入字段

        private void button1_Click(object sender, EventArgs e)
        {
            report.Clear();
            report.PrepareLoadData();
            report.LoadFromFile(@"Test.grf");
            report.LoadDataFromXML(GetAllBodyXML());
            report.PrintPreview(true);
        }
        private string GetAllBodyXML()
        {
           //测试二
            DataTable dtUnique = new DataTable();
            dtUnique.Columns.Add(new DataColumn("cInvCode"));
            dtUnique.Columns.Add(new DataColumn("cInvName"));
            dtUnique.Columns.Add(new DataColumn("cInvStd"));
            dtUnique.Columns.Add(new DataColumn("cBarCode"));
            DataRow dr = dtUnique.NewRow();
            dr["cInvCode"] = "147258369";
            dr["cInvName"] = "德芙巧克力";
            dr["cInvStd"] = "30mm*20mm";
            dr["cBarCode"] = "6943620580207";
            dtUnique.Rows.Add(dr);
            DataRow dr2 = dtUnique.NewRow();
            dr2["cInvCode"] = "123456789";
            dr2["cInvName"] = "杯子";
            dr2["cInvStd"] = "500mm*200mm";
            dr2["cBarCode"] = "6943620580207";
            dtUnique.Rows.Add(dr2);
            DataSet ds = new DataSet();
            ds.Tables.Add(dtUnique);
            return ds.GetXml();
        }  
    }

测试二

<Data>
	<Rows>
		<Row>
		 <cInvCode>02001</cInvCode>
		 <cInvName>存货A</cInvName>
		 <cBarCode>200,2jjj,698</cBarCode>
		 <cVenName>供应商A</cVenName>
		 <cInvStd>2018*20mm</cInvStd>
		 <PackSize>200</PackSize>
		 <iQuantity>30</iQuantity>
		 <cBatch>20180913</cBatch>
		</Row>
		<Row>
		 <cInvCode>02002</cInvCode>
		 <cInvName>存货B</cInvName>
		 <cBarCode>200,2jjj,AA</cBarCode>
		 <cVenName>供应商A</cVenName>
		 <cInvStd>2018*20mm</cInvStd>
		 <PackSize>200</PackSize>
		 <iQuantity>50</iQuantity>
		 <cBatch>20180913</cBatch>
		</Row>
	</Rows>
</Data>

上面的数据保存为xml文件,设置数据源xml即可测试预览

实际应用场景:数据库返回DataTable,转DataSet=>xml填充

引用:gregn6Lib.dll
using gregn6Lib;
//全局变量
private GridppReport report = new GridppReport();

/// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            report.Clear();
            report.PrepareLoadData();
            report.LoadFromFile(@"TVBprint.grf");
            report.LoadDataFromXML(GetAllBodyXML());
            report.PrintPreview(true);
        }

   private string GetAllBodyXML()
        {
            DataTable dtUnique = new DataTable();
            dtUnique.Columns.Add("cInvCode", typeof(string));
            dtUnique.Columns.Add("cInvName", typeof(string));
            dtUnique.Columns.Add("cBarCode", typeof(string));
            dtUnique.Columns.Add("cVenName", typeof(string));
            dtUnique.Columns.Add("cInvStd", typeof(string));
            dtUnique.Columns.Add("PackSize", typeof(double));
            dtUnique.Columns.Add("iQuantity", typeof(double));
            dtUnique.Columns.Add("cBatch", typeof(string));
            //第一行
            DataRow dr = dtUnique.NewRow();
            dr["cInvCode"] = "02001";
            dr["cInvName"] = "存货A";
            dr["cBarCode"] = "02001,20180916,001";
            dr["cVenName"] = "供应商A";
            dr["cInvStd"] = "2018*20mm";
            dr["PackSize"] = 200;
            dr["iQuantity"] = 200;
            dr["cBatch"] = "20180916";
            dtUnique.Rows.Add(dr);
            //第二行
            DataRow dr2 = dtUnique.NewRow();
            dr2["cInvCode"] = "02001";
            dr2["cInvName"] = "存货A";
            dr2["cBarCode"] = "02001,20180916,002";
            dr2["cVenName"] = "供应商A";
            dr2["cInvStd"] = "2018*20mm";
            dr2["PackSize"] = 200;
            dr2["iQuantity"] = 50;
            dr2["cBatch"] = "20180916";
            dtUnique.Rows.Add(dr2);
            DataSet ds = new DataSet();
            ds.Tables.Add(dtUnique);
            return ds.GetXml();
        }

JSON数据InvLabel.grf

{
    "Detail": [
       {"cInvCode":"存货","cInvName":"品名","cInvStd":"存货规格","PackSize":20.0,"iQuantity":20.0, "cBatch":"批次001","cBarCode":"ABCD","cVenName":"ABC","test":"test"}
    ],

    "Master": [
    ]
}

posted @ 2026-09-06 09:38  清哥的码农生活  阅读(13)  评论(0)    收藏  举报