473,386 Members | 1,679 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,386 software developers and data experts.

how to add two value in combobox using datagrid?

i have successfully added a combobox to my datagrid by setting their
datasource from one of my table. here's my code...

Dim grdColStyle6 As New DataGridComboBoxColumn()
With grdColStyle6
.MappingName = "MajorID" 'must be from the grid
table...
.HeaderText = "Major"
.Width = 120
.ColumnComboBox.DataSource =
DsStudentCourse1.Tables("Major").DefaultView 'dv;
.ColumnComboBox.DisplayMember = "Major"
.ColumnComboBox.ValueMember = "MajorID"

grdTableStyle1.PreferredRowHeight =
..ColumnComboBox.Height + 2
End With

my problem is that i need to add a combobox that contains only two
value called "1st" and "2nd" that when i click the combobox the value
"1st" and "2nd" will appear. so this value is not taken from a table.
i want this only to put in my code...

i tried this code...

With grdColStyle4
.MappingName = "Year" 'must be from the grid table...
.HeaderText = "Year"
.Width = 75

.ColumnComboBox.Items.Add("1st")
.ColumnComboBox.Items.Add("2nd")
.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDown
grdTableStyle1.PreferredRowHeight =
..ColumnComboBox.Height + 2
End With

but it gives me an error...

An unhandled exception of type 'System.NullReferenceException'
occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.
any help is greatly appreciated...
Nov 20 '05 #1
5 2819
Hi,

If you only want the user to be able to select 1st or 2nd the
combobox drop down style should be set to dropdownlist. As for the
'System.NullReferenceException' you probably have an error in the combobox
column. Post the code for the datagridcomboboxcolumn.

Ken
---------------
"jaYPee" <hi******@yahoo.com> wrote in message
news:nf********************************@4ax.com...
i have successfully added a combobox to my datagrid by setting their
datasource from one of my table. here's my code...

Dim grdColStyle6 As New DataGridComboBoxColumn()
With grdColStyle6
.MappingName = "MajorID" 'must be from the grid
table...
.HeaderText = "Major"
.Width = 120
.ColumnComboBox.DataSource =
DsStudentCourse1.Tables("Major").DefaultView 'dv;
.ColumnComboBox.DisplayMember = "Major"
.ColumnComboBox.ValueMember = "MajorID"

grdTableStyle1.PreferredRowHeight =
.ColumnComboBox.Height + 2
End With

my problem is that i need to add a combobox that contains only two
value called "1st" and "2nd" that when i click the combobox the value
"1st" and "2nd" will appear. so this value is not taken from a table.
i want this only to put in my code...

i tried this code...

With grdColStyle4
.MappingName = "Year" 'must be from the grid table...
.HeaderText = "Year"
.Width = 75

.ColumnComboBox.Items.Add("1st")
.ColumnComboBox.Items.Add("2nd")
.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDown
grdTableStyle1.PreferredRowHeight =
.ColumnComboBox.Height + 2
End With

but it gives me an error...

An unhandled exception of type 'System.NullReferenceException'
occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.
any help is greatly appreciated...

Nov 20 '05 #2
thanks for the reply. here is the code of datagridcomboboxcolumn

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms

Namespace DataGridTextBoxCombo
' Step 1. Derive a custom column style from DataGridTextBoxColumn
' a) add a ComboBox member
' b) track when the combobox has focus in Enter and Leave events
' c) override Edit to allow the ComboBox to replace the TextBox
' d) override Commit to save the changed data
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public ColumnComboBox As NoKeyUpCombo
Private _source As System.Windows.Forms.CurrencyManager
Private _rowNum As Integer
Private _isEditing As Boolean
Public Shared _RowCount As Integer
Public Sub New()
_source = Nothing
_isEditing = False
_RowCount = -1

ColumnComboBox = New NoKeyUpCombo()
ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList

AddHandler ColumnComboBox.Leave, AddressOf LeaveComboBox
AddHandler ColumnComboBox.SelectionChangeCommitted,
AddressOf ComboStartEditing
End Sub 'New

Private Sub HandleScroll(ByVal sender As Object, ByVal e As
EventArgs)
If ColumnComboBox.Visible Then
ColumnComboBox.Hide()
End If
End Sub 'HandleScroll

Private Sub ComboStartEditing(ByVal sender As Object, ByVal e
As EventArgs)
_isEditing = True
MyBase.ColumnStartedEditing(sender)
End Sub 'ComboMadeCurrent
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As
EventArgs)
If _isEditing Then
SetColumnValueAtRow(_source, _rowNum,
ColumnComboBox.Text)
_isEditing = False
Invalidate()

End If
ColumnComboBox.Hide()
AddHandler Me.DataGridTableStyle.DataGrid.Scroll, New
EventHandler(AddressOf HandleScroll)
End Sub 'LeaveComboBox
Protected Overloads Overrides Sub Edit(ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText As String, ByVal cellIsVisible As Boolean)

MyBase.Edit([source], rowNum, bounds, [readOnly],
instantText, cellIsVisible)

_rowNum = rowNum
_source = [source]

ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Size.Height)
ColumnComboBox.SelectedIndex =
ColumnComboBox.FindStringExact(Me.TextBox.Text)
ColumnComboBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
ColumnComboBox.Visible = True
AddHandler Me.DataGridTableStyle.DataGrid.Scroll,
AddressOf HandleScroll

ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub 'Edit
Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As
Boolean

If _isEditing Then
_isEditing = False
SetColumnValueAtRow(dataSource, rowNum,
ColumnComboBox.Text)
End If
Return True
End Function 'Commit
Protected Overrides Sub ConcedeFocus()
Console.WriteLine("ConcedeFocus")
MyBase.ConcedeFocus()
End Sub 'ConcedeFocus

Protected Overrides Function GetColumnValueAtRow(ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Object

Dim s As Object = MyBase.GetColumnValueAtRow([source],
rowNum)
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource,
DataView)
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your dataview
'& use binary search instead of this linear one
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.ValueMember)
If (Not s1 Is DBNull.Value) AndAlso _
(Not s Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While

If i < rowCount Then
Return dv(i)(Me.ColumnComboBox.DisplayMember)
End If
Return DBNull.Value
End Function 'GetColumnValueAtRow
Protected Overrides Sub SetColumnValueAtRow(ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
value As Object)
Dim s As Object = value

Dim dv As DataView = CType(Me.ColumnComboBox.DataSource,
DataView)
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your dataview
'& use binary search instead of this linear one
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.DisplayMember)
If (Not s1 Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While
If i < rowCount Then
s = dv(i)(Me.ColumnComboBox.ValueMember)
Else
s = DBNull.Value
End If
MyBase.SetColumnValueAtRow([source], rowNum, s)
End Sub 'SetColumnValueAtRow
End Class 'DataGridComboBoxColumn
End Namespace

Nov 20 '05 #3
Hi,

If the combobox isn't bound to a dataview this GetColumnValueAtRow
will fail. Try something like this.
Protected Overrides Function GetColumnValueAtRow(ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Object

Dim s As Object = MyBase.GetColumnValueAtRow([source],
rowNum)
Try
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource,
DataView)
catch
return s
end try
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your dataview
'& use binary search instead of this linear one
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.ValueMember)
If (Not s1 Is DBNull.Value) AndAlso _
(Not s Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While

If i < rowCount Then
Return dv(i)(Me.ColumnComboBox.DisplayMember)
End If
Return DBNull.Value
End Function 'GetColumnValueAtRow

Ken
-----------------------
Nov 20 '05 #4
thanks you once again for the reply. i'll try your code tomorow..

thanks once again...

jaYPee
On Sat, 24 Apr 2004 06:38:14 -0400, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:
Hi,

If the combobox isn't bound to a dataview this GetColumnValueAtRow
will fail. Try something like this.
Protected Overrides Function GetColumnValueAtRow(ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Object

Dim s As Object = MyBase.GetColumnValueAtRow([source],
rowNum)
Try
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource,
DataView)
catch
return s
end try
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your dataview
'& use binary search instead of this linear one
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.ValueMember)
If (Not s1 Is DBNull.Value) AndAlso _
(Not s Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While

If i < rowCount Then
Return dv(i)(Me.ColumnComboBox.DisplayMember)
End If
Return DBNull.Value
End Function 'GetColumnValueAtRow

Ken
-----------------------


Nov 20 '05 #5
i tried your code but i get same error...this is really because the
combobox is not bound to a dataview or is not a lookup table

On Sat, 24 Apr 2004 06:38:14 -0400, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:
Hi,

If the combobox isn't bound to a dataview this GetColumnValueAtRow
will fail. Try something like this.
Protected Overrides Function GetColumnValueAtRow(ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Object

Dim s As Object = MyBase.GetColumnValueAtRow([source],
rowNum)
Try
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource,
DataView)
catch
return s
end try
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your dataview
'& use binary search instead of this linear one
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.ValueMember)
If (Not s1 Is DBNull.Value) AndAlso _
(Not s Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While

If i < rowCount Then
Return dv(i)(Me.ColumnComboBox.DisplayMember)
End If
Return DBNull.Value
End Function 'GetColumnValueAtRow

Ken
-----------------------


Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard Hallgren | last post by:
Hi, In Windows Forms the usual approach to add a combobox in a datagrid involves adding a single combobox to the DataGrid.Controls, and then selectively displaying it as needed when a...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
2
by: Robert | last post by:
I'm sure this is a fairly basic question, but I've been looking all over the web for days for suggestions on how to do this. I've got a datagrid that's bound to a dataset on my form. It includes...
0
by: Johann Blake | last post by:
I have a dataset that I bind to two different datagrids. The dataset contains 2 tables. One is the main table used to store what I call session data which includes a Country field where the ID of a...
1
by: Peter W Johnson | last post by:
Hi Guys, I have a form setup with a Datagrid. The Datagrid has a combobox in one column. I am trying to find out the value of the combobox after it has been changed. I have added a handler...
0
by: zhuang | last post by:
Hi, Adding combobox to datagrid has been posted many times. I have a datagrid which has multiple combobox columns and normal textbox columns. But how could I change other combo box values at...
2
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
1
by: sathyan8294 | last post by:
i am using dotnet 2003.i am doing my project in vb.net windows application.i have two form in my project.first form is datagrid .In second form label,textboxes and combobox are avilable to save the...
0
by: mansoorali | last post by:
hi, am using hittest class in parent form for datagrid and 1 value to be retreived in child form for combobox when i click grid in parent form, am getting the 0 index value by default perhaps i want...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.