使用DevExpress.XtraGrid控件时如何通过鼠标调整行高

原创|其它|编辑:郝浩|2011-12-26 21:49:24.000|阅读 2188 次

概述:在项目中使用XtraGrid控件时遇见这样一个问题,要是能够实现通过鼠标改变指定的行高,这样一来,终端用户使用起来便方便多了。但是,XtraGrid控件不能在内部缓存任何数据,因此目前并不能实现通过鼠标来指定行高的功能。然而,您可以自己实现这个功能。要实现此功能,你需要处理GridView控件的MouseDown, MouseUp以及CalcRowHeight事件。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  在项目中使用XtraGrid控件时遇见这样一个问题,要是能够实现通过鼠标改变指定的行高,这样一来,终端用户使用起来便方便多了。»更多DevExpress开发资源与帮助文档

  但是,XtraGrid控件不能在内部缓存任何数据,因此目前并不能实现通过鼠标来指定行高的功能。然而,您可以自己实现这个功能。要实现此功能,你需要处理GridView控件的MouseDown, MouseUp以及CalcRowHeight事件。在MouseDown和MouseUp事件中就会涉及到改变行的大小。而在CalcRowHeight事件处理器中,你得为指定的行设置有效的行高。通过下面的代码示例可以展示一个此功能的实现:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Grid3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private DevExpress.XtraGrid.GridControl gridControl1;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.gridControl1 = new DevExpress.XtraGrid.GridControl();
this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
this.SuspendLayout();
//
// gridControl1
//
this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
//
// gridControl1.EmbeddedNavigator
//
this.gridControl1.EmbeddedNavigator.Name = "";
this.gridControl1.Location = new System.Drawing.Point(0, 0);
this.gridControl1.LookAndFeel.SkinName = "Money Twins";
this.gridControl1.LookAndFeel.Style =

DevExpress.LookAndFeel.LookAndFeelStyle.Skin;
this.gridControl1.LookAndFeel.UseDefaultLookAndFeel = false;
this.gridControl1.LookAndFeel.UseWindowsXPTheme = false;
this.gridControl1.MainView = this.gridView1;
this.gridControl1.Name = "gridControl1";
this.gridControl1.Size = new System.Drawing.Size(776, 422);
this.gridControl1.TabIndex = 0;
this.gridControl1.UseEmbeddedNavigator = true;
this.gridControl1.ViewCollection.AddRange

(new DevExpress.XtraGrid.Views.Base.BaseView[] {
this.gridView1});
//
// gridView1
//
this.gridView1.GridControl = this.gridControl1;
this.gridView1.Name = "gridView1";
this.gridView1.OptionsCustomization.AllowRowSizing = true;
this.gridView1.CalcRowHeight += new
DevExpress.XtraGrid.Views.Grid.RowHeightEventHandler(this.gridView1_CalcRowHeight);
this.gridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gridView1_MouseDown);
this.gridView1.MouseUp +=
new System.Windows.Forms.MouseEventHandler(this.gridView1_MouseUp);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(776, 422);
this.Controls.Add(this.gridControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

Hashtable rowsSizes;
const int defaultRowHeight = 30;

private void Form1_Load(object sender, System.EventArgs e) {
rowsSizes = new Hashtable();
InitData();
FillData();
InitGridColumns();
InitDataEvents();
}

//<gridControl1>
private DataTable data;
private void InitData() {
data = new DataTable("ColumnsTable");
data.BeginInit();
AddColumn(data, "ID", System.Type.GetType("System.Int32"), true);
AddColumn(data, "First Name", System.Type.GetType("System.String"));
AddColumn(data, "Last Name", System.Type.GetType("System.String"));
AddColumn(data, "Payment Type", System.Type.GetType("System.String"));
AddColumn(data, "Customer", System.Type.GetType("System.Boolean"));
AddColumn(data, "Payment Amount", System.Type.GetType("System.Single"));
data.EndInit();
}
//</gridControl1>

private void AddColumn(DataTable data, string name, System.Type type)
{ AddColumn(data, name, type, false); }
private void AddColumn(DataTable data, string name, System.Type type, bool ro) {
DataColumn col;
col = new DataColumn(name, type);
col.Caption = name;
col.ReadOnly = ro;
data.Columns.Add(col);
}

private void FillData() {
string[,] sNames = new string[,] { {
"Elizabeth", "Lincoln"}, {"Yang", "Wang"}, {
"Patricio", "Simpson"},
{"Francisco", "Chang"}, {
"Ann", "Devon"}, {"Roland", "Mendel"}, {
"Paolo", "Accorti"}, {"Diego", "Roel"}};
string[] sType = new string[] {"Visa", "Master", "Cash"};
data.Clear();
Random rnd = new Random();
for(int i = 0; i <= sNames.GetUpperBound(0); i++)
data.Rows.Add(new object[] {i + 1, sNames[i,0], sNames[i,1], sType[i % 3],
rnd.Next(-1, 1), rnd.Next(10000) *
0.01});
}

private void InitGridColumns() {
gridControl1.DataSource = data;
gridControl1.DefaultView.PopulateColumns();
gridView1.Columns["Payment Amount"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
gridView1.Columns["Payment Amount"].DisplayFormat.FormatString = "c";
gridView1.BestFitColumns();
}

private void InitDataEvents() {
data.RowDeleting += new DataRowChangeEventHandler(data_RowDeleting);
}

private void data_RowDeleting(object sender, DataRowChangeEventArgs e) {
rowsSizes.Remove(e.Row);
}

private void gridView1_CalcRowHeight(object sender,

DevExpress.XtraGrid.Views.Grid.RowHeightEventArgs e) {
object rowHeight = rowsSizes[gridView1.GetDataRow(e.RowHandle)];
if (rowHeight != null)
e.RowHeight = Convert.ToInt32(rowHeight);
else
e.RowHeight = defaultRowHeight;
}

int resizeStartPos;
int resizingRowHandle;

private void gridView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi = gridView1.CalcHitInfo(e.X, e.Y);
if (hi.HitTest == DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitTest.RowEdge) {
resizeStartPos = e.Y;
resizingRowHandle = hi.RowHandle;
}
}

private void gridView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if (resizingRowHandle >= 0) {
resizeStartPos = e.Y - resizeStartPos;
object rowSize = rowsSizes[gridView1.GetDataRow(resizingRowHandle)];
int newRowSize;
if (rowSize != null)
newRowSize = Convert.ToInt32(rowSize);
else
newRowSize = defaultRowHeight;
newRowSize += resizeStartPos;
if (newRowSize < defaultRowHeight)
newRowSize = defaultRowHeight;
rowsSizes[gridView1.GetDataRow(resizingRowHandle)] = newRowSize;
resizingRowHandle = -1;
}
}

}
}

下载《XtraGrid Suite


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)



标签:DevExpress

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP