예제 #1
0
        /// <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)
            {
            }
        }
예제 #2
0
        /// <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)
            {
            }
        }