foreach DataTable或Table时要略过第一行。

昨天有续写一个练习《输入数字动态创建行(二)http://www.cnblogs.com/insus/p/4916260.html ,最终是需要把数据存入数据库中。

在循环ASP:Table时,我们需要去除表第一行表头。
当时Insus.NET的做法,是使用for来实现:

 


改变之后,使用GetRowIndex()方法,可获取Row的索引。

 
扩展,根把此方法,你可以获取任意一行的索引,这样不必使用for来实现。当然你使用for也没有任何问题,多一种方法而已。



using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Windows.Forms; namespace SPT_Monitor { public partial class PopFormParam : Form { private MainForm mainForm; public DataTable tableData = new DataTable(); public DataGridView dgParams = new DataGridView(); private bool[] groupStates = { false, false, false, false, false }; public string[] groupNames = { "P0", "C1", "C2", "C3", "C4" }; public string[] subColHeader = { "出荷", "使用值", "最小单位", "备注" }; public PopFormParam(MainForm mainForm) { InitializeComponent(); this.mainForm = mainForm; //表格基础样式 dgParams.Dock = DockStyle.Fill; dgParams.AllowUserToAddRows = false; dgParams.AllowUserToDeleteRows = false; dgParams.AllowUserToOrderColumns = false; dgParams.AllowUserToResizeRows = false; dgParams.EnableHeadersVisualStyles = false; // 确保表头样式生效 dgParams.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke; // 奇偶行异色 dgParams.RowHeadersVisible = false; // 隐藏行号列 dgParams.ColumnHeadersHeight = 80; // 表头高度(适配合并表头) // 表格添加到指定面板 pParams.Controls.Add(dgParams); //初始化表格列 dgParams.ColumnCount = 4 + 5 * 4; dgParams.Columns[0].Name = "No."; dgParams.Columns[1].Name = "内容"; dgParams.Columns[2].Name = "表示范围"; dgParams.Columns[3].Name = "单位"; dgParams.Columns[0].ReadOnly = true; dgParams.Columns[1].ReadOnly = true; dgParams.Columns[2].ReadOnly = true; dgParams.Columns[3].ReadOnly = true; dgParams.Columns[0].Width = 40; // No.列固定宽度 dgParams.Columns[1].Width = 220; // 内容列固定宽度 dgParams.Columns[2].Width = 80; // 表示范围列固定宽度 dgParams.Columns[3].Width = 60; // 单位列固定宽度 dgParams.CellPainting += (sender, e) => { if (e.RowIndex == -1 && e.ColumnIndex >= 0) { e.PaintBackground(e.CellBounds, true); //前4列不用分割,直接绘制内容 if (e.ColumnIndex < 4) { e.PaintContent(e.CellBounds); e.Handled = true; return; } //计算组索引和组内索引列(5组,每组4列) int groupIndex = (e.ColumnIndex - 4) / 4; int columInGroup = (e.ColumnIndex - 4) % 4; if(columInGroup == 0) { int groupWidth = 0; for(int j = 0; j < 4; j++) { groupWidth += dgParams.Columns[4 + groupIndex * 4 + j].Width; } Rectangle groupRect = new Rectangle(e.CellBounds.Left, e.CellBounds.Top, groupWidth, e.CellBounds.Height / 2); // 绘制组标题 using (StringFormat groupFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { string groupHeader = groupNames[groupIndex]; // 使用组名数组 e.Graphics.DrawString(groupHeader, dgParams.Font, Brushes.Black, groupRect, groupFormat); } } // 在每组所有4列的下半部分绘制子标题 int subColIndex = columInGroup; if (subColIndex < subColHeader.Length) { // 创建子标题矩形(高度为单元格下半部分) Rectangle subRect = new Rectangle(e.CellBounds.Left, e.CellBounds.Top + e.CellBounds.Height / 2, e.CellBounds.Width,e.CellBounds.Height / 2 ); // 绘制子标题 using (StringFormat subFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) using (Font subFont = new Font(dgParams.Font.FontFamily, dgParams.Font.Size - 1)) { e.Graphics.DrawString(subColHeader[subColIndex], subFont, Brushes.Black, subRect, subFormat); } } // 绘制分组分隔线(组内列的竖线) using (Pen pen = new Pen(Color.LightGray, 1)) { // 上半部分不绘制竖线(合并组标题) // 仅在下半部分绘制列分隔线 int midY = e.CellBounds.Top + e.CellBounds.Height / 2; e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, midY, e.CellBounds.Right - 1, e.CellBounds.Bottom); } e.Handled = true; } }; } private void PopFormParam_FormClosed(object sender, FormClosedEventArgs e) { mainForm.popDlgs[0] = null; mainForm.toggleBtn(mainForm.popBtns[0], false, "P参数\r\n关"); } } } 解决上半部分没有显示P0等数据的问题
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值