Click here to Skip to main content
15,909,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am working on user rights. I want to load the grid items checked using following code.

Dim l As Integer = 0, vrGridName As New DataGridView, vrGridItemIndex As Integer
       taSaveTemplates.Connection.ConnectionString += ";password=" & vrSAPWD
       Me.taSaveTemplates.Fill(Me.DsSaveTemplates.tblTemplates, lstTemplateID.Text)
       'Load Grids according to data saved
       Do While DsSaveTemplates.tblTemplates.Rows.Count > l
           vrGridName.Name = DsSaveTemplates.tblTemplates.Rows(l).Item("GridName")
           vrGridItemIndex = DsSaveTemplates.tblTemplates.Rows(l).Item("GridItemIndex")
           vrGridName.Item(0, vrGridItemIndex).Value = True
           l = l + 1
       Loop


vrGridName stores the name of grid selected from DB and vrGridItemIndex stores the item that needs to be checked.

The problem is, when I run the code, it says Index is our of range.
I have checked, the vrGridName does not store the name of grid but stores
System.windows.datagridview

Please advise.
Thanks
Posted

Well...there are a huge number of things wrong here, but lets concentrate on the fundamentals...
This approach won't work.

The Name property of a DataGridView is a string, nothing more. It does not transfer any other information, and it certainly doesn't make a DataGridView refer to an existing version with data in it - so when you try to access items, there aren't any, because the DataGridView you are accessing is not the same one as the existing one full of data, it just has the same Name property.

Now, I'm not sure exactly what you are trying to do, but I suspect that you are trying to store a whole DataGridView into your database by doing something along the lines of:
VB
Dim sql As String = "INSERT INTO MyTable (MyColumn) VALUES ('" + myDataGridView + "')"
Which won;t work, even slightly, because all it does it store the name of the class into the database:
System.Windows.DataGridView
not the content.

You need to sit down and rethink the whole of this project: because I suspect that everything need to be redesigned, from the database on up!
 
Share this answer
 
Comments
hypermellow 6-Jun-14 9:16am    
Good advice that's worthy of a 5*
The GridViewName can be retrieved from your vrGridName variable by access it's ID property.

VB
Dim sGridName As String = vrGridName.ID


Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900