/// <summary> /// 刷新数据 /// </summary> public void ReloadSource() { if (DesignMode) { return; } try { ControlHelper.FreezeControl(this.panRow, true); //this.panRow.Controls.Clear(); Rows = new List <IDataGridViewRow>(); if (m_columns == null || m_columns.Count <= 0) { return; } if (m_dataSource != null) { int intIndex = 0; Control lastItem = null; int intSourceCount = 0; if (m_dataSource is DataTable) { intSourceCount = (m_dataSource as DataTable).Rows.Count; } else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType())) { intSourceCount = (m_dataSource as IList).Count; } foreach (Control item in this.panRow.Controls) { if (intIndex >= intSourceCount) { item.Visible = false; } else { var row = (item as IDataGridViewRow); row.IsShowCheckBox = m_isShowCheckBox; if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).Rows[intIndex]; } else { row.DataSource = (m_dataSource as IList)[intIndex]; } row.BindingCellData(); if (row.RowHeight != m_rowHeight) { row.RowHeight = m_rowHeight; } item.Visible = true; item.BringToFront(); if (lastItem == null) { lastItem = item; } Rows.Add(row); } intIndex++; } if (intIndex < intSourceCount) { for (int i = intIndex; i < intSourceCount; i++) { IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType); if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).Rows[i]; } else { row.DataSource = (m_dataSource as IList)[i]; } row.Columns = m_columns; List <Control> lstCells = new List <Control>(); row.IsShowCheckBox = m_isShowCheckBox; row.ReloadCells(); row.BindingCellData(); Control rowControl = (row as Control); this.panRow.Controls.Add(rowControl); row.RowHeight = m_rowHeight; rowControl.Dock = DockStyle.Top; row.CellClick += (a, b) => { SetSelectRow(rowControl, b); }; row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); }; row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } }; row.SourceChanged += RowSourceChanged; rowControl.BringToFront(); Rows.Add(row); if (lastItem == null) { lastItem = rowControl; } } } if (lastItem != null && intSourceCount == m_showCount) { lastItem.Height = this.panRow.Height - (m_showCount - 1) * m_rowHeight - 2; } } else { foreach (Control item in this.panRow.Controls) { item.Visible = false; } } } finally { ControlHelper.FreezeControl(this.panRow, false); } }
/// <summary> /// 移除行 /// </summary> /// <param name="rowIndex"></param> public void EditRow(int rowIndex) { if (DesignMode) { return; } try { this.panRow.Controls.Clear(); var NewRows = new List <IDataGridViewRow>(); if (m_columns == null || m_columns.Count <= 0) { return; } if (this.Rows != null) { int index = 0; Control lastItem = null; int intSourceCount = 0; intSourceCount = this.Rows.Count; for (int i = 0; i < intSourceCount; i++) { IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType); row.DataSource = this.Rows[i].DataSource; row.Columns = m_columns; row.RowIndex = index; List <Control> lstCells = new List <Control>(); row.IsShowCheckBox = m_isShowCheckBox; if (i == rowIndex) { row.AddCells(); row.BindingAddCellData(); } else { row.ReloadCells(); row.BindingCellData(); } Control rowControl = (row as Control); this.panRow.Controls.Add(rowControl); row.RowHeight = m_rowHeight; rowControl.Dock = DockStyle.Top; row.CellClick += (a, b) => { SetSelectRow(rowControl, b); }; row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); }; row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } }; row.SourceChanged += RowSourceChanged; rowControl.BringToFront(); NewRows.Add(row); ++index; if (lastItem == null) { lastItem = rowControl; } } if (lastItem != null && intSourceCount == m_showCount) { lastItem.Height = this.panRow.Height - (m_showCount - 1) * m_rowHeight; } } else { foreach (Control item in this.panRow.Controls) { item.Visible = false; } } Rows.Clear(); Rows.AddRange(NewRows); } catch (Exception ex) { } }
/// <summary> /// 刷新数据 /// </summary> public void ReloadSource() { if (DesignMode) { return; } try { ControlHelper.FreezeControl(this, true); panHead.Location = new Point(0, 0); Rows = new List <IDataGridViewRow>(); if (m_columns == null || m_columns.Count <= 0) { return; } if (m_dataSource != null) { int intIndex = 0; int intSourceCount = 0; if (m_dataSource is DataTable) { intSourceCount = (m_dataSource as DataTable).Rows.Count; } else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType())) { intSourceCount = (m_dataSource as IList).Count; } foreach (Control item in this.panRow.Controls) { if (intIndex >= intSourceCount) { item.Visible = false; } else { var row = (item as IDataGridViewRow); row.IsShowCheckBox = m_isShowCheckBox; if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).Rows[intIndex]; } else { row.DataSource = (m_dataSource as IList)[intIndex]; } row.BindingCellData(); if (row.RowHeight != m_rowHeight) { row.RowHeight = m_rowHeight; } item.Visible = true; item.Width = panHead.Width; Rows.Add(row); row.RowIndex = Rows.IndexOf(row); } intIndex++; } if (intIndex < intSourceCount) { for (int i = intIndex; i < intSourceCount; i++) { IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType); if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).Rows[i]; } else { row.DataSource = (m_dataSource as IList)[i]; } row.Columns = m_columns; List <Control> lstCells = new List <Control>(); row.IsShowCheckBox = m_isShowCheckBox; row.ReloadCells(); row.BindingCellData(); Control rowControl = (row as Control); rowControl.Width = panHead.Width; row.RowHeight = m_rowHeight; row.CellClick += (a, b) => { this.FindForm().ActiveControl = this; rowControl.Focus(); SetSelectRow(rowControl, b); }; row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); }; row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } }; row.SourceChanged += RowSourceChanged; Rows.Add(row); row.RowIndex = Rows.IndexOf(row); this.panRow.Controls.Add(rowControl); } } //this.panRow.Height = intSourceCount * RowHeight; } else { foreach (Control item in this.panRow.Controls) { item.Visible = false; } } } finally { ControlHelper.FreezeControl(this, false); } }
/// <summary> /// 刷新数据 /// </summary> public void AddRow(object rowData) { if (DesignMode) { return; } try { this.panRow.Controls.Clear(); Rows = new List <IDataGridViewRow>(); if (m_columns == null || m_columns.Count <= 0) { return; } if (m_dataSource != null) { int index = 0; #region 新增的行 if (m_dataSource != null) { IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType); if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).NewRow(); } else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType())) { var type = (m_dataSource as IList)[0].GetType(); var model = Activator.CreateInstance(type); var type1 = rowData.GetType(); var props = type.GetProperties(); var rowDataProps = type1.GetProperties(); foreach (var prop in props) { if (prop.CanWrite) { foreach (var rowDataProp in rowDataProps) { try { if (prop.Name == rowDataProp.Name) { var data = rowDataProp.GetValue(rowData, null); if (data != DBNull.Value) { prop.SetValue(model, Convert.ChangeType(data, prop.PropertyType), null); } } } catch (IndexOutOfRangeException) { continue; } } } } row.DataSource = model; } row.RowIndex = index; row.Columns = m_columns; row.IsShowCheckBox = m_isShowCheckBox; row.AddCells(); row.BindingAddCellData(); Control addRowControl = (row as Control); this.panRow.Controls.Add(addRowControl); row.RowHeight = m_rowHeight; addRowControl.Dock = DockStyle.Top; row.CellClick += (a, b) => { SetSelectRow(addRowControl, b); }; row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(addRowControl, b); }; row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } }; row.SourceChanged += RowSourceChanged; addRowControl.BringToFront(); Rows.Add(row); ++index; } #endregion #region 旧的数据源 Control lastItem = null; int intSourceCount = 0; if (m_dataSource is DataTable) { intSourceCount = (m_dataSource as DataTable).Rows.Count; } else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType())) { intSourceCount = (m_dataSource as IList).Count; } for (int i = 0; i < intSourceCount; i++) { IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType); if (m_dataSource is DataTable) { row.DataSource = (m_dataSource as DataTable).Rows[i]; } else { row.DataSource = (m_dataSource as IList)[i]; } row.Columns = m_columns; row.RowIndex = index; row.IsShowCheckBox = m_isShowCheckBox; row.ReloadCells(); row.BindingCellData(); Control rowControl = (row as Control); this.panRow.Controls.Add(rowControl); row.RowHeight = m_rowHeight; rowControl.Dock = DockStyle.Top; row.CellClick += (a, b) => { SetSelectRow(rowControl, b); }; row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); }; row.RowCustomEvent += (a, b) => { if (RowCustomEvent != null) { RowCustomEvent(a, b); } }; row.SourceChanged += RowSourceChanged; rowControl.BringToFront(); Rows.Add(row); ++index; if (lastItem == null) { lastItem = rowControl; } } if (lastItem != null && intSourceCount == m_showCount) { lastItem.Height = this.panRow.Height - (m_showCount - 1) * m_rowHeight; } #endregion } else { foreach (Control item in this.panRow.Controls) { item.Visible = false; } } } catch (Exception ex) { } }