Turtleburn1
asked on
How can I determine if a Cell in a Datagridview has been changed?
Is there a way to determine if a cell has been edited on a datagrid view? My datagridview is not bound to a dataset. My program walkes thru the datagridview and saves a row in a table for every cell on the datagridview. I would like to avoid having to save the data in all cells if only a few have been edited when the save button is pressed.
Hi,
You can use RowState for this purpose.
and on completion of save task do AcceptChanges
You can use follwoing code
void save(DataSet ds)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[0].RowS tate == DataRowState.Added)
{
//Perform insert Task
}
else if (ds.Tables[0].Rows[0].RowS tate == DataRowState.Modified)
{
//Perform update Task
}
}
ds.AcceptChanges();
}
Thanks
You can use RowState for this purpose.
and on completion of save task do AcceptChanges
You can use follwoing code
void save(DataSet ds)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[0].RowS
{
//Perform insert Task
}
else if (ds.Tables[0].Rows[0].RowS
{
//Perform update Task
}
}
ds.AcceptChanges();
}
Thanks
Sorry, I added C# code, in vb.net you can do as following
Sub Save(ByVal ds As DataSet)
For i As Integer = 0 To i < ds.Tables(0).Rows.Count
If (ds.Tables(0).Rows(i).RowS tate = DataRowState.Added) Then
'Perform insert Task
ElseIf (ds.Tables(0).Rows(i).RowS tate = DataRowState.Modified) Then
'Perform update Task
End If
Next
ds.AcceptChanges()
End Sub
Sub Save(ByVal ds As DataSet)
For i As Integer = 0 To i < ds.Tables(0).Rows.Count
If (ds.Tables(0).Rows(i).RowS
'Perform insert Task
ElseIf (ds.Tables(0).Rows(i).RowS
'Perform update Task
End If
Next
ds.AcceptChanges()
End Sub
ASKER
Thanks for the responses, I am using vb.net.
Not sure we are on the same page. I am not using a dataset and the DataGridView is not bound. The grid has about 50 rows and 10 cells in each row. Each cell on the grid represents a row of data on a SQL table. I am manually loading the data from the table to the grid and manually saving the data from the grid to the table.
The CellValueChanged event will not work because I don't want to save the cell data every time a cell is edited, I want to save the data once the save button on my form is pressed. The user could have changed multiple cells before this happens.
I was hoping there was a way to determine if a cell has been changed as I walk thru all the cells after the save buttion has been pressed.
Not sure we are on the same page. I am not using a dataset and the DataGridView is not bound. The grid has about 50 rows and 10 cells in each row. Each cell on the grid represents a row of data on a SQL table. I am manually loading the data from the table to the grid and manually saving the data from the grid to the table.
The CellValueChanged event will not work because I don't want to save the cell data every time a cell is edited, I want to save the data once the save button on my form is pressed. The user could have changed multiple cells before this happens.
I was hoping there was a way to determine if a cell has been changed as I walk thru all the cells after the save buttion has been pressed.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are bind grid with datatable then are you using yor custom object list?
ASKER
IJZ - the datagridview is not bound.
Dhaest - I believe what you suggest will work
I was hoping there was something like the rowstate at the cell level that I was not aware of, but it looks like that is not the case.
I thought of a solution myself. When I retrieve the data for the grid I could save it in a collection and then when the save button is pressed, I can compare the data in the collection to the data in the grid and any cell that is different, I trigger the update to the database. I think this will work since the data I am retrieving is a fixed and relativly small sized.
Dhaest - I believe what you suggest will work
I was hoping there was something like the rowstate at the cell level that I was not aware of, but it looks like that is not the case.
I thought of a solution myself. When I retrieve the data for the grid I could save it in a collection and then when the save button is pressed, I can compare the data in the collection to the data in the grid and any cell that is different, I trigger the update to the database. I think this will work since the data I am retrieving is a fixed and relativly small sized.
That's indeed also a possible solution for small sized data (otherwise there will be a lot of memory-consuming overhead)
If you do so, you can use the DataGridView.CellValueChan
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx