取不到内部控件的情况下只有模拟,能取到就好办了,下面给出模拟法,久没用VB了,不记得怎么写代码了,用C#请见谅,另外数据是随机的,用的int类型,具体的数据处理部分到时你要根据你的情况修改。
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
程序代码:
using System;
using System.Data;
using System.Windows.Forms;
namespace GetSelectionInDataGridViewEditingCell
{
public partial class FormMain : Form
{
private readonly TextBox _textBox;
public FormMain()
{
InitializeComponent();
_textBox = new TextBox { Visible = false };
_textBox.KeyDown += TextBox_KeyDown;
_textBox.KeyUp += TextBox_KeyUp;
_textBox.MouseMove += TextBox_MouseMove;
_textBox.LostFocus += TextBox_LostFocus;
Controls.Add(_textBox);
_textBox.BringToFront();
var table = new DataTable();
for (var i = 0; i < 10; i++)
table.Columns.Add(string.Concat("C", i), typeof (int));
var random = new Random(DateTime.Now.Millisecond);
for (var i = 0; i < 10; i++)
{
var values = new object[10];
for (var q = 0; q < 10; q++)
values[q] = random.Next(int.MinValue, int.MaxValue);
table.Rows.Add(values);
}
Dgv.DataSource = table;
Dgv.CellBeginEdit += Dgv_CellBeginEdit;
}
void Dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
_textBox.Text = Dgv.CurrentCell.EditedFormattedValue.ToString();
var R = Dgv.GetCellDisplayRectangle(Dgv.CurrentCell.ColumnIndex, Dgv.CurrentCell.RowIndex, false);
_textBox.SetBounds(R.X + Dgv.Location.X, R.Y + Dgv.Location.Y, R.Width, R.Height);
_textBox.Visible = true;
}
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Escape && e.KeyCode != Keys.Enter) return;
switch (e.KeyCode)
{
case Keys.Enter:
{
int value;
if (int.TryParse(_textBox.Text, out value))
Dgv.CurrentCell.Value = int.Parse(_textBox.Text);
}
break;
}
_textBox.Visible = false;
}
void TextBox_KeyUp(object sender, KeyEventArgs e)
{
Label.Text = _textBox.SelectedText;
}
void TextBox_LostFocus(object sender, EventArgs e)
{
_textBox.Visible = false;
}
void TextBox_MouseMove(object sender, MouseEventArgs e)
{
Label.Text = _textBox.SelectedText;
}
}
}
[
本帖最后由 mmxo 于 2012-11-22 23:53 编辑 ]