DataGridView的类第一部分
DataGridView 使用经验
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
using System.Data;
using BaseCommon;
namespace CommonControls
{
public partial class NewDataGridView : DataGridView
{
public delegate bool DateGridViewEvent();
public delegate bool DataGridViewWhenDeletting(string p_strGuid);
/// <summary>
/// 用词按下删除键时,发出的删除提示事件,如果需要则使用
/// </summary>
[Description("行删除前,发出的提示信息事件,如果不使用此事件,则删除时不会发出提示,如果需要提示,则要使用这个事件")]
public event DateGridViewEvent ShowDelMsg;
/// <summary>
/// 保存数据事件
/// </summary>
[Description("所有全部删除后发出的存盘事件")]
public event DateGridViewEvent SaveDataTable;
/// <summary>
/// 每当删除一条数据时发出此事件,外部可以使用此信息进行额外处理
/// </summary>
[Description("行在删除前触发的处理事件")]
public event DataGridViewWhenDeletting WillDeletting;
[Description("新的只触发一次的行选择事件")]
public event EventHandler SelectionChangedNew;
/// <summary>
/// 高亮的行的GUID
/// </summary>
List<string> m_listHiLine = new List<string>();
/// <summary>
/// 高亮行的单元设置
/// </summary>
DataGridViewCellStyle m_styleHiLine = null;
/// <summary>
/// 设置高亮行,p_styleHiLine以最后一次调用为准
/// </summary>
/// <param name="p_strGuid"></param>
/// <param name="p_styleHiLine"></param>
public void SetHiLine(string p_strGuid, DataGridViewCellStyle p_styleHiLine)
{
if (m_listHiLine.Contains(p_strGuid) == false)
{
m_listHiLine.Add(p_strGuid);
m_styleHiLine = p_styleHiLine;
}
}
Color colorLeave, colorEnter;
List<int> m_listHearderClick = new List<int>();
public NewDataGridView()
: base()
{
InitializeComponent();
}
List<string> m_listRemainberSelected = new List<string>();
public DataTable NewDataSource
{
set
{
BindingSource bs;
if (AutoGenerateColumns) //在绑定之前设置一下数据源仅使用绑定列
AutoGenerateColumns = false;
if (DataSource != null && DataSource.GetType().FullName == "System.Windows.Forms.BindingSource")
{
bs = DataSource as BindingSource;
}
else
{
bs = new BindingSource();
DataSource = bs;
}
bs.DataSource = value;
}
get
{
if (DataSource != null)
{
Type t = DataSource.GetType();
if (t.FullName == "System.Windows.Forms.BindingSource")
{
return ((BindingSource)DataSource).DataSource as DataTable;
}
else if (t.FullName == "System.Data.DataTable")
return ((DataTable)DataSource);
}
return null;
}
}
public void NewEndEdit()
{
if (DataSource != null)
{
Type t = DataSource.GetType();
if (t.FullName == "System.Windows.Forms.BindingSource")
{
BindingSource bs = (BindingSource)DataSource;
bs.EndEdit();
}
}
EndEdit();
}
/// <summary>
/// 如果某一行被编辑了,但是由于某种原因,相应的DataTable还未做相应的标记,则这里手工设置一下相应的标记
/// 这个方法可以不用了,使用 NewEndEdit()强制提交所有已更改数据
/// </summary>
/// <param name="p_strGuid"></param>
public bool SetModeify(DataGridViewRow p_dgvRow)
{
DataRow drNow = (p_dgvRow.DataBoundItem as DataRowView).Row;
if (drNow.RowState != DataRowState.Modified)
{
//先复制一下数据,设置完状态后,数据会变为修改前的,所以必须备份数据
object[] objBak = (object[])drNow.ItemArray.Clone();
drNow.SetModified();
drNow.ItemArray = objBak;
}
return true;
}
/// <summary>
/// 保存当前DataGridView的选择行到公共池中
/// </summary>
/// <param name="p_dgv"></param>
/// <returns></returns>
public void SaveGridVeiwSelectedRow()
{
Dictionary<int, string> dictTmp = new Dictionary<int, string>();
for (int i = 0; i < SelectedRows.Count; i++)
{
string dataRowGuid = SelectedRows[i].Cells[0].Value.ToString();
dictTmp.Add(SelectedRows[i].Index, dataRowGuid);
}
m_listRemainberSelected = dictTmp.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value).Values.ToList<string>(); // (o => o.value);
}
public void RestoreGridViewSelectedRow()
{
//让所有之前选择的行恢复选择状态
for (int i = 0; i < Rows.Count; i++)
{
if (Rows[i].Cells[0].Value == null)
continue;
string dataRowGuid = Rows[i].Cells[0].Value.ToString();
if (m_listRemainberSelected.Contains(dataRowGuid))
Rows[i].Selected = true;
else
Rows[i].Selected = false;
}
m_listRemainberSelected.Clear();
}
public string m_strEditValue = string.Empty;
bool m_bSetReadOnly = false;
public bool SetReadOnlyAfterEdit
{
set { m_bSetReadOnly = value; }
}
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
m_strEditValue = Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? string.Empty : Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); //保存之前的值
base.OnCellBeginEdit(e);
for (int i = 0; i < Rows.Count; i++)
{
if (i == e.RowIndex)
Rows[i].Selected = true;
else
Rows[i].Selected = false;
}
}
protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
{
if (m_bSetReadOnly)
ReadOnly = true;
if (Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null || m_strEditValue == Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
return; //如果相等,则不调用结束处理
base.OnCellEndEdit(e);
if (Rows[e.RowIndex].Cells[0] == null || Rows[e.RowIndex].Cells[0].Value == null || Rows[e.RowIndex].Cells[0].Value.ToString().Length == 0)
{
Rows[e.RowIndex].Cells[0].Value = System.Guid.NewGuid(); //.ToString();
}
FouceGrid();
}
bool m_bStopSort = true;
public void StopSort(bool p_bStopSort)
{
m_bStopSort = p_bStopSort;
}