public partial class ReportSearch : Base.BaseReportSearchControl
{
public string XmlKey { get; set; }
public ReportSearch(LocalReport localReport)
: base(localReport)
{
InitializeComponent();
}
#region private Variable
Field field = new Field();
#endregion
private void ReportSearch_Load(object sender, EventArgs e)
{
if (FirstLoad)
{
try
{
//rdlc = new RdlcHelper(RptMasterSon.dataTable, ReportMaster.RdlcPath);
field = rdlcField;
colFields.DisplayMember = "Name";
colFields.ValueMember = "Name";
colFields.DataSource = field.FieldList;
colOperator.DataSource = Enum.GetNames(typeof(OperatorEnums));
}
catch (Exception ex)
{
UI.Common.MsgInfo(Properties.Resources.MsgError, ex);
}
}
}
#region Event Function
private void btnDelete_Click(object sender, EventArgs e)
{
dgListDelRow();
}
private void btnEmpty_Click(object sender, EventArgs e)
{
EmptyRow();
}
private void btnSearch_Click(object sender, EventArgs e)
{
string ErrMsg = string.Empty;
bool invalidDate = false;
string strInvDate = string.Format("{0}({1})", Properties.Resources.InvalidDate, NJRLib.Formats.Date);
try
{
Filters filters = new Filters();
//遍历dgList
foreach (DataGridViewRow r in dgList.Rows)
{
DataGridViewCell cell = r.Cells[colValue.Name];
cell.ErrorText = string.Empty;
if (cell.Value != null && !string.IsNullOrEmpty(cell.Value.ToString()))
{
DataGridViewComboBoxCell cField = r.Cells[colFields.Name] as DataGridViewComboBoxCell;
if (cField != null && cField.Tag != null)
{
Field f = cField.Tag as Field;
string operatorType = r.Cells[colOperator.Name].Value.ToString();
string value;
if (r.Cells[colFiltersType.Name].Value != null && (r.Cells[colFiltersType.Name].Value.ToString() == TypeCode.Decimal.ToString() || r.Cells[colFiltersType.Name].Value.ToString() == TypeCode.Double.ToString()))
{
decimal d;
decimal.TryParse(cell.Value.ToString(), out d);
value = d.ToString();
}
else
value = cell.Value.ToString();
//验证日期是否有效
if (f.TypeCode == TypeCode.DateTime)
{
DateTime dt = DateTime.MinValue;
if (!DateTime.TryParse(value, out dt))
{
invalidDate = true;
cell.ErrorText = strInvDate;
continue;
}
}
if (f != null)
{
filters.Add(new Filter(f, operatorType, value, AndOrEnums.None));
}
}
}
}
if (invalidDate)
{
UI.Common.MsgInfo(strInvDate);
}
else
{
string Url = GetSvaeUrl(XmlKey);
RdlcHelper rdlc = new RdlcHelper();
if (rdlc.SearchXmlFile(filters, Url, ReportMaster.RdlcPath, out ErrMsg))
{
OnSData(Url, filters);
btnCancel.PerformClick();
}
else
{
UI.Common.MsgInfo(ErrMsg);
}
}
}
catch (Exception ex)
{
UI.Common.MsgInfo(ex.Message);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Visible = false;
dgList.DisplayRows = 0;
}
private void dgList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != -1)
dgListDelRow();
}
private void dgList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.RowIndex != -1 && e.ColumnIndex == colFields.Index)
{
DataGridViewRow r = dgList.Rows[e.RowIndex] as DataGridViewRow;
string operatorValue = r.Cells[colOperator.Name].Value != null ? r.Cells[colOperator.Name].Value.ToString() : string.Empty;
DataGridViewComboBoxCell cell = dgList[e.ColumnIndex, e.RowIndex] as DataGridViewComboBoxCell;
if (cell != null && cell.Value != null)
{
string fieldName = cell.EditedFormattedValue.ToString();
if (!string.IsNullOrEmpty(fieldName))
{
Field f = field.FieldList.Find(fieldName);
cell.Tag = f;
CheckTextBoxValue(r, f); //判断选中类型和限制文本框输入的方法
if (string.IsNullOrEmpty(operatorValue))
r.Cells[colOperator.Index].Value = "Equal";
}
}
}
}
catch (Exception ex)
{
UI.Common.MsgInfo(ex.Message);
}
}
#endregion
#region private Function
private string GetSvaeUrl(string xmlKey)
{
string File;
//xml文件名
if (!string.IsNullOrEmpty(xmlKey))
File = "ReportXml_" + xmlKey + ".xml";
else
File = "ReportXml.xml";
//得到本地保存xml的路径
string Folder = UI.Common.GetDataPath();//.GetAppDataPath ( string.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Night Job Report");
if (!Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
return Folder + "\\" + File;
}
/// <summary>
/// 清除dglist中的数据
/// </summary>
private void EmptyRow()
{
foreach (DataGridViewRow r in dgList.Rows)
{
r.Cells[colFields.Name].Value = null;
r.Cells[colOperator.Name].Value = null;
r.Cells[colValue.Name].Value = null;
r.Cells[colAndOr.Name].Value = null;
r.Cells[colFiltersType.Name].Value = null;
}
}
/// <summary>
/// 多行删除
/// </summary>
private void dgListDelRow()
{
try
{
if (dgList != null && dgList.SelectedRows.Count > 0)
{
List<int> index = new List<int>();
for (int i = 0; i < dgList.SelectedRows.Count; i++)
{
index.Add(dgList.SelectedRows[i].Index);
}
index.Sort();
for (int x = index.Count - 1; x >= 0; x--)
{
dgList.Rows.Remove(dgList.Rows[index[x]]);
if (dgList.RowSeqVisible)
dgList.Rows.InsertCopy(dgList.Rows.Count - 1, dgList.Rows.Count - 1);
}
}
}
catch (Exception ex)
{
UI.Common.MsgInfo(ex.Message);
}
}
/// <summary>
/// 类型和限制文本框输入的方法
/// </summary>
/// <param name="filtersValue">选中filters的值</param>
/// <param name="r">dgList 行的对象</param>
private void CheckTextBoxValue(DataGridViewRow r, Field field)
{
//文本框对象
hwj.UserControls.DataList.xDataGridViewTextBoxCell value = r.Cells[colValue.Name] as hwj.UserControls.DataList.xDataGridViewTextBoxCell;
if (value == null)
return;
//类型发生改变清空文本框数据
r.Cells[colValue.Index].Value = string.Empty;
//限制输入
if (field.TypeCode == TypeCode.Decimal || field.TypeCode == TypeCode.Double)
{
value.ContentType = hwj.UserControls.CommonControls.ContentType.Numberic;
}
else if (field.TypeCode == TypeCode.Int16 || field.TypeCode == TypeCode.Int32 || field.TypeCode == TypeCode.Int64)
{
value.Style.Format = "";
value.ContentType = hwj.UserControls.CommonControls.ContentType.Integer;
}
else
{
value.ContentType = hwj.UserControls.CommonControls.ContentType.None;
}
r.Cells[colFiltersType.Index].Value = field.TypeCode;
}
#endregion
}