简体   繁体   中英

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

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

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:

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

You can do that by calling BeginEdit() method for the DataGridView control. It will start edit mode for the selected cell. You can also set the EditMode of the DataGridView control:

//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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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