using System; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.Xml; using Nevron.UI.WinForm.Controls; namespace UC { public partial class UCBlinkDataGridView : NDataGridView { #region Vars private bool _blink = true; private Color _blink_back_color = Color.Red; private Color _blink_fore_color = Color.White; private int _blink_times = 1; private int _blink_interval = 200; private int _visible_rows = 0; private int _visible_column = 0; private NVScrollBar _vscrool_bar = new NVScrollBar(); private NHScrollBar _hscroll_bar = new NHScrollBar(); private DataTable _data_source = null; private bool _rows_changed = false; private bool _column_changed = false; #endregion #region Constructor public UCBlinkDataGridView() { InitializeComponent(); this.DoubleBuffered = true; this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.UserPaint, true); this.UpdateStyles(); this.AllowUserToResizeColumns = true; this.AutoGenerateColumns = false; this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; this.ScrollBars = ScrollBars.None; _vscrool_bar.Palette.Scheme = ColorScheme.GrayScale; _vscrool_bar.Dock = DockStyle.Right; _vscrool_bar.Minimum = 0; _vscrool_bar.Maximum = 100; _vscrool_bar.LargeChange = 1; _vscrool_bar.SmallChange = 1; _vscrool_bar.Value = 0; _vscrool_bar.Visible = false; _vscrool_bar.ValueChanged += OnVScrollBarValueChanged; this.Resize += new EventHandler(UCBlinkDataGridView_Resize); this.Controls.Add(_vscrool_bar); _vscrool_bar.BringToFront(); _hscroll_bar.Palette.Scheme = ColorScheme.GrayScale; _hscroll_bar.Dock = DockStyle.Bottom; _hscroll_bar.Minimum = 0; _hscroll_bar.Maximum = 100; _hscroll_bar.LargeChange = 1; _hscroll_bar.SmallChange = 1; _hscroll_bar.Value = 0; _hscroll_bar.Visible = false; _hscroll_bar.ValueChanged += OnHScrollBarValueChanged; this.Controls.Add(_hscroll_bar); _hscroll_bar.BringToFront(); } #endregion #region Handling Events #region OnDataSourceChanged() protected override void OnDataSourceChanged(EventArgs e) { base.OnDataSourceChanged(e); if (this.DataSource != null) { try { _data_source = (DataTable)this.DataSource; _data_source.DefaultView.ListChanged += new ListChangedEventHandler(OnListChanged); _rows_changed = true; _column_changed = true; SetVScrollbarVisible(); SetHScrollbarVisible(); } catch { } } } #endregion #region OnListChanged() private void OnListChanged(object sender, ListChangedEventArgs e) { _rows_changed = true; SetVScrollbarVisible(); _column_changed = true; SetHScrollbarVisible(); } #endregion #region OnMouseWheel() protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); if ((e.Delta != 0) && (_vscrool_bar.Visible)) { int new_value = _vscrool_bar.Value - (e.Delta / 120); if (new_value < 0) new_value = 0; if (new_value > _vscrool_bar.Maximum) new_value = _vscrool_bar.Maximum; _vscrool_bar.Value = new_value; } } #endregion #region OnColumnAdded() protected override void OnColumnAdded(DataGridViewColumnEventArgs e) { this.Columns[e.Column.Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; base.OnColumnAdded(e); if (_data_source == null) { _column_changed = true; SetHScrollbarVisible(); } } #endregion #region OnRowsAdded() protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e) { int columns_count = this.Columns.Count; for (int i = 0; i < columns_count; i++) { DataGridViewColumn column = this.Columns[i]; if (column.AutoSizeMode == DataGridViewAutoSizeColumnMode.ColumnHeader) column.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet; if (this[i, e.RowIndex].GetType() == typeof(UCBlinkTextboxCell)) { UCBlinkTextboxCell cell = (UCBlinkTextboxCell)this[i, e.RowIndex]; cell.Blink = _blink; cell.BlinkBackColor = _blink_back_color; cell.BlinkForeColor = _blink_fore_color; cell.BlinkInterval = _blink_interval; cell.BlinkTimes = _blink_times; } } base.OnRowsAdded(e); if (_data_source == null) { _rows_changed = true; SetVScrollbarVisible(); _column_changed = true; SetHScrollbarVisible(); } if (this.AutoSizeColumnsMode == DataGridViewAutoSizeColumnsMode.None) { this.AutoResizeColumns(); _hscroll_bar.Refresh(); this.Invalidate(); } } #endregion #region OnRowsRemoved() protected override void OnRowsRemoved(DataGridViewRowsRemovedEventArgs e) { base.OnRowsRemoved(e); if (_data_source == null) { _rows_changed = true; SetVScrollbarVisible(); _column_changed = true; SetHScrollbarVisible(); } } #endregion #region OnColumnRemoved() protected void OnColumnRemoved(DataGridViewColumnEventArgs e) { base.OnColumnRemoved(e); if (_data_source == null) { _column_changed = true; SetHScrollbarVisible(); } } #endregion #region OnSizeChanged() protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); SetVScrollbarVisible(); SetHScrollbarVisible(); } #endregion #region OnVisibleChanged() protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if ((this.Visible) && (this.AutoSizeColumnsMode == DataGridViewAutoSizeColumnsMode.None)) { this.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); this.Invalidate(); } } #endregion #region OnVScrollBarValueChanged() private void OnVScrollBarValueChanged(object sender, ScrollBarEventArgs e) { if ((_vscrool_bar.Visible) && (_vscrool_bar.Value >= 0) && (_vscrool_bar.Value < this.RowCount)) { this.FirstDisplayedScrollingRowIndex = _vscrool_bar.Value; //Rectangle rect = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Bottom + _hscroll_bar.Height); this.NotifyInvalidate(this.ClientRectangle); } } #endregion #region OnHScrollBarValueChanged private void OnHScrollBarValueChanged(object sender, ScrollBarEventArgs e) { if ((_hscroll_bar.Visible) && (_hscroll_bar.Value >= 2) && (_hscroll_bar.Value < this.ColumnCount)) { if (this.Columns[_hscroll_bar.Value].Visible) { this.FirstDisplayedScrollingColumnIndex = _hscroll_bar.Value; //Rectangle rect = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height + _hscroll_bar.Height); this.NotifyInvalidate(this.ClientRectangle); } } } #endregion #region UCBlinkDataGridView_Resize() void UCBlinkDataGridView_Resize(object sender, EventArgs e) { _vscrool_bar.Refresh(); _hscroll_bar.Refresh(); this.Refresh(); } #endregion #endregion #region Properties #region Blink public bool Blink { get { return _blink; } set { _blink = value; } } #endregion #region DefaultBlinkBackColor public Color DefaultBlinkBackColor { get { return _blink_back_color; } set { _blink_back_color = value; } } #endregion #region DefaultBlinkForeColor public Color DefaultBlinkForeColor { get { return _blink_fore_color; } set { _blink_fore_color = value; } } #endregion #region DefaultBlinkInterval public int DefaultBlinkInterval { get { return _blink_interval; } set { _blink_interval = value; } } #endregion #region DefaultBlinkTimes public int DefaultBlinkTimes { get { return _blink_times; } set { _blink_times = value; } } #endregion #endregion #region Methods #region GetXMLString() public string GetXMLString() { string xml_text = ""; xml_text += String.Format("<{0}>{1}", "blink", (this.Blink) ? "1" : "0"); xml_text += String.Format("<{0}>{1}", "blink_backcolor", this.DefaultBlinkBackColor.ToArgb()); xml_text += String.Format("<{0}>{1}", "blink_forecolor", this.DefaultBlinkForeColor.ToArgb()); xml_text += String.Format("<{0}>{1}", "blink_interval", this.DefaultBlinkInterval); xml_text += String.Format("<{0}>{1}", "blink_times", this.DefaultBlinkTimes); foreach (DataGridViewColumn column in this.Columns) { xml_text += ""; xml_text += String.Format("<{0}>{1}", "name", column.Name); xml_text += String.Format("<{0}>{1}", "position", column.DisplayIndex); xml_text += String.Format("<{0}>{1}", "visible", (column.Visible)? "1": "0"); xml_text += String.Format("<{0}>{1}", "width", column.Width); xml_text += ""; } xml_text += ""; return (xml_text); } #endregion #region RestoreFromXMLString() public void RestoreFromXMLString(string xml_node) { if (xml_node.Trim().Length == 0) return; this.SuspendLayout(); try { XmlDocument xml_doc = new XmlDocument(); xml_doc.LoadXml(xml_node); XmlNodeList datagrid_nodes = xml_doc.GetElementsByTagName("datagrid"); XmlNode datagrid_node = datagrid_nodes[0]; for (int i = 0; i < datagrid_node.ChildNodes.Count; i++) { int argb_color = Color.White.ToArgb(); switch (datagrid_node.ChildNodes[i].Name) { case "blink": this.Blink = (datagrid_node.ChildNodes[i].InnerXml == "1"); break; case "blink_backcolor": argb_color = Color.White.ToArgb(); int.TryParse(datagrid_node.ChildNodes[i].InnerXml, out argb_color); this.DefaultBlinkBackColor = Color.FromArgb(argb_color); break; case "blink_forecolor": argb_color = Color.Black.ToArgb(); int.TryParse(datagrid_node.ChildNodes[i].InnerXml, out argb_color); this.DefaultBlinkForeColor = Color.FromArgb(argb_color); break; case "blink_interval": int interval = 300; if (int.TryParse(datagrid_node.ChildNodes[i].InnerXml, out interval)) this.DefaultBlinkInterval = interval; break; case "blink_times": int blink_times = 1; if (int.TryParse(datagrid_node.ChildNodes[i].InnerXml, out blink_times)) this.DefaultBlinkTimes = blink_times; break; } } XmlNodeList columns_nodes = xml_doc.GetElementsByTagName("column"); foreach (XmlNode column_node in columns_nodes) { DataGridViewColumn column = null; for (int i = 0; i < column_node.ChildNodes.Count; i++) { switch (column_node.ChildNodes[i].Name) { case "name": string column_name = column_node.ChildNodes[i].InnerXml; if (this.Columns.Contains(column_name)) column = this.Columns[column_name]; break; case "position": if (column != null) { int display_index = column.DisplayIndex; if (int.TryParse(column_node.ChildNodes[i].InnerXml, out display_index)) column.DisplayIndex = display_index; } break; case "visible": if (column != null) column.Visible = (column_node.ChildNodes[i].InnerXml == "1"); break; case "width": if (column != null) { int width = column.Width; if (int.TryParse(column_node.ChildNodes[i].InnerXml, out width)) column.Width = width; } break; } } } } catch (Exception ex) { UCException.ProcessError(ex); } this.ResumeLayout(); } #endregion #region SetVisibleRows() private void SetVisibleRows() { int height = this.Height; //if ((this.HorizontalScrollBar != null) && (this.HorizontalScrollBar.Visible)) // height -= (this.HorizontalScrollBar.Height); //if ((_hscroll_bar != null) && (_hscroll_bar.Visible)) // height -= _hscroll_bar.Height; if (this.RowTemplate == null) { _visible_rows = (height / this.RowTemplate.Height); } else { if (this.RowCount > 0) _visible_rows = (height / this.Rows[0].Height); } if (_data_source != null) { if (_visible_rows > _data_source.DefaultView.Count) _visible_rows = _data_source.DefaultView.Count; } //if ((this.HorizontalScrollBar == null) || (!this.HorizontalScrollBar.Visible)) // _visible_rows += 1; //if ((_hscroll_bar == null) || (!_hscroll_bar.Visible)) // _visible_column += _visible_column; } #endregion #region SetVisibleColumn() private void SetVisibleColumn() { int height = this.Height; int width = this.Width; if ((_hscroll_bar != null) && (_hscroll_bar.Visible)) width -= _hscroll_bar.Width; //if (this.RowTemplate == null) //{ // _visible_rows = (width / this.RowTemplate.Height); //} //else if (this.RowTemplate != null) { if (this.ColumnCount > 0) _visible_column = (width / this.Columns[0].Width); } if (_data_source != null) { if (_visible_column > _data_source.DefaultView.Count) _visible_column = _data_source.DefaultView.Count ; } if ((_hscroll_bar != null) || (!_hscroll_bar.Visible)) _visible_column += 1; } #endregion #region SetVScrollbarVisible() private void SetVScrollbarVisible() { try { SetVisibleRows(); int rows_count = this.RowCount; if ((this.RowCount == 0) || (rows_count < _visible_rows)) { if (_vscrool_bar.Visible) { _vscrool_bar.LargeChange = 1; _vscrool_bar.Visible = false; this.Invalidate(true); } } else { _vscrool_bar.Maximum = this.RowCount - 1; if (!_vscrool_bar.Visible || _rows_changed) { int large_change = (_vscrool_bar.Maximum / 10); if (large_change < 5) large_change = 5; if (large_change >= _visible_column ) large_change = _visible_rows / 3; if (!_vscrool_bar.Visible) { _vscrool_bar.LargeChange = large_change - 1; _vscrool_bar.Visible = true; this.Invalidate(true); } _rows_changed = false; } _vscrool_bar.Maximum = this.RowCount - _visible_rows + _vscrool_bar.LargeChange + 1 ; if (_vscrool_bar.Value > _vscrool_bar.Maximum) _vscrool_bar.Value = _vscrool_bar.Maximum; } } catch (Exception ex) { UCException.ProcessError(ex); } } #endregion #region SetHScrollbarVisible() private void SetHScrollbarVisible() { try { SetVisibleColumn(); int column_count = this.ColumnCount ; if ((this.ColumnCount == 0) || (column_count < this._visible_column)) { if (_hscroll_bar.Visible) { _hscroll_bar.LargeChange = 1; _hscroll_bar.Visible = false; this.Invalidate(true); } } else { _hscroll_bar.Maximum = this.ColumnCount - 1; if (!_hscroll_bar.Visible || _column_changed) { int large_change = (_hscroll_bar.Maximum / 10); if (large_change < 5) large_change = 5; if (large_change >= _visible_column) large_change = _visible_column / 3; if (!_hscroll_bar.Visible) { _hscroll_bar.LargeChange = large_change - 1; _hscroll_bar.Visible = true; this.Invalidate(true); } _column_changed = false; } _hscroll_bar.Maximum = this.ColumnCount - _visible_column + _hscroll_bar.LargeChange + 1; if (_hscroll_bar.Value > _hscroll_bar.Maximum) _hscroll_bar.Value = _hscroll_bar.Maximum; } } catch (Exception ex) { UCException.ProcessError(ex); } } #endregion #endregion } }