日期:2014-05-19  浏览次数:20851 次

C# 关于DataGrid单元格文本
当DataGrid的ReadOnly属性设置为True时,表格单元格中的文本还是能够选中,这样单元格的双击事件就没法响应,该怎么响应双击事件啊?还请高手指点!

------解决方案--------------------
对于选中文本问题可以考虑使用Label替换默认的Textbox,解决!
至于双击,看看能否通过HitTest来解决!
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Sony.Offer.WindowsUI
{
public class DataGridLabelColumn:DataGridTextBoxColumn
{
Label lbl = new Label();
public DataGridLabelColumn() : base()
{
lbl = new Label();
lbl.Parent = this.TextBox.Parent;
this.TextBox.Visible=false;
this.TextBox.VisibleChanged +=new EventHandler(VisibleChanged);
this.TextBox.SizeChanged +=new EventHandler(SizeChanged);
this.TextBox.TextChanged +=new EventHandler(TextChanged);
}

protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds,
bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
this.TextBox.Visible = false;
lbl.Visible = true;
lbl.BringToFront();
}

private void SizeChanged(object sender, EventArgs e)
{
lbl.Location = this.TextBox.Location;
lbl.Size = new Size(this.TextBox.Size.Width, this.TextBox.Size.Height);
}

private void VisibleChanged(object sender, EventArgs e)
{
lbl.Location = this.TextBox.Location;
lbl.Size = new Size(this.TextBox.Size.Width, this.TextBox.Size.Height);
}

private void TextChanged(object sender, EventArgs e)
{
lbl.Text = this.TextBox.Text;
}
}
}