// ***********************************************************************
// Assembly : JinYuan.ControlLib
// Created : 08-09-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JinYuan.ControlLib.UCHelper;
namespace JinYuan.ControlLib
{
///
/// Class UCDataGridViewRow.
/// Implements the
/// Implements the
///
///
///
[ToolboxItem(false)]
public partial class UCDataGridViewRow : UserControl, IDataGridViewRow
{
#region 属性
///
/// CheckBox选中事件
///
public event DataGridViewEventHandler CheckBoxChangeEvent;
///
/// Occurs when [row custom event].
///
public event DataGridViewRowCustomEventHandler RowCustomEvent;
///
/// 点击单元格事件
///
public event DataGridViewEventHandler CellClick;
///
/// 数据源改变事件
///
public event DataGridViewEventHandler SourceChanged;
///
/// 列参数,用于创建列数和宽度
///
/// The columns.
public List Columns
{
get;
set;
}
///
/// 数据源
///
/// The data source.
public object DataSource
{
get;
set;
}
///
/// 行号
///
/// The Index of the row.
public int RowIndex { get; set; }
///
/// Gets or sets a value indicating whether this instance is show CheckBox.
///
/// true if this instance is show CheckBox; otherwise, false.
public bool IsShowCheckBox
{
get;
set;
}
///
/// The m is checked
///
private bool m_isChecked;
///
/// 是否选中
///
/// true if this instance is checked; otherwise, false.
public bool IsChecked
{
get
{
return m_isChecked;
}
set
{
if (m_isChecked != value)
{
m_isChecked = value;
(this.panCells.Controls.Find("check", false)[0] as UCCheckBox).Checked = value;
}
}
}
///
/// The m row height
///
int m_rowHeight = 40;
///
/// 行高
///
/// The height of the row.
public int RowHeight
{
get
{
return m_rowHeight;
}
set
{
m_rowHeight = value;
this.Height = value;
}
}
#endregion
///
/// Initializes a new instance of the class.
///
public UCDataGridViewRow()
{
InitializeComponent();
}
///
/// 绑定数据到Cell
///
/// 返回true则表示已处理过,否则将进行默认绑定(通常只针对有Text值的控件)
public void BindingCellData()
{
if (DataSource != null)
{
for (int i = 0; i < Columns.Count; i++)
{
DataGridViewColumnEntity com = Columns[i];
var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
if (cs != null && cs.Length > 0)
{
if (DataSource is DataRow)
{
DataRow row=DataSource as DataRow;
if (com.Format != null)
{
cs[0].Text = com.Format(row[com.DataField]);
}
else
{
cs[0].Text = row[com.DataField].ToStringExt();
}
}
else
{
var pro = DataSource.GetType().GetProperty(com.DataField);
if (pro != null)
{
var value = pro.GetValue(DataSource, null);
if (com.Format != null)
{
cs[0].Text = com.Format(value);
}
else
{
cs[0].Text = value.ToStringExt();
}
}
}
}
}
}
foreach (Control item in this.panCells.Controls)
{
if (item is IDataGridViewCustomCell)
{
IDataGridViewCustomCell cell = item as IDataGridViewCustomCell;
cell.RowCustomEvent += cell_RowCustomEvent;
cell.SetBindSource(DataSource);
}
}
}
void cell_RowCustomEvent(object sender, DataGridViewRowCustomEventArgs e)
{
if (RowCustomEvent != null)
{
RowCustomEvent(sender, e);
}
}
///
/// Handles the MouseDown event of the Item control.
///
/// The source of the event.
/// The instance containing the event data.
void Item_MouseDown(object sender, MouseEventArgs e)
{
if (CellClick != null)
{
CellClick(this, new DataGridViewEventArgs()
{
CellControl = this,
CellIndex = (sender as Control).Tag.ToInt(),
RowIndex = this.RowIndex
});
}
}
///
/// 设置选中状态,通常为设置颜色即可
///
/// 是否选中
public void SetSelect(bool blnSelected)
{
if (blnSelected)
{
this.BackColor = Color.FromArgb(255, 247, 245);
}
else
{
this.BackColor = Color.Transparent;
}
}
///
/// Reloads the cells.
///
public void ReloadCells()
{
try
{
ControlHelper.FreezeControl(this, true);
this.panCells.Controls.Clear();
this.panCells.ColumnStyles.Clear();
int intColumnsCount = Columns.Count();
if (Columns != null && intColumnsCount > 0)
{
if (IsShowCheckBox)
{
intColumnsCount++;
}
this.panCells.ColumnCount = intColumnsCount;
for (int i = 0; i < intColumnsCount; i++)
{
Control c = null;
if (i == 0 && IsShowCheckBox)
{
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
UCCheckBox box = new UCCheckBox();
box.Name = "check";
box.TextValue = "";
box.Size = new Size(30, 30);
box.Dock = DockStyle.Fill;
box.CheckedChangeEvent += (a, b) =>
{
IsChecked = box.Checked;
if (CheckBoxChangeEvent != null)
{
CheckBoxChangeEvent(a, new DataGridViewEventArgs()
{
RowIndex = this.RowIndex,
CellControl = box,
CellIndex = 0
});
}
};
c = box;
}
else
{
var item = Columns[i - (IsShowCheckBox ? 1 : 0)];
this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
if (item.CustomCellType == null)
{
Label lbl = new Label();
lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
lbl.Name = "lbl_" + item.DataField;
lbl.Font = new Font("微软雅黑", 12);
lbl.ForeColor = Color.Black;
lbl.AutoSize = false;
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = item.TextAlign;
lbl.MouseDown += (a, b) =>
{
Item_MouseDown(a, b);
};
c = lbl;
}
else
{
Control cc = (Control)Activator.CreateInstance(item.CustomCellType);
cc.Dock = DockStyle.Fill;
c = cc;
}
}
this.panCells.Controls.Add(c, i, 0);
}
}
}
finally
{
ControlHelper.FreezeControl(this, false);
}
}
}
}