473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to update data in Windows Datagrid

Hi,

I am writing a windows app in .net 2003. I have a datagrid which gets
data from a storedprocedure. My question is how can I update the data
in the datagrid? I want to call another storedprocedure to update the
data in the datagrid.

Thanks in advance.

Mar 22 '06 #1
6 4656
Shil,

The datagrid reflects the data in a datasource, therefore you will have to
update that datasource.

(How you get that data is not so important, the stored procedure to get data
is the same as a text procedure in ADONET, the only difference is where it
is located.)

I hope this helps,

Cor

"shil" <jo******@gmail.com> schreef in bericht
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,

I am writing a windows app in .net 2003. I have a datagrid which gets
data from a storedprocedure. My question is how can I update the data
in the datagrid? I want to call another storedprocedure to update the
data in the datagrid.

Thanks in advance.

Mar 22 '06 #2
Hi Cor,

I created a save button on the same form where I have datagrid. Here is
the code on that button click I worte.

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Save.Click
Dim myDa1 As SqlDataAdapter
Dim myCmd As SqlCommand

myCmd = New SqlCommand("sp_AP_Upd_AssignedUsers", myConn)
myCmd.CommandType = CommandType.StoredProcedure

Dim paramUserID As SqlParameter = New SqlParameter("@UserID",
SqlDbType.SmallInt)
myCmd.Parameters.Add(paramUserID)
paramUserID.Value = dgAssignedUsers.Item(0, 12)

Dim paramEmail2 As SqlParameter = New SqlParameter("@Email2",
SqlDbType.NVarChar, 50)
myCmd.Parameters.Add(paramEmail2)
paramEmail2.Value = dgAssignedUsers.Item(0, 7)

myCmd.ExecuteNonQuery()
End Sub

This works fine for the first row since I hardcoded that. How can I
loop through all rows in the datagrid? For example in web forms
datagrid I can do

For i = 0 To dgAssignedUses.Items.Count-1

Next

How can I perform the same thing in windows datagrid?

Thanks.

Mar 22 '06 #3
Shil,

In a windowforms datagrid or datagridview should you forget to loop through
the rows of a datagrid. It are just the cells that represents the
datasource.

In a windowform you loop through the rows of your datasource which is mostly
a datatable or a dataview. (You have not the problem that your datasource is
in fact at the service side).

The looping is than the same let say a dataview

\\\
for i as integer = 0 to dv.count - 1
if dv(i)("myfield") = "whatever" then
'the action to be done
end if
next
///

I hope this gives an idea

Cor

"shil" <jo******@gmail.com> schreef in bericht
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi Cor,

I created a save button on the same form where I have datagrid. Here is
the code on that button click I worte.

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Save.Click
Dim myDa1 As SqlDataAdapter
Dim myCmd As SqlCommand

myCmd = New SqlCommand("sp_AP_Upd_AssignedUsers", myConn)
myCmd.CommandType = CommandType.StoredProcedure

Dim paramUserID As SqlParameter = New SqlParameter("@UserID",
SqlDbType.SmallInt)
myCmd.Parameters.Add(paramUserID)
paramUserID.Value = dgAssignedUsers.Item(0, 12)

Dim paramEmail2 As SqlParameter = New SqlParameter("@Email2",
SqlDbType.NVarChar, 50)
myCmd.Parameters.Add(paramEmail2)
paramEmail2.Value = dgAssignedUsers.Item(0, 7)

myCmd.ExecuteNonQuery()
End Sub

This works fine for the first row since I hardcoded that. How can I
loop through all rows in the datagrid? For example in web forms
datagrid I can do

For i = 0 To dgAssignedUses.Items.Count-1

Next

How can I perform the same thing in windows datagrid?

Thanks.

Mar 23 '06 #4
Hi Shil,

You're doing it the wrong way, if you're trying to loop through the
cells of the Datagrid and manually update each item at the back-end.

Remember that the Datagrid just represents a visual view of the data
present in your DataTable or Dataset as the case might be.
Therefore, any changes made to the contents of the Datagrid are
automatically reflected in the contents of the Datatable or Dataset.
So, you're next task remains to update this information back to the
Database.

Hopefully the following will clear the doubt : (lines might break up)

DataGrid -----------------> DataSet / DataTable ---------> Database
(SQL Server)
(visual representation) (Logical representation) (Data store)

So, to update the Dataset back to the Database, just use the
SqlDataAdapter.Update(myDataSet, "TableName") method.

If you plan to have a stored procedure run when updating the Database,
make sure that the UpdateCommand property of your SqlDataAdapter points
to command object which is the stored procedure.

Regards,

Cerebrus.

Mar 23 '06 #5
Cerebrus,

My select query into dataset is quering multiple tables. It is not
simple select. So when I call Update command of dataadapter, I get this
error "Additional information: Dynamic SQL generation is not supported
against multiple base tables." How do I update data of a datagrid which
has data from multiple tables?

Thanks.

Mar 27 '06 #6
Hi shil,

Yes, that is natural. Use your own Update SQL statement as the
UpdateCommand.

myDataAdapter.UpdateCommand.CommandText = "UPDATE Categories SET
Description='Cheeses, Milk, Ice Cream' WHERE CategoryName='Dairy
Products'"

Regards,

Cerebrus.

Mar 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PAUL EDWARDS | last post by:
I have a windows form that is bound to a datatable. In VB6 I could just update the field contents and it would be updated in the database, however if I update the text property of the control from...
0
by: Sabari | last post by:
Hi, I have a DataGrid in my application and i use inline editing to add and update data.I also have a DropdownList in my edit command.When i update the datagrid it works well with windows XP but...
3
by: D. Shane Fowlkes | last post by:
I have a Datagrid which in theory, should allow you to edit and update the records. I've stripped my test page down so that it's only attempting to update one field - "description". Yet when I...
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
8
by: Strahimir Antoljak | last post by:
I bound a DataGrid to a table (I tried with a DataView too). I removed the row from the table, and then inserted a new row at the same row index. the table gets the row to the right position, but...
5
by: jason | last post by:
Hi, all How can I update data (multiple rows, but not every rows) using dataset in datagrid? I mean is there any way I can let datagrid know which row(s)/column(s) has been modified and update...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
0
by: Patty05 | last post by:
I have a datagrid on a form that update properly when form loads. When the program runs and I type in/add a new row in the datagrid, it does not save the changes. Any help would be greatly...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.