注册 登录
编程论坛 VB.NET论坛

如何获得datagridview中当前编辑单元格中的已选择数据?

wangzhenghua 发布于 2012-11-05 06:56, 2248 次点击
请问如何获得datagridview中当前编辑单元格中的已选择数据?如某一单元格中包含数据“12345”,我只想获得123,如何实现?
2 回复
#2
jianjunfeng2012-11-20 21:46
回复 楼主 wangzhenghua
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged

        Dim str As String
        With Me.DataGridView1
            str = .Item(.CurrentCell.ColumnIndex, .CurrentCell.RowIndex).Value.ToString '获得datagridview中当前单元格中的已选择数据
            str = Mid(str, 1, 3) '选择前面3位数,取得123            
        End With
    End Sub
#3
mmxo2012-11-22 23:46
取不到内部控件的情况下只有模拟,能取到就好办了,下面给出模拟法,久没用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 编辑 ]
1