Saturday, January 16, 2010

How do you get the current row and column of a datagridview control in C#?

In VB6 it is grid.row and grid.col. I have searched the mountains of help and found nothing. CurrentRow.index doesn't see to be it. Also, there is confusion between the graphical row/col and the row and column collections.How do you get the current row and column of a datagridview control in C#?
You can use:





- grid.CurrentCell.RowIndex


- grid.CurrentCell.ColumnIndex





to get the respective indexes (they are zero-based)How do you get the current row and column of a datagridview control in C#?
Each row and column can be obtained in the DataGrid OnDataBind method in the code behind.





void Item_Bound(Object sender, DataGridItemEventArgs e)


{


if((e.Item.ItemType == ListItemType.Item) ||


(e.Item.ItemType == ListItemType.AlternatingItem))


{





// Retrieve the text of Column 2 from the DataGridItem


// and convert the value to a Double.


Double Price = Convert.ToDouble(e.Item.Cells[2].Text);





// Format the value as currency and redisplay it in the DataGrid.


e.Item.Cells[2].Text = Price.ToString(';c';);





}


}

No comments:

Post a Comment