110,534
社区成员
发帖
与我相关
我的任务
分享
/*
* Write By yty at 2009/8/24
* WeiKe (C) 2009
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace KitchenMana.UInter
{
public partial class DataGridViewRowEx : DataGridView
{
private UserControl _ucDetails;
private int _iRowHeight;
public DataGridViewRowEx()
{
InitializeComponent();
this.ScrollBars = ScrollBars.Vertical;
this._iRowHeight = 30;
this.Font = new Font("黑体", 12.5f);
//
this.RowHeadersWidth = 25;
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
[Browsable(true),
Category("扩展属性")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
[Category("扩展属性"),
Description("获取或设置细节显示控件")]
public UserControl DetailControl
{
get
{
return this._ucDetails;
}
set
{
if (value != null)
{
this._ucDetails = value;
if (this.Width > 32 + this.RowHeadersWidth)
{
this._ucDetails.Width = this.Width - 32 - this.RowHeadersWidth;
}
this._ucDetails.Visible = false;
this.Controls.Add(this._ucDetails);
}
}
}
[Category("扩展属性"),
Description("获取或设置默认行高")]
public int DefaultRowHeight
{
get
{
return this._iRowHeight;
}
set
{
if (value > 20)
{
this._iRowHeight = value;
}
}
}
protected void ReSetRowHeight()
{
for (int i = 0; i < this.RowCount; i++)
{
this.Rows[i].Height = this._iRowHeight;
}
}
private void TreatTheDetailControl()
{
if (this._ucDetails != null)
{
if (this.CurrentRow.Index >= 0)
{
//ReSetRowHeight();
if (this.CurrentRow.Index >= this.FirstDisplayedCell.RowIndex)
{
this.CurrentRow.Height = this._ucDetails.Height;
this._ucDetails.Top = this.ColumnHeadersHeight + this._iRowHeight * (
this.CurrentRow.Index - this.FirstDisplayedCell.RowIndex);
this._ucDetails.Left = this.RowHeadersWidth;
this._ucDetails.Show();
}
else
{
this._ucDetails.Hide();
}
}
}
}
protected override void OnCellClick(DataGridViewCellEventArgs e)
{
base.OnCellClick(e);
ReSetRowHeight();
TreatTheDetailControl();
}
protected override void OnScroll(ScrollEventArgs e)
{
base.OnScroll(e);
TreatTheDetailControl();
//ReSetRowHeight();
}
protected override void OnCurrentCellChanged(EventArgs e)
{
base.OnCurrentCellChanged(e);
ReSetRowHeight();
TreatTheDetailControl();
}
}
}
private Form2 frm;
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = GetData();
this.dataGridView1.DataSource = dt.DefaultView;
}
private DataTable GetData()
{
DataTable dtData = new DataTable();
using (SqlConnection thisConnection = new SqlConnection(
@"Data Source=HUI\SQLSERVER;Initial Catalog=Northwind;Integrated Security=True"))
{
using (SqlDataAdapter thisAdapter = new SqlDataAdapter(
"SELECT * FROM Customers", thisConnection))
{
thisAdapter.Fill(dtData);
}
thisConnection.Close();
}
return dtData;
}
private void Form1_Load(object sender, EventArgs e)
{
frm = new Form2();
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
this.dataGridView1.Controls.Add(frm);
frm.Hide();
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dataGridView1.CurrentCell==null)
return;
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex + 1,
dataGridView1.CurrentCell.RowIndex + 1, false);
frm.Location = new Point(rect.Left - 50, rect.Top - 10);
frm.Size = new Size(200, 200);
frm.BackColor = Color.Yellow;
//此处可对frm进行初始化值
frm.Show();
}
private void dataGridView1_Leave(object sender, EventArgs e)
{
frm.Hide();
}