很多系統都帶有自定義報表的功能,而此功能都需依參數自動生成控制項,舉例如下: 如上圖,一條查詢語句當中,包含了3個參數,其中兩個是日期型(使用:DATE!進行標識),一個是字元型(使用:進行標識),要生成的效果圖如下所示: 代碼如下: 好了,分享就到這裡,希望對大家有一些幫助。 ...
很多系統都帶有自定義報表的功能,而此功能都需依參數自動生成控制項,舉例如下:
如上圖,一條查詢語句當中,包含了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; }
好了,分享就到這裡,希望對大家有一些幫助。