Click here to Skip to main content
14,865,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how can i know the datagridview cell location?...How can i edit the gridview values fetched from DB...Plz suggest me...
Posted

1 solution

With data grid views you can edit the values in cells directly using the control on the form, and it should automatically update the DB if it's data bound.

To edit programmatically, if you know the X and Y positions of what you're trying to retrieve/edit, you can use:
using System.Windows.Forms;

// Get the value
object obj = myDataGridView[X, Y].Value

// Set the value
myDataGridView[X, Y].Value = obj;


This will allow you to access the value directly.


However, you can't always explicitly state what you want to retrieve. If the user selects a row on the data grid view you can use:
// This retrieves the current row selected on the data grid view
DataGridViewRow row = myDataGridView.CurrentRow

// This gets the cell objects for the row
DataGridViewCellCollection cells = row.Cells;

// This gets the value stored for the column with header 'ColumnName' on the current row
object obj = cells["ColumnName"].Value;

// Use this to get the value in one line
object obj = myDataGridView.CurrentRow.Cells["ColumnName"].Value;

// Use this to set the value in one line
myDataGridView.CurrentRow.Cells["ColumnName"].Value = obj;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900