Datagrid Help

LaTTicE

Member
Joined
Jun 14, 2004
Messages
11
Programming Experience
1-3
I have read items from SQL Server into a datagrid that I need to allow people to edit. The only problem I am having with this, is after they are done editing what they need to. There is only one column out of 4 that is editable, but I can't figure out how to update the old data with the new and save it to the database. The grid automatically lets people edit it at runtime I guess, but I just can't seem to figure out how to make it so the database takes those edits and saves them. Please Help

Thanks in advance
 
How are you reading the data? Using a dataAdapter/dataSet?

If you are using a dataAdapter to fill a dataset, you should be able to use the Update method to update the changes.

Post some of your read code if possible.
 
Thanks paszt. I actually was able to figure it out while waiting for a reply. I am actually using a for loop to iterate through the datagrid and update the database accordingly. Right now, I am trying to figure out how to programmatically iterate through each row, get that row index, and use that row index to validate that the user input is between two values. I know something is wrong here and it is probably straightforward, but I can't find what. Min and max are static values, while Actual will vary with user input. Appreciate any help...

VB.NET:
[size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] (DSet.Tables(0).Rows.Count - 1)

Actual = dgDisplay.Item(i, 3)

[/size][size=2][color=#0000ff]If[/color][/size][size=2] [/size][size=2][color=#0000ff]CDbl[/color][/size][size=2](Actual) < [/size][size=2][color=#0000ff]CDbl[/color][/size][size=2](Min) [/size][size=2][color=#0000ff]Or[/color][/size][size=2] [/size][size=2][color=#0000ff]CDbl[/color][/size][size=2](Actual) > [/size][size=2][color=#0000ff]CDbl[/color][/size][size=2](Max) [/size][size=2][color=#0000ff]Or[/color][/size][size=2] IsNumeric([/size][size=2][color=#0000ff]CDbl[/color][/size][size=2](Actual)) = [/size][size=2][color=#0000ff]False[/color][/size][size=2] [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]MessageBox.Show("Actual Value Must be Between Min and Max Values.", "Range", MessageBoxButtons.OK, MessageBoxIcon.Error)

[/size][size=2][color=#0000ff]Exit[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]Next

[/color][/size]
 
I would initially set Min and Max as doubles, that way you could leave out a couple of CDbls.

VB.NET:
If Not IsNumeric(Actual) OrElse CDbl(Actual) < Min OrElse CDbl(Actual) > Max Then ...

The order is important here because I'm using the short-circuiting operator OrElse. With the OrElse operator, if the first expression is true, the second expression isn't evaluated, in this case avoiding the error you would receive trying to perform the CDbl on a non-numeric value.
 
updating datagrid

LaTTicE said:
I have read items from SQL Server into a datagrid that I need to allow people to edit. The only problem I am having with this, is after they are done editing what they need to. There is only one column out of 4 that is editable, but I can't figure out how to update the old data with the new and save it to the database. The grid automatically lets people edit it at runtime I guess, but I just can't seem to figure out how to make it so the database takes those edits and saves them. Please Help

Thanks in advance

you should use the update command for the adapter who retreive the data from sql
 
Back
Top