473,387 Members | 1,528 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 save all the filled rows in a datagrid

Hi everybody,I’v a question in a Vb.Net 2003 program,How can save or(insert) all the rows that I fill in a datagrid to be saved in itis datasource(table)at one time ,because my insertion code just save and insert the row where the curser is on it.So,IS there a solution.
With thanks, here is my code,
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.OleDb
  2. Public Class Form7
  3.     Inherits System.Windows.Forms.Form
  4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         Dim strcon As String
  6.         Try
  7.             strcon = " provider=microsoft.jet.oledb.4.0;data source=Cars.mdb "
  8.  
  9.             Dim con As New OleDbConnection(strcon)
  10.             con.Open()
  11.  
  12.             Dim adapter As New OleDbDataAdapter("select OrderNo,CarName,Model,Color,Quantity,OrderDate from  CompanyOrders", con)
  13.  
  14.             Dim dset As New DataSet
  15.  
  16.             adapter.Fill(dset, "CompanyOrders")
  17.  
  18.              dset.Clear()
  19.             DataGrid1.DataSource = dset.Tables("CompanyOrders")
  20.  
  21.         Catch exp As Exception
  22.             MsgBox(exp.ToString)
  23.         End Try
  24.     End Sub
  25.  
  26.  
  27.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  28.  
  29.         Dim strcon As String
  30.         Try
  31.             strcon = "provider=microsoft.jet.oledb.4.0;data source=Cars.mdb"
  32.             Dim con As New OleDbConnection(strcon)
  33.             con.Open()
  34.             Dim cmd As New OleDbCommand
  35.             cmd.CommandText = "insert into CompanyOrders(OrderNo,CarName,Model,Color,Quantity,OrderDate) values(" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0) & ",'" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 1) & "', " & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 2) & " , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) & "' , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 4) & "' , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 5) & "')"
  36.             cmd.Connection = con
  37.  
  38.  
  39.  
  40.             cmd.ExecuteNonQuery()
  41.             MessageBox.Show("The addition process done successfully")
  42.  
  43.             con.Close()
  44.         Catch exp As Exception
  45.             MsgBox(exp.ToString)
  46.         End Try
  47.     End Sub
  48.  
  49.  
  50.  
  51.  
  52. End Class
May 15 '06 #1
2 3184
Please help me,I donot know how to solve this problem.
O.K is there any different way I can use it ,i need a tool as datagrid has many rows to fill it with data as Company Orders(ord_num,ord_name,ord_qty,ord_date).
May 15 '06 #2
Banfa
9,065 Expert Mod 8TB
I am no VB expert but from a general programming point of view I would have thought that you need to replace the code line

Expand|Select|Wrap|Line Numbers
  1.  cmd.CommandText = "insert into CompanyOrders(OrderNo,CarName,Model,Color,Quantity  ,OrderDate) values(" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0) & ",'" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 1) & "', " & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 2) & " , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 3) & "' , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 4) & "' , '" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 5) & "')"
with a for loop (VB has for loops right?) which performs an insert for each row in the data grid and instead of DataGrid1.CurrentCell.RowNumber you use your loop control variable.
May 15 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Angie | last post by:
datagrid ex. a 1 checkbox b 2 checkbox c 3 checkbox once a user selects these 3 rows from a datagrid and i want to save the 1, 2, 3 value into the database how do i do this? I know...
0
by: chrisben | last post by:
Hi, I made a DataTable table and add four DataColumn to it. Then I created a DataView by calling dbv=new DataView (table). Then I insert some rows to the dataview and bind it to my datagrid as...
0
by: Bill Toulias | last post by:
I want to be able to display the tuples of a select query (which may change at runtime, so no prior knowledge for column characteristics) and edit them as well. I used this code, that executes...
2
by: VM | last post by:
I'm trying to dynamically display the rows in a datagrid while filling the datatable source. I'm doing it this way because we had already finished the app but this section sometimes took so long...
1
by: VM | last post by:
Initially, I had created my Windows application so that my datagrid's source would be updated once the method that filled the table was finished: private void btn_run_Click(object sender,...
2
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different...
2
by: tnks | last post by:
hi i have a data grid (i fill when i run the program) then the user insert rows and i wana save.. i used SqlClient.SqlCommandBuilder but it fail, just save 1 recordr, some times 2 records,...
6
by: alexia.bee | last post by:
Hi all, I am new to C# so please bear me :). I have an app which uses datagridview. it has 8 columns. I also have struct which has 8 members(the same as the datagridview). when I add row to...
4
by: nch1978 | last post by:
I am new to programming and am sorry if this post is in the wrong area. I have a listbox bound to a table and a datagrid bound to another one with a connection through xml. I can change the data...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.