简体   繁体   English

在datagridview c#中仅选择单元格的文本

[英]Select only a text of cell in datagridview c#

in a datagridview I want to select only the text of the cell and not the whole cell 在datagridview中,我只想选择单元格的文本,而不是整个单元格

if I put 如果我放

Datagridview.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;

the entire cell is selected 整个单元格被选中

is there a way to select only the text of the cell like a vb6 有没有办法只选择像vb6这样的单元格文本

Datagridview.SelStart=0
Datagridview.SelLength=Len(Datagridview.Text)

thanks for help 感谢帮助

BeginEdit will select everything in the cell but you can specify the start and length of selection by: BeginEdit将选择单元格中的所有内容,但您可以通过以下方式指定选择的开始和长度:

        ((TextBox)dgv.EditingControl).SelectionStart = 0;
        ((TextBox)dgv.EditingControl).SelectionLength = 3;

You can do that by calling BeginEdit() method for the DataGridView control. 您可以通过为DataGridView控件调用BeginEdit()方法来实现。 It will start edit mode for the selected cell. 它将启动所选单元格的编辑模式。 You can also set the EditMode of the DataGridView control: 您还可以设置DataGridView控件的EditMode

//set selected cell as you showed in your question
datagridview1.CurrentRow.Cells.[Datagridview.CurrentCell.ColumnIndex].Selected=true;

//call BeginEdit with true argument which will select all text within the cell
dataGridView1.BeginEdit(true);

//optionally set the EditMode before you call BeginEdit
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;

要以编程方式获取当前单元格:

string msg = String.Format("Row: {0}, Column: {1}", dataGridView1.CurrentCell.RowIndex, dataGridView1.CurrentCell.ColumnIndex); MessageBox.Show(msg, "Current Cell");

This work for me, after adding a new row i want to edit the 2nd column of the row being added. 在添加新行之后,我想编辑要添加的行的第二列。 at same time the value of the cell is being highlighted. 同时突出显示该单元格的值。 Hope this helps" 希望这可以帮助”

        DataGridViewCell cellQty = dgvBundle.Rows[dgvBundle.Rows.Count - 1].Cells[1];
        dgvBundle.CurrentCell = cellQty;            
        dgvBundle.CurrentRow.Cells[1].Selected = true;
        dgvBundle.BeginEdit(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM