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