You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
342 lines
12 KiB
C#
342 lines
12 KiB
C#
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 System.Collections;
|
|
using System.Reflection;
|
|
|
|
namespace Mesnac.Controls.Default
|
|
{
|
|
public partial class MCGridView : UserControl
|
|
{
|
|
#region 字段定义
|
|
|
|
private HScrollBar hScrollBar;//给拍闹一个水平的滚动条
|
|
private Hashtable BoxHash;//存放动态生成编辑框
|
|
int columnsWidth = 0;
|
|
Hashtable columnswidthvalue = new Hashtable();
|
|
|
|
#endregion
|
|
|
|
#region 构造方法
|
|
|
|
public MCGridView()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 属性定义
|
|
|
|
private BindingSource _bindingSource = new BindingSource();
|
|
[DefaultValue(null), Description("数据源")]
|
|
public BindingSource BindingSource
|
|
{
|
|
get { return _bindingSource; }
|
|
set { _bindingSource = value; }
|
|
}
|
|
|
|
private bool _isEdit = false;
|
|
[DefaultValue(false), Description("是否可编辑")]
|
|
public bool IsEdit
|
|
{
|
|
get { return _isEdit; }
|
|
set { _isEdit = value; }
|
|
}
|
|
|
|
private bool _isPanelEdit = true;
|
|
[DefaultValue(false), Description("是否在Panel里进行编辑")]
|
|
public bool IsPanelEdit
|
|
{
|
|
get { return _isPanelEdit; }
|
|
set { _isPanelEdit = value; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 方法定义
|
|
|
|
/// <summary>
|
|
/// 计算列的总长度
|
|
/// </summary>
|
|
private void calculateColumnsWidth()
|
|
{
|
|
columnswidthvalue.Clear();
|
|
columnsWidth = 0;
|
|
for (int iCnt = 0; iCnt < dataGridView1.Columns.Count; iCnt++)
|
|
{
|
|
if (dataGridView1.Columns[iCnt].Visible)
|
|
{
|
|
if (dataGridView1.Columns[iCnt].AutoSizeMode == DataGridViewAutoSizeColumnMode.Fill)
|
|
{
|
|
columnsWidth += dataGridView1.Columns[iCnt].MinimumWidth;
|
|
}
|
|
else
|
|
columnsWidth += dataGridView1.Columns[iCnt].Width;
|
|
columnswidthvalue.Add(iCnt, dataGridView1.Columns[iCnt].Width);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据gridviewcol生成编辑box
|
|
/// </summary>
|
|
public void CreateEditBoxs(int rowindex)
|
|
{
|
|
TextBox tb;
|
|
foreach (Control control in BoxHash.Values)
|
|
{
|
|
this.panel1.Controls.Remove(control);
|
|
}
|
|
BoxHash.Clear();
|
|
for (int i = 0; i < dataGridView1.Columns.Count; i++)
|
|
{
|
|
tb = new TextBox();
|
|
BoxHash.Add(dataGridView1.Columns[i], tb);
|
|
this.panel1.Controls.Add(tb);
|
|
tb.Size = new Size(dataGridView1.Columns[i].Width, dataGridView1.RowTemplate.Height);
|
|
tb.Location = new Point(Convert.ToInt32(columnswidthvalue[i]) * i + 42, 1);
|
|
tb.BackColor = dataGridView1.DefaultCellStyle.BackColor;
|
|
tb.Text = dataGridView1.Rows[rowindex].Cells[i].Value.ToString();
|
|
}
|
|
//MessageBox.Show(this.panel1.Controls.Count.ToString());
|
|
this.panel1.Refresh();
|
|
}
|
|
|
|
#region 格式转化
|
|
|
|
/// <summary>
|
|
/// 将集合类转换成DataTable
|
|
/// </summary>
|
|
/// <param name="list">集合</param>
|
|
/// <returns></returns>
|
|
public DataTable ToDataTable(IList list)
|
|
{
|
|
DataTable result = new DataTable();
|
|
if (list.Count > 0)
|
|
{
|
|
PropertyInfo[] propertys = list[0].GetType().GetProperties();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
result.Columns.Add(pi.Name, pi.PropertyType);
|
|
}
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
ArrayList tempList = new ArrayList();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
object obj = pi.GetValue(list[i], null);
|
|
tempList.Add(obj);
|
|
}
|
|
object[] array = tempList.ToArray();
|
|
result.LoadDataRow(array, true);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将泛型集合类转换成DataTable
|
|
/// </summary>
|
|
/// <typeparam name="T">集合项类型</typeparam>
|
|
/// <param name="list">集合</param>
|
|
/// <returns>数据集(表)</returns>
|
|
//public bool DataFormat<T>(IList<T> list)
|
|
//{
|
|
// return ToDataTable<T>(list);
|
|
//}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 将泛型集合类转换成DataTable
|
|
/// </summary>
|
|
/// <typeparam name="T">集合项类型</typeparam>
|
|
/// <param name="list">集合</param>
|
|
/// <param name="propertyName">需要返回的列的列名</param>
|
|
/// <returns>数据集(表)</returns>
|
|
public DataTable ToDataTableByPara<T>(IList<T> list, params string[] propertyName)
|
|
{
|
|
List<string> propertyNameList = new List<string>();
|
|
if (propertyName != null)
|
|
propertyNameList.AddRange(propertyName);
|
|
|
|
DataTable result = new DataTable();
|
|
if (list.Count > 0)
|
|
{
|
|
PropertyInfo[] propertys = list[0].GetType().GetProperties();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
if (propertyNameList.Count == 0)
|
|
{
|
|
result.Columns.Add(pi.Name, pi.PropertyType);
|
|
}
|
|
else
|
|
{
|
|
if (propertyNameList.Contains(pi.Name))
|
|
result.Columns.Add(pi.Name, pi.PropertyType);
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
ArrayList tempList = new ArrayList();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
if (propertyNameList.Count == 0)
|
|
{
|
|
object obj = pi.GetValue(list[i], null);
|
|
tempList.Add(obj);
|
|
}
|
|
else
|
|
{
|
|
if (propertyNameList.Contains(pi.Name))
|
|
{
|
|
object obj = pi.GetValue(list[i], null);
|
|
tempList.Add(obj);
|
|
}
|
|
}
|
|
}
|
|
object[] array = tempList.ToArray();
|
|
result.LoadDataRow(array, true);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region 事件处理
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
this.dataGridView1.RowTemplate.Height = 20;
|
|
this.dataGridView1.Controls.Add(this.comboBox1);
|
|
this.bindingNavigator1.BindingSource = _bindingSource;
|
|
this.dataGridView1.DataSource = _bindingSource;
|
|
if (this._isEdit)
|
|
{
|
|
hScrollBar = new HScrollBar();
|
|
BoxHash = new Hashtable();
|
|
this.dataGridView1.CellDoubleClick += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
|
|
this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
|
|
this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
|
|
}
|
|
}
|
|
|
|
void dataGridView1_Scroll(object sender, ScrollEventArgs e)
|
|
{
|
|
if (columnswidthvalue.Count == 0)
|
|
return;
|
|
//int scrollvalue = 0;
|
|
//for (int i = 0; i < dataGridView1.FirstDisplayedScrollingColumnIndex; i++)
|
|
//{
|
|
// scrollvalue += Convert.ToInt32(columnswidthvalue[i]);
|
|
//}
|
|
Decimal precent = decimal.Parse(this.dataGridView1.FirstDisplayedScrollingColumnIndex.ToString()) / decimal.Parse(this.dataGridView1.Columns.Count.ToString());
|
|
this.panel1.HorizontalScroll.Value = e.NewValue;
|
|
}
|
|
|
|
private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
|
|
{
|
|
this._bindingSource.MoveFirst();
|
|
}
|
|
|
|
private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
|
|
{
|
|
this._bindingSource.MovePrevious();
|
|
}
|
|
|
|
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
|
|
{
|
|
this._bindingSource.MoveNext();
|
|
}
|
|
|
|
private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
|
|
{
|
|
this._bindingSource.MoveLast();
|
|
}
|
|
|
|
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
if (!_isPanelEdit)
|
|
{
|
|
DataGridView dgv = sender as DataGridView;
|
|
DataGridViewCell dgvc = dgv.CurrentCell;
|
|
if (dgvc == null)
|
|
return;
|
|
Rectangle rec = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
|
|
Size size = rec.Size;
|
|
this.comboBox1.Size = size;
|
|
this.comboBox1.Location = new Point(rec.Right - this.comboBox1.Width, rec.Y);
|
|
this.comboBox1.Visible = true;
|
|
this.comboBox1.BringToFront();
|
|
}
|
|
else
|
|
{
|
|
this.panel1.Visible = true;
|
|
if (this.dataGridView1.DisplayedColumnCount(false) == this.dataGridView1.Columns.Count)
|
|
{
|
|
this.panel1.Height = dataGridView1.RowTemplate.Height + 3;
|
|
}
|
|
else
|
|
{
|
|
this.panel1.Height = dataGridView1.RowTemplate.Height + 20;
|
|
calculateColumnsWidth();
|
|
this.panel1.Width = columnsWidth;
|
|
this.panel1.AutoScroll = true;
|
|
this.panel1.VerticalScroll.Visible = false;
|
|
//this.panel1.HorizontalScroll.Maximum = 220;
|
|
this.panel1.Scroll += new ScrollEventHandler(panel1_Scroll);
|
|
//this.panel1.Controls.Add(hScrollBar);
|
|
}
|
|
CreateEditBoxs(e.RowIndex);
|
|
}
|
|
}
|
|
|
|
void panel1_Scroll(object sender, ScrollEventArgs e)
|
|
{
|
|
this.panel1.HorizontalScroll.Value = e.NewValue;
|
|
int panelscroll = e.NewValue / 100;
|
|
dataGridView1.HorizontalScrollingOffset = e.NewValue;
|
|
}
|
|
|
|
|
|
|
|
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
this.comboBox1.Visible = false;
|
|
}
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
|
|
{
|
|
object o = this._bindingSource.Current;
|
|
this._bindingSource.Remove(o);
|
|
}
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void toolStripButton1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|