473,507 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rows cannot be programmatically 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 3246

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

Similar topics

1
4548
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...
3
4856
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...
3
22127
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. ...
2
6162
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...
2
12561
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...
2
11469
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...
6
19647
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 ...
6
8418
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...
3
9667
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...
0
7223
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,...
0
7319
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,...
0
7376
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...
0
7485
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.