473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rows cannot be programmaticall y added to the DataGridView's rows collection when the

2 New Member
Can anyone help me this?

I have a class.

Expand|Select|Wrap|Line Numbers
  1. Public Class db_Vehicle
  2.  
  3.     Public bs_VehicleDetails As New BindingSource()
  4.     Public da_VehicleDetails As New SqlDataAdapter()
  5.     Public table As New DataTable()
  6.  
  7.     Private dbsettings As cls_settings
  8.  
  9.     Public Sub New(ByRef mySettings As cls_settings)
  10.         Me.dbsettings = mySettings
  11.     End Sub
  12.  
  13.     Public Function GetVehicleDetails() As BindingSource
  14.  
  15.             'Dim ds As DataSet
  16.             Dim dsChanges As New DataSet
  17.  
  18.             ' Specify a connection string.
  19.             Dim dbcnn As SqlConnection = New SqlConnection(dbsettings.getDatabaseString)
  20.  
  21.             ' Create a new data adapter based on the specified query.
  22.             Me.da_VehicleDetails = New SqlDataAdapter("SELECT ServiceFlag FROM ServiceDates", dbcnn)
  23.  
  24.             ' Create a command builder to generate SQL update, insert, and
  25.             ' delete commands based on selectCommand. These are used to
  26.             ' update the database.
  27.             Dim commandBuilder As New SqlCommandBuilder(Me.da_VehicleDetails)
  28.  
  29.             ' Populate a new data table and bind it to the BindingSource.
  30.             table.Locale = System.Globalization.CultureInfo.InvariantCulture
  31.             Me.da_VehicleDetails.Fill(table)
  32.             Me.bs_VehicleDetails.DataSource = table
  33.  
  34.             'Newform.dgv_Vehicles.DataSource = bs_VehicleDetails
  35.             Me.da_VehicleDetails.Update(table)
  36.  
  37.  
  38.  
  39.         Return bs_VehicleDetails
  40.  
  41.     End Function
  42.  
  43. End Class

Then I call my class.

Expand|Select|Wrap|Line Numbers
  1. Public Sub Retrieve_SF()
  2.  
  3.         Dim imageNo As Image = System.Drawing.Image.FromFile("C:\Development\Image\Red.bmp")
  4.         Dim imageYes As Image = System.Drawing.Image.FromFile("C:\Development\Image\Green.bmp")
  5.  
  6.         Dim dgvICol As New DataGridViewImageColumn()
  7.         Dim Conn_SF As SqlClient.SqlConnection = New SqlClient.SqlConnection(dbsettings.getDatabaseString)
  8.         Dim command As SqlCommand = New SqlCommand("SELECT ServiceFlag FROM ServiceDates", Conn_SF)
  9.  
  10.         'Dim instance As New DataGridViewRowCollection
  11.         'Dim dataGridViewRow As New DataGridViewRow
  12.         'Dim returnValue As Integer
  13.  
  14.         Conn_SF.Open()
  15.  
  16.         dgvICol.HeaderText = "Service Flag"
  17.         dgvICol.Name = "ImageCol"
  18.  
  19.         Dim reader As SqlDataReader = command.ExecuteReader()
  20.  
  21.         dgv_Vehicles.Columns.Insert(0, dgvICol)
  22.         'dgv_Vehicles.AllowUserToAddRows = True
  23.  
  24.         Dim i As Integer = 0
  25.  
  26.         'This is to temporarily store the results of the reader.read method
  27.  
  28.         Dim strItem As String
  29.  
  30.         While reader.Read()
  31.             With dgv_Vehicles
  32.  
  33.                 '.EditMode.EditProgrammatically = DataGridViewEditMode.EditProgrammatically
  34.                 .EditMode = DataGridViewEditMode.EditProgrammatically
  35.  
  36.                 Dim rw As New DataGridViewRow
  37.                 'Dim dradd As New DataRow
  38.  
  39.                 rw.Cells.Add(New DataGridViewImageCell)
  40.                 strItem = reader.Item(0).ToString
  41.                 If reader.HasRows Then
  42.  
  43.                     i += 1
  44.  
  45.                     strItem = strItem.TrimEnd(" ")
  46.  
  47.                     If strItem = "Out" Then
  48.                         rw.Cells(0).Value = imageNo
  49.  
  50.                     ElseIf strItem = "In" Then
  51.                         rw.Cells(0).Value = imageYes
  52.  
  53.                     End If
  54.  
  55.                     '.DataSource = DBNull.Value
  56.                     'Vehicle.bs_VehicleDetails.Add(rw)
  57.                     .Rows.Add(rw) "Error here"
  58.                     'returnValue = instance.Add(dataGridViewRow)
  59.  
  60.                     .AutoResizeColumn(0)
  61.                     'This clears the variable
  62.  
  63.                     strItem = ""
  64.  
  65.                 End If
  66.             End With
  67.         End While
  68.  
  69.     End Sub
When I try & add my row I get the error. I've tried loads of different things but can't get it to work.
Feb 5 '08 #1
0 3320

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

Similar topics

1
4569
by: Mike Malter | last post by:
I take it that the only way to add rows to a DataList control is through DataBind? If not, how do I manually add rows to a DataList control? I have searched everywhere and can't find an example anywhere other than through building a DataTable, and then doing a DataBind. Thanks. Mike
3
4885
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
3
22153
by: Rain County | last post by:
I am programmatically building and populating a table and then making it the data source for a dataGridView. I want to add a column to the dataGridView which will be a DataGridViewComboBoxColumn. I wish to populate, by default, all of the ComboBoxes with the same list of three choices. How do I add the DataGridViewComboBoxColumn, and do I first populate one as a template for the others? I will appreciate any help.
2
6182
by: Ryan | last post by:
I want to programmatically add a row to DataGridView (a datasource was binded with it), but when I call the method DataGridView.Rows.Add, a System.InvalidOperationException will be thrown and the error message is: Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound. But if I create a DataGridView without any datasource binded, the method DataGridView.Rows.Add can work normally. I...
2
12577
by: shalakasan | last post by:
Hi, I am new to the forms world and have a silly doubt. I have form with a DataGridview and an "Add" button. When the user clicks on the add button, one row gets added to the datagrid. Now I want that when the user hits the "Add" button, then new row should be added and all the previous rows should be disabled for selection/editing. How can I disable previous rows for selection/editing?
2
11483
by: lord.zoltar | last post by:
I have a DataGridView that has potentially several hundred rows (possibly a thousand or two). I'd like for each row to have the proper height to accomadate the text in the row. I tried setting the AutoSizeRowMode to AllCells. This worked great on small datasets, but the program hung for a long time on larger datasets. I changed it to DisplayedCells, but this has some quirks: All of the initally displayed rows are the correct height (for...
6
19700
by: Bill Nguyen | last post by:
I tried almost everything ..Rows.clear() ..rowcount = 0 and the rows in a datagridview still not cleared I populated the Datagridview manually using Row.Add Any help is greatly appreciated Bill
6
8453
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview bound it to sql table Throw Bindingsource To fill it: MyTableTableAdaptor.fill(MyDataset.Mytable);
3
9689
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this cool little feature - it will not call DataTable.Rows.Add until after you leave the DataGridView row. This is cool because it lets your user edit the record as much as needed to get it into a valid state before actually adding it to the DataTable.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.