Datagrid binding and updating

timothym

New member
Joined
Jul 29, 2005
Messages
3
Programming Experience
3-5
Hi,

I have done the following code for databinding a datagrid to a dataset which is filled by a query I make on a database table.

Private
Sub dgDataMain_Databind()
'Bind the Data in ShedMain to the DataGrid

'The DataSet that holds the data.
Dim dsMain As DataSet

'Load the data.
Dim daMain As SqlDataAdapter

'Create the SqlDataAdapter.
Dim strSQL AsString = GetdgDataMainSQLStr() 'The query string

daMain =
New SqlDataAdapter(strSQL, conn) 'conn is defined publicly

'Map Table to ShedMain.
daMain.TableMappings.Add("Table", "ShedMain")

'Fill the DataSet.
dsMain = New DataSet
daMain.Fill(dsMain)

'Bind the DataGrid control to the ShedMain DataTable.
dgDataMain.SetDataBinding(dsMain, "ShedMain")

EndSub

This function is called on loading the form, and on inserting a new record in the ShedMain table, after validation and the new row is committed.
Now, what I would like to know is how to go about reflecting updates in the datagrid cells/rows to the database table 'ShedMain'.
What I would like is that on changing the cells of the datagrid, the user presses an 'Update' button, then the same (if possible?) validation function that is used for A SINGLE NEW INSERTION OF A NEW ROW will be used on all the changed rows and ultimately change the 'ShedMain' table.
I would greatly appreciate any help...Thanks in advance!

Timothy

 
Set the dataAdapter's UpdateCommand, InsertCommand, and DeleteCommand to valid SqlCommand objects. The SqlCommand objects will need valid CommandText and most likely parameters.
If you were to use the designer to add a SqlDataAdapter component, you could use the Data Adapter Configuration Wizard to setup all the SqlCommands and Parameters.
With all the SqlCommands setup correctly, you only need to call the Update method of the DataAdapter to update the source table with the changes in the dataSet/dataTable.
 
Thanks!

The problem is that I would like to be able to perform validation on the cells updated (from possibly different rows) before updating the database table. I do not understand exactly how to go about this. If it is done with parameters, could you please give me an example of how they can be configured to perform validation?

The following is the function (in a module) which performs validation:

VB.NET:
[size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size][size=2] IsDatainvalidForInsert([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] CaseNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] CaseYear[/size][size=2][color=#0000ff] As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] strDate [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean
[/color][/size][size=2][/size][size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]Dim[/color][/size][size=2] bError [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2] = [/size][size=2][color=#0000ff]False
[/color][/size][size=2][color=#008000]'Case number field must be 1-5 digits
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] Len(CaseNumber) > 5 [/size][size=2][color=#0000ff]Or[/color][/size][size=2] Len(CaseNumber) = 0 [/size][size=2][color=#0000ff]Or[/color][/size][size=2] [/size][size=2][color=#0000ff]Not[/color][/size][size=2] IsNumeric(CaseNumber) [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]MsgBox("Case Number is invalid! Must be 1-5 digits.", MsgBoxStyle.Exclamation)
bError = [/size][size=2][color=#0000ff]True
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If
[/color][/size][size=2][/size][size=2][color=#008000][/color][/size] 
[size=2][color=#008000]'Case year field must be 2-4 digits[/color][/size]
[size=2][color=#0000ff]If[/color][/size][size=2] Len(CaseYear) > 4 [/size][size=2][color=#0000ff]Or[/color][/size][size=2] Len(CaseYear) < 2 [/size][size=2][color=#0000ff]Or[/color][/size][size=2] [/size][size=2][color=#0000ff]Not[/color][/size][size=2] IsNumeric(CaseYear) [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]MsgBox("Case Year is invalid! Must be 2-4 digits.", MsgBoxStyle.Exclamation)
bError = [/size][size=2][color=#0000ff]True
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If
[/color][/size][size=2][/size][size=2][color=#008000]'Date must be valid dd/mm/yyyy date and not in the future
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] (([/size][size=2][color=#0000ff]Not[/color][/size][size=2] IsDate(strDate)) [/size][size=2][color=#0000ff]Or[/color][/size][size=2] Len(strDate) <> 10) [/size][size=2][color=#0000ff]And[/color][/size][size=2] strDate <> "" [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]MsgBox("Date is invalid!", MsgBoxStyle.Exclamation)[/size]
[size=2]bError = [/size][size=2][color=#0000ff]True[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]ElseIf[/color][/size][size=2] (strDate <> "") [/size][size=2][color=#0000ff]Then
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] [/size][size=2][color=#0000ff]CDate[/color][/size][size=2](strDate) > Now [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]MsgBox("Date is invalid!", MsgBoxStyle.Exclamation)
bError = [/size][size=2][color=#0000ff]True
[/color][/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]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2]IsDatainvalidForInsert = bError
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function
[/color][/size]

Thanks again,
Timothy
 
Back
Top