DataGrid and DataSet

  • Thread starter Thread starter John
  • Start date Start date
J

John

Dear all,

I have a question about adding a data row into the data
table. I have developed a form with a datagrid in it.
The datagrid is bound to a database table
named "products". I have added a "add" button into the
form and I write the following code:

dim newRec as datarow

newRec = dtProducts.NewRow()
newRec.BeginEdit
newRec(0) = ""
newRec.EndEdit
dtProducts.rows.add(newRec)


It works fine. The data row is appended to the end of the
datagrid when I click the "add" button. However, I don't
know how to set this row at the current row in the
datagrid. Do any one can help me? In addition, how can I
write the code to ensure that something must be entered in
the key column (cannot leave it blank). I have no idea
how to do it. Do any one give me some suggestion?

Thanks,
John
 
DataGrid's have a currentcell property. There are a couple
ways to ensure that something is entered depedning on how
you want the user to resond. The most obtrusive is to
fire and update after the cell change and wait for the
exception if they messed up.

You can use the currentcell to determine what row and
colum they are in, and if they attempt to leave the cell,
check it's value if it's column is say 2 for instance and
this is the key field being bound. Fire a message box or
whatever else you want to do to respond.

You could also put a default value in there for them
depening on the app but I don't think that's what you want
to do.

Now, you also know the number of rows in your
DataTable...DataTable.Rows.Count. When the row is added,
you can use CurrentRowIndex to Set the value of the
current cell as well.

I think that should get you what you need, but if not, let
me know.

Good Luck,

Bill
 
Back
Top