Datagrid

  • Thread starter Thread starter ManuNeko
  • Start date Start date
M

ManuNeko

Can anyone tell me how to work with a datagrid or where I can find a
tutorial or an example?

I have a datagrid on my form and now if a user adds a row I want to
insert that row into the database. I have absolutely no idea even how
and where to start looking to do that. I have been asking questions
and looking on the Internet for a long time. So I'm strating to think
that this is one of the best kept secrets on this planet. May be
Microsoft does NOT want anybody to know how to work with their
prducts.
:evil: :twisted: :x
 
Manu,

I strongly disagree that Microsoft doesn't want anyone to know how to use
their products. This newsgroup, and a whole other number of sites, not to
mention the 3 gigabyte MSDN you have with visual studio, are all aids to
understanding "How to use their products".

Specifically about how to persist an added row to the database, what kind of
datagrid are you using? Web/Windows?

There are many answers to this question, but what might be a seemingly
simple question could have many answers, simply because the datagrid shows
you a dataset (or a bindable object), whose structure might be totally
different from your database structure. So depending upon the situation, you
could use SqlDataAdapter.Update (once your dataset has been updated with the
new row), or you might have to specify an insertcommand, or at the simplest
you could do SqlCommand.ExecuteQuery.

I can give you a few pointers, but you would need to do the reading
yourself. The objects that might interest you are - Dataset, DataTable,
SqlCommand, SqlDataAdapter.

Or invest in a good book on ADO.NET. I've written one, you might like it.

- Sahil Malik
You can reach me thru my blog -
http://www.dotnetjunkies.com/weblog/sahilmalik
 
Hi ManuNeko:

Inserting a new record is typically done from a form tailored to
display and validate the fields for a single record, unlike the
DataGrid which displays a collection of records. Although it is
possible to do this with a DataGrid also, you have to consider what
the best user interface is for the task.

Here is one article demonstrates the technique:

"Inserting Data Into a SQL Database"
Description: The following code example shows how to add new rows of
data to a database using a DataGrid control and a custom HTML form
that accepts user input. This example demonstrates database security
best practices by using parameter objects for user input, rather than
using string concatenation. You can also add client-side or
server-side input validation to prevent malformed or malicious input.
http://msdn.microsoft.com/library/d...de/html/cpconinsertingdataintosqldatabase.asp

Also:

DataGrid In-place Editing (also covers insertion of a new record)
http://msdn.microsoft.com/msdnmag/issues/01/06/Cutting/default.aspx

Managing Hierarchical Inserts in ASP.NET and ADO.NET
http://msdn.microsoft.com/msdnmag/issues/03/07/DataPoints/default.aspx

Walkthrough: Editing an Access Database with ADO.NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adon_wtaccessdb.asp


HTH,
 
Back
Top