Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to access the cell values of datagrid in my code.
pls let me knw how to achieve the same.

If i select multiple rows and click on a context menu, I need to update all the cell values to a value 'Fixed'.

thanks,
sheethal
Posted
Updated 28-Sep-10 17:35pm
v4
Comments
Henry Minute 28-Sep-10 6:02am    
You should edit your question to indicate if it is Windows Forms, ASP or WPF or whatever.
Hiren solanki 28-Sep-10 6:17am    
based on the word 'datagrid' I can assume that it is a question of winforms. so minor edit for that.

C#
foreach (GridViewRow row in grid.Rows)
{
    foreach (TableCell cell in row.Cells)
    {
        string cellValue = cell.Text.ToString();
    }
}


Please vote and Accept Answer if it Helped.
 
Share this answer
 
v2
Comments
Sheethal R A 28-Sep-10 6:13am    
i want to get the selected rows and for all the rows selected i have to update a value to a particular column.
Henry Minute 28-Sep-10 6:54am    
Your edit to the original question may have been correct, or not, but the use of 'datagrid' certainly does not limit it to Win Forms. In fact, unless it is a very old version of the framework, 'datagridview' would do that.
C#
int PID = 0;
private void dgrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgrid.Rows.Count >= 0)
{
    if (e.RowIndex >= 0)
    {
PID = Convert.ToInt32(dgrid.Rows[e.RowIndex].Cells["PID"].Value.ToString());
    }
}
}


this way u can fetch the cell values..

if u want to update cell value to Database
use CellValueChanged Event..
 
Share this answer
 

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