[RESOLVED] DataGridViewComboBox problem!
Hi
I have a DataGridView and am adding columns at runtime. One particular column is a ComboBox column. I have populated it but when I come to run the program, the ComboBoxes are in the relevant cells but don't drop down when you click on them! My code is below:
Code:
With DataGridView1
Dim Penalty As New DataGridViewComboBoxColumn
With Penalty
.Name = "Penalty"
.HeaderText = "Penalty"
.Width = 40
.Visible = True
.ReadOnly = False
.Items.Add("1")
.Items.Add("3")
.Items.Add("4")
.Items.Add("5")
.Items.Add("6")
.Items.Add("7")
.Items.Add("8")
.Items.Add("11")
End With
DataGridView1.Columns.Insert(6, Penalty)
End With
Any help would be great! Thanks :ehh:
Re: DataGridViewComboBox problem!
Me again! :D
Ok, firstly, don't nest With statements. I'm surprised that compiled, as .Name could a property of either. The following code (which is essentially the same) works on my computer:
Code:
Dim Penalty As New DataGridViewComboBoxColumn With {.Name = "Penalty", .ReadOnly = False, .HeaderText = "Penalty", .Width = 40, .Visible = True}
With Penalty.Items
.Add("1")
.Add("3")
.Add("4")
.Add("5")
.Add("6")
.Add("7")
.Add("8")
.Add("11")
End With
DataGridView1.Columns.Insert(6, Penalty)
Re: DataGridViewComboBox problem!
Why hello again - thanks for the help!
Code:
Dim Penalty As New DataGridViewComboBoxColumn with{.Name = "Penalty", .ReadOnly = False, .HeaderText = "Penalty", .Width = 40, .Visible = True}
didn't work - it had blue curly lines from the 'with' part onwards and the error said that End of statement was expected.
Re: DataGridViewComboBox problem!
You could try :
Code:
Dim Penalty As New DataGridViewComboBoxColumn
With Penalty : .Name = "Penalty" : .ReadOnly = False : .HeaderText = "Penalty" : .Width = 40 : .Visible = True : End With
You use the colon ( : ) to chain multiple statements onto one line.
Very Bad practice IMHO, though - and, doesn't address your actual problem ;)
Re: DataGridViewComboBox problem!
HanneSThEGreaT
Have you any idea why the values are not showing up in my ComboBoxes?
I have been looking over it for about 45mins now with no joy! :mad:
Re: DataGridViewComboBox problem!
I was curious...
Hi brjames32!
Well, I took your code exactly as it is on post#1, and it worked - just changed the column index. OK, my DataGridView was not bound to any datasource, but it works, as in the pic.
So, perhaps your problem is elsewhere ¿
Re: DataGridViewComboBox problem!
Yep - I thought it should work!
Right then - I shall continue to TRY to find out what is stopping the ComboBoxes populating. The boxes are there in each row but have nothing in them.
Surely the DataGridView being populated at runtime shouldn't stop this working?
Re: DataGridViewComboBox problem!
It might do if the program thinks that the column is bound at all. Are you populating manually or from a dataset?
Re: DataGridViewComboBox problem!
I am populating manually at runtime so it can't be that the column thinks it's already bound.
It's a strange one ain't it?
Re: DataGridViewComboBox problem!
I hooked up my dgv with a dataset, etc. And it still works :confused:
Is there any piece of code that can influence this ¿
1 Attachment(s)
Re: DataGridViewComboBox problem!
I have attached the whole project to this reply. I can't find out what it could be! would be greatful if any of you can! The part we are discussing is in:
Code:
Private Sub cmdShow_Click
section.
Thanks a lot ;)
Re: DataGridViewComboBox problem!
Without the database, it's kinda hard to run. I can spot anything wrong with that section, although with Option Strict, there a few warnings about (a couple of unused variables, which I think will get, and some functions that don't return values). Wish I could be of more help.
Re: DataGridViewComboBox problem!
Oh yeah :blush: forgot about the Database. It's 4.9 meg at the moment and contains sensitive student data as it's for a school admin system.
Will see if I can strip some stuff out of it and maybe password protect it too.
:)
Re: DataGridViewComboBox problem!
You could copy it, delete all the data, and type in some non-nonsensical stuff.
2 Attachment(s)
Re: DataGridViewComboBox problem!
WOW - Access Database .rar well - it shrank from 5.9MB to 436KB!!! Also, I have attached the spreadhseets that the info is saved to - there is no problem saving though.
Anyway, here it is attached to this post . . . . .
Re: DataGridViewComboBox problem!
Did anyone manage to take a look? I agree that the code is fine - tried it in another databound Datagrid of mine and it worked fine.
There's gotta be something preventing it either in the designer or in code!
Re: DataGridViewComboBox problem!
I have sorted it. All I did was change the Property Readonly from True to False in the Datagridview designer and now it works!
Surely that shouldn't have been affecting the ComboBoxColumn??
Re: DataGridViewComboBox problem!
That kind of makes sense - if the entire grid is read-only, then you won't be allowed to change the value of the combo, so it won't drop down.
Re: DataGridViewComboBox problem!
It's doing something else strange now - when I click save it says that:
Quote:
Parameter @7 has no default value
This is the column I was having trouble with before. I have tried both selecting values for each row in the column and also setting:
Code:
.DefaultCellStyle.NullValue = "0"
but it still comes up with that error. I have also tried:
Code:
DataGridView1.CommitEdit
but still nothing!
Re: DataGridViewComboBox problem!
LOL! It is always the small things that mean big headaches!
I knew it had to be something else :)
I just run your app now ( only started with it now ), and I got a Parameter @13 has no default value, in this section :
Code:
If ExcelYear = "10" Then
Dim MyExcelConnection As New System.Data.OleDb.OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\08-09\Year 10 2008-09.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=0""")
MyExcelConnection.Open()
With myInsertCommand
.CommandText = "Insert INTO [Sheet1$] ([StudentId],[StudentName],[ClassGrp],[TheDate],[Merits],[Demerits],[Penalty],[Lesson]," + _
"[TeachingGrp],[Staff],[Description],[Action],[MeritType]) VALUES(@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13)"
.CommandType = CommandType.Text
.Connection = MyExcelConnection
For Each Row As DataGridViewRow In DataGridView1.Rows
For Col As Int32 = 1 To Row.Cells.Count
.Parameters.Add(New OleDb.OleDbParameter("@" & Col, OleDb.OleDbType.Char)).Value = CStr(Row.Cells(Col - 1).Value)
Next Col
.ExecuteNonQuery()
.Parameters.Clear()
Next
End With
Parameter 13 in this case is Merits