勇哥的一个案子里面用到这个功能。
因为考虑到数据库的表字段没有定形,经常需要修改。如果使用手工拖放TextBox的方案与字段对应的话,程序经常需要改变。
因此最好是由数据库表的字段,动态生成TextBox进行显示与编辑,就像下图程序界面这样。

首先,我们列出表结构,获取全部字段。
然后new出textbox控件,add进控件flowLayoutPanel,这个控件是其它控件的容器,内容过多时自带滚动条。
怎么获取sql server中表的结构呢,请看勇哥另一个贴子:
Sql Server 2008R2查看表结构
实现比较简单,代码如下,请各位参考。如果有函数找不原型可以找勇哥索要。
另外,那个臃肿的case是勇哥偷了个懒,这些read系列的函数原本应该有个父类,不至于在这里这么重复书写以至污了各位的法眼,抱歉各位。
using algx.coslightx.fmsx.appx;
using MSSQL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public struct tabFielesObj
{
public int index;
//字段的名字,也是控件的名字
public string textName;
public FieldSet_TextBox textObj;
//字段数据类型
public string dataType;
public string fieldValue;
}
public List<tabFielesObj> tObj = new List<tabFielesObj>();
string[] tabNameAry = new string[]
{
"B线OCV",
"B线PPG尺寸测量",
"B线tco焊机",
"C线tco焊机",
"B线边电压",
"B线三封机",
"D线分档机",
"美纹胶",
"喷码机",
"切极耳",
"撕膜机",
"高温胶机",
"正压测漏"
};
public SQLServerClient sql = new SQLServerClient();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if(index>=0)
{
tObj.Clear();
createFieldObj(tabNameAry[index]);
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (!Init())
{
MessageBox.Show("登陆失败!");
return;
}
listBox1.Items.Clear();
listBox1.Items.AddRange(tabNameAry);
}
private void createFieldObj(string tabName)
{
var ary1 = sql.read读表结构(tabName);
flowLayoutPanel1.Controls.Clear();
int js1 = 0;
foreach (var m in ary1)
{
var obj = new FieldSet_TextBox()
{
caption = m.Item1,
Name = m.Item1,
txtWidth = 135
};
flowLayoutPanel1.Controls.Add(obj);
tObj.Add(new tabFielesObj()
{
index = js1,
textName = m.Item1,
dataType = m.Item2,
fieldValue = "",
textObj = obj
});
++js1;
}
updateTextValue();
}
private void updateTextValue()
{
switch (listBox1.SelectedIndex)
{
case 0:
var v = sql.readOCV(tbxCurBattery.Text);
if (null == v) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 1:
var v1 = sql.readPPG(tbxCurBattery.Text);
if (null == v1) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v1);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 2:
var v2 = sql.read焊机(tbxCurBattery.Text);
if (null == v2) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v2);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 3:
var v3 = sql.readtco焊机(tbxCurBattery.Text);
if (null == v3) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v3);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 4:
var v4 = sql.read边电压(tbxCurBattery.Text);
if (null == v4) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v4);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 5:
var v5 = sql.read封机(tbxCurBattery.Text);
if (null == v5) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v5);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 6:
var v6 = sql.read分档(tbxCurBattery.Text);
if (null == v6) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v6);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 7:
var v7 = sql.read美胶(tbxCurBattery.Text);
if (null == v7) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v7);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 8:
var v8 = sql.read喷码机(tbxCurBattery.Text);
if (null == v8) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v8);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 9:
var v9 = sql.read切极耳(tbxCurBattery.Text);
if (null == v9) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v9);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 10:
var v10 = sql.read切(tbxCurBattery.Text);
if (null == v10) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v10);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 11:
var v11 = sql.read撕膜(tbxCurBattery.Text);
if (null == v11) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v11);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 12:
var v12 = sql.read贴胶机(tbxCurBattery.Text);
if (null == v12) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v12);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
case 13:
var v13 = sql.read测漏(tbxCurBattery.Text);
if (null == v13) return;
for (int i = 0; i < tObj.Count; i++)
{
var fvalue = GetModelValue(tObj[i].textName, v13);
tObj[i] = new tabFielesObj()
{
index = i,
textName = tObj[i].textName,
dataType = tObj[i].dataType,
fieldValue = fvalue,
textObj = tObj[i].textObj
};
tObj[i].textObj.text = tObj[i].fieldValue;
}
break;
}
}
private bool Init()
{
var conn = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",
tbxServerName.Text, "guangyu", tbxUser.Text, tbxPassword.Text);
try
{
return sql.ConnectLib(conn);
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 获取类中的属性值
/// </summary>
/// <param name="FieldName"></param>
/// <param name="obj"></param>
/// <returns></returns>
public string GetModelValue(string FieldName, object obj)
{
try
{
var Ts = obj.GetType();
object o = Ts.GetProperty(FieldName).GetValue(obj, null);
string Value = Convert.ToString(o);
if (string.IsNullOrEmpty(Value)) return null;
return Value;
}
catch
{
return null;
}
}
}
}---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


少有人走的路


















