很多系统都带有自定义报表的功能,而此功能都需依参数自动生成控件,举例如下:
如上图,一条查询语句当中,包含了3个参数,其中两个是日期型(使用:DATE!进行标识),一个是字符型(使用:进行标识),要生成的效果图如下所示:
代码如下:
private void FrmDefine_MyEventClose(string strID, string strName, string strSql)
{
//值传递
ReturnID = strID;
ReturnName = strName;
ReturnSql = strSql;
//空格清除
while (ReturnSql.IndexOf(": ") != -)
{
ReturnSql = ReturnSql.Replace(": ", ":");
}
while (ReturnSql.IndexOf(":DATE! ") != -)
{
ReturnSql = ReturnSql.Replace(":DATE! ", ":DATE!");
}
//产生标题
rtxtName.Text = "[" + strID + "]" + strName;
rtxtName.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Center;
//清除控件
splitContainer2.Panel1.Controls.Clear();
//添加控件
if (strSql.IndexOf(":") != -)
{
int ParamIndex = ; //参数序号,从100往前倒数。
ParamSql = string.Empty;
List<string> list = new List<string>(ReturnSql.Split(' '));
for (int i = list.Count - ; i > -; i--)
{
if (list[i].IndexOf(":") == -)
{
ParamSql = list[i] + " " + ParamSql;
list.RemoveAt(i);
}
else
{
if (list[i].IndexOf(":DATE!") != -)
{
if (list[i].IndexOf(":DATE!") == )
{
ParamSql = "Params[" + ParamIndex.ToString() + "] " + ParamSql;
ParamIndex--;
}
else
{
ParamSql = list[i].Substring(, list[i].IndexOf(":DATE!")) + "Params[" + ParamIndex.ToString() + "] " + ParamSql;
ParamIndex--;
}
}
else
{
if (list[i].IndexOf(":") == )
{
ParamSql = "Params[" + ParamIndex.ToString() + "] " + ParamSql;
ParamIndex--;
}
else
{
ParamSql = list[i].Substring(, list[i].IndexOf(":")) + "Params[" + ParamIndex.ToString() + "] " + ParamSql;
ParamIndex--;
}
}
}
}
//添加控件
if (list.Count > )
{
//坐标初始化
Point point = new Point(, );
//控件行数
int intRow = ;
//以6个参数为一行,初始化控件面板的高度。
splitContainer2.SplitterDistance = * Convert.ToInt32(Math.Ceiling(list.Count / 6.0));
//控件添加
ParamIndex = - list.Count + ;
for (int i = ; i < list.Count; i++)
{
string strLabelText = string.Empty;
if (i % == && i != )
{
intRow++;
point.X = ;
point.Y = + * intRow;
}
//日期及文本
if (list[i].IndexOf(":DATE!") != -)
{
//Label
strLabelText = list[i].Substring(list[i].IndexOf(":DATE!") + , list[i].Length - list[i].IndexOf(":DATE!") - );
Label label = new Label();
label.Name = "Label" + i.ToString();
label.Text = strLabelText;
label.AutoSize = true;
label.Location = point;
splitContainer2.Panel1.Controls.Add(label);
//DateTimePicker
DateTimePicker dateTimePicker = new DateTimePicker();
dateTimePicker.Name = "Params[" + ParamIndex.ToString() + "]";
dateTimePicker.Format = DateTimePickerFormat.Custom;
dateTimePicker.CustomFormat = "yyyy-MM-dd";
dateTimePicker.Width = ;
point.X = point.X + label.Width + ;
point.Y = label.Location.Y - ;
dateTimePicker.Location = point;
splitContainer2.Panel1.Controls.Add(dateTimePicker);
//坐标初始化
point.X += ;
point.Y = label.Location.Y;
}
else
{
//Label
strLabelText = list[i].Substring(list[i].IndexOf(":") + , list[i].Length - list[i].IndexOf(":") - );
Label label = new Label();
label.Name = "Label" + i.ToString();
label.Text = strLabelText;
label.AutoSize = true;
label.Location = point;
splitContainer2.Panel1.Controls.Add(label);
//TextBox
TextBox textBox = new TextBox();
textBox.Name = "Params[" + ParamIndex.ToString() + "]";
textBox.Text = string.Empty;
textBox.Width = ;
point.X = point.X + label.Width + ;
point.Y = label.Location.Y - ;
textBox.Location = point;
splitContainer2.Panel1.Controls.Add(textBox);
//坐标初始化
point.X += ;
point.Y = label.Location.Y;
}
//参数序号赋值
ParamIndex++;
}
}
}
//数据源初始化
dataGridView1.DataSource = null;
}
好了,分享就到这里,希望对大家有一些帮助。