예제 #1
0
    public void SetDataSource(BindingSource source)
    {
      tblMain.Controls.Clear();
      tblMain.RowStyles.Clear();
      tblMain.RowCount = 0;

      var dataTypeArray = Enum.GetValues(typeof(QueryParameter.DataType)).OfType<QueryParameter.DataType>().ToArray();
      var props = source.GetItemProperties(null).OfType<PropertyDescriptor>()
        .Where(p => Filter == null || Filter.Contains(p))
        .OrderBy(p => p.DisplayName ?? p.Name)
        .ToArray();

      foreach (var prop in props)
      {
        tblMain.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        tblMain.RowCount += 1;
        var currRow = tblMain.RowCount - 1;

        var lbl = new Label();
        lbl.AutoSize = true;
        lbl.Text = prop.DisplayName ?? prop.Name;
        lbl.Anchor = AnchorStyles.Left;
        tblMain.Controls.Add(lbl, 0, currRow);

        var currProp = prop as QueryParamDescriptor;
        if (currProp != null)
        {
          var box = new ComboBox();
          box.Anchor = AnchorStyles.Left;
          box.DropDownStyle = ComboBoxStyle.DropDownList;
          box.DataSource = Enum.GetValues(typeof(QueryParameter.DataType));
          tblMain.Controls.Add(box, 1, currRow);
          _initialValues.Add(Array.IndexOf(dataTypeArray, currProp.Param.Type));
          box.SelectedIndexChanged += (s, e) =>
          {
            if (!_isLoaded)
              return;

            tblMain.Controls.Remove(tblMain.GetControlFromPosition(2, currRow));
            var newValue = (QueryParameter.DataType)((ComboBox)s).SelectedValue;
            currProp.Param.Type = newValue;
            switch (newValue)
            {
              case QueryParameter.DataType.Boolean:
                currProp.SetValue(source.Current, default(bool));
                AddControl(source, currProp, typeof(bool), currRow);
                break;
              case QueryParameter.DataType.DateTime:
                currProp.SetValue(source.Current, default(DateTime));
                AddControl(source, currProp, typeof(DateTime), currRow);
                break;
              case QueryParameter.DataType.Decimal:
                currProp.SetValue(source.Current, default(decimal));
                AddControl(source, currProp, typeof(decimal), currRow);
                break;
              case QueryParameter.DataType.Integer:
                currProp.SetValue(source.Current, default(long));
                AddControl(source, currProp, typeof(long), currRow);
                break;
              default:
                currProp.SetValue(source.Current, default(string));
                AddControl(source, currProp, typeof(string), currRow);
                break;
            }
          };
        }

        AddControl(source, prop, null, currRow);
      }
    }