一个DataGrid组例子
- 时间:2015年04月02日 15:41:03 来源:魔法猪系统重装大师官网 人气:17041
namespace ITMBA.BusinessRules.CommonBaseClassBR.Dock
{
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data.SqlClient;
///
/// DataGridBR重写了DataGrid组件
///
public class DataGridBR : System.Windows.Forms.DataGrid
{
//创建数组
private System.Windows.Forms.DataGrid [] _DataGrid;
private System.Windows.Forms.Form tempForm;
//DataGrid索引
private int _index = 0;
//设置当前操作DataGrid索引
private int _CurrentIndex=0;
//当前选择值
private DataView _ParentDV;//操作的父DataGrid的视图
private int _ParentRow;//操作的父DataGrid的行索引
private int _ParentColumn;//操作的父DataGrid的列索引
///
/// 操作的父DataGrid的视图
///
public DataView ParentDV
{
get
{
return _ParentDV;
}
set
{
_ParentDV = value;
}
}
///
/// 操作的父DataGrid的行索引
///
public int ParentRow
{
get
{
return _ParentRow;
}
set
{
_ParentRow = value;
}
}
///
/// 操作的父DataGrid的列索引
///
public int ParentColumn
{
get
{
return _ParentColumn;
}
set
{
_ParentColumn = value;
}
}
///
/// 构造函数
///
/// 容器窗口
/// 当前操作的视图
/// 选择的行索引
/// 选择的列索引
/// 将创建DataGrid数目
public DataGridBR(System.Windows.Forms.Form owner,DataView dv,int row,int column, int Num)
{
_DataGrid = new DataGrid[Num];
for(int i=0;i
}
tempForm = owner;
ParentDV = dv;
ParentRow = row;
ParentColumn = column;
}
///
/// 当前操作DataGrid索引
///
public int CurrentIndex{
set{
_CurrentIndex = value;
}
get{
return _CurrentIndex;
}
}
///
/// 初始化数据源
///
/// 数据源
public void AddSource(System.Data.DataView dv){
if(dv.Count>0){
_DataGrid[_index++].DataSource = dv;
}
}
///
/// 取得已创建的DataGrid数目
///
///
public int GetNumEntries(){
return _index;
}
///
/// 允许数组式访问
///
public System.Windows.Forms.DataGrid this[int index]{
get{
_DataGrid[index].Dock = DockStyle.Fill;
_DataGrid[index].RowHeadersVisible = true;
_DataGrid[index].CaptionVisible = false;
_DataGrid[index].DoubleClick += new System.EventHandler(this.DataGridBR_DoubleClick);
return _DataGrid[index];
}
set{
_DataGrid[index] = value;
}
}
///
/// 选择事件
///
///
///
private void DataGridBR_DoubleClick(object sender, System.EventArgs e)
{
DataView tempDV = (DataView)_DataGrid[CurrentIndex].DataSource;
int Count = tempDV.Count;
if(Count != 0)
{
//MessageBox.Show(_DataGrid[CurrentIndex].CurrentRowIndex.ToString());
int tempNo = _DataGrid[CurrentIndex].CurrentRowIndex;
DataSet ds = tempDV.Table.DataSet;
//MessageBox.Show(ds.Tables[0].Rows[tempNo][0].ToString());
DataSet ds0 = ParentDV.Table.DataSet;
ds0.Tables[0].Rows[ParentRow][ParentColumn] = ds.Tables[0].Rows[tempNo][0].ToString();
//MessageBox.Show("row:"+ParentRow.ToString()+",column:"+ParentColumn.ToString());
tempForm.Close();
}
else
{
MessageBox.Show("没有记录被选中","信息提示",MessageBoxButtons.OK);
}
}
}
}