| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2227 人关注过本帖
标题:如何获得datagridview中当前编辑单元格中的已选择数据?
只看楼主 加入收藏
wangzhenghua
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-12-21
收藏
 问题点数:0 回复次数:2 
如何获得datagridview中当前编辑单元格中的已选择数据?
请问如何获得datagridview中当前编辑单元格中的已选择数据?如某一单元格中包含数据“12345”,我只想获得123,如何实现?
搜索更多相关主题的帖子: 编辑 如何 
2012-11-05 06:56
jianjunfeng
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:42
专家分:166
注 册:2009-3-13
收藏
得分:0 
回复 楼主 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
2012-11-20 21:46
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:0 
取不到内部控件的情况下只有模拟,能取到就好办了,下面给出模拟法,久没用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 编辑 ]

为提高中华编程水平而奋斗
2012-11-22 23:46
快速回复:如何获得datagridview中当前编辑单元格中的已选择数据?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016339 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved