效果图:选择不同的内容,会从数据库读取对应的数据,并加载到Panel中
以下代码控制一行显示4个:
private void Step1项目_项目类型_TextChanged(object sender, EventArgs e)
{
Step1Panel.Controls.Clear();
DataTable dt = BLL.L_Config.GetValue(Step1项目_项目类型.Text+"2");
for (int i = 0; i < dt.Rows.Count; i++)
{
int rowIndex = i / 4;
int columnIndex = i % 4;
CheckBox chb = new CheckBox();
chb.AutoSize = true;
chb.BringToFront();
chb.Name = "Step1" + "_" + i.ToString();
chb.Text = dt.Rows[i]["Value"].ToString();
chb.Tag = dt.Rows[i]["Type"].ToString();
chb.ForeColor = Color.Black;
if (columnIndex == 0)
{
chb.Location = new Point(6, 3 + rowIndex * 20);
}
else if (columnIndex == 1)
{
chb.Location = new Point(166, 3 + rowIndex * 20);
}
else if (columnIndex == 2)
{
chb.Location = new Point(316, 3 + rowIndex * 20);
}
else if (columnIndex == 3)
{
chb.Location = new Point(486, 3 + rowIndex * 20);
}
Step1Panel.Controls.Add(chb);
}
}
在加载新窗体时,把之前选中的项进行选中
DataTable dtDetail = BLL.L_BillDetail.GetDetail(BillID);
foreach (Control c in Step1Panel.Controls)
{
if (c.GetType().Name == "CheckBox")
{
(c as CheckBox).Checked = BLL.L_BillDetail.GetValue(dtDetail, c.Tag.ToString()) == "Y";
}
}
public static string GetValue(DataTable dt, string Tag)
{
try
{
return dt.Select("Tag = '"+Tag+"'")[0]["Value"].ToString();
}
catch
{
return "";
}
}
设计的表结构: