DataGridView Gets or sets the contents of the current cell

Source: Internet
Author: User
Tags rowcount

The current cell refers to the cell in which the DataGridView focus is located, which can be obtained through the CurrentCell property of the DataGridView object. Returns null if the current cell does not exist.

Get the contents of the current cell:

Object obj = This.dgv_PropDemo.CurrentCell.Value;

Note: The return value is of type object.

Gets the column of the current cell index:

int columnindex = This.dgv_PropDemo.CurrentCell.ColumnIndex;

Gets the index of the row where the current cell is located:

int rowindex= This.dgv_PropDemo.CurrentCell.RowIndex;

In addition, use the Datagridview.currentcelladdress property to determine the row where the cell is located:

int row= This.dgv_propdemo.currentcelladdress.y;

Column:

int column = this.dgv_propdemo.currentcelladdress.x;

Note: The row and column indexes for DataGridView are all starting from 0.

The current cell can be changed by setting the CurrentCell of the DataGridView object.

Datagridview1.currentcell=datagridview1[int Columnindex,int RowIndex];

Note: If the selected mode of DataGridView is a row selection, the entire row in which the current cell is located is selected. Otherwise, only the current cell that is set is selected.

Set CurrentCell to NULL to deactivate the current cell.

Example: Setting the first row the second column is the current CurrentCell

This.dgv_PropDemo.CurrentCell = this.dgv_propdemo[1, 0];

Example: Traversing DataGridView with up and down implementations

/// <summary>        ///Traverse up/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtn_up_click (Objectsender, EventArgs e) {            //gets the index of the previous row            intUprowindex = This. Dgv_PropDemo.CurrentCell.RowIndex-1; if(Uprowindex <0)            {                //Select the last line                 This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, This. Dgv_propdemo.rowcount-1]; }            Else            {                 This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, Uprowindex]; }        }        /// <summary>        ///Traverse down/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtn_down_click (Objectsender, EventArgs e) {             //gets the index of the next row            intNextrowindex = This. Dgv_PropDemo.CurrentCell.RowIndex +1; if(Nextrowindex > This. Dgv_propdemo.rowcount-1)            {                 This. Dgv_propdemo.currentcell = This. dgv_propdemo[0,0]; }            Else            {                 This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, Nextrowindex]; }        }

DataGridView Gets or sets the contents of the current cell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.