VS2005 - DefaultValuesNeeded - bug? Adding default values in DataGridView

D

dbuchanan

Hello,

Is this a bug?
Is there some kind of work around?

I want to add default values for a few columns in my datagridview

I found the "DefaultValuesNeeded" event for the datagridview

I gave it a try using the example given in

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.defaultvaluesneeded.aspx

Here is my code
\\
Private Sub dgvDeviceTypes_DefaultValuesNeeded(ByVal sender As
Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs)
Handles dgvDeviceTypes.DefaultValuesNeeded
'enter default values into lkpDeviceTypes
With e.Row
.Cells("dtHide").Value = False
.Cells("dtOrd").Value = 0
.Cells("bMachType").Value = True

End With
End Sub
//

I get the following error;
-----------------------
Column named rEditedBy cannot be found.
Parameter name: columnName
-----------------------


Here is my table;

pkDeviceTypeId smallint
DeviceName varchar(20)
Prefix char(4)
bMachType bit
dtOrd tinyint
dtHide bit


Below is the text of the error - Notice that the code cannot find the
column name. It seems that it thinks the column name is "columnName";

System.ArgumentException was unhandled
Message="Column named rEditedBy cannot be found.
Parameter name: columnName"
ParamName="columnName"
Source="System.Windows.Forms"
StackTrace:
at
System.Windows.Forms.DataGridViewCellCollection.get_Item(String
columnName)
at
QmsUI.f080AdminSetup.dgvLaborCostCodes_DefaultValuesNeeded(Object
sender, DataGridViewRowEventArgs e) in D:\DBuchanan MyDocuments\Visual
Studio 2005\Projects\Qms_01\QmsUI\Form1.vb:line 287
at
System.Windows.Forms.DataGridView.OnDefaultValuesNeeded(DataGridViewRowEventArgs
e)
at
System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo
hti, Boolean isShiftDown, Boolean isControlDown)
at
System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs
e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs
e)
at System.Windows.Forms.Control.WmMouseDown(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at QmsUI.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Is this a bug?
Is there some kind of work around?

thank you,
dbuchanan
 
G

Gabriel Lozano-Morán

Are you sure that in dgvDeviceTypes_DefaultValuesNeeded you have no
..Cells("rEditBy").Value = ... ? The error message just indicates that the
argument "columnName" of the operation
"System.Windows.Forms.DataGridViewCellCollection.get_Item()" is not correct.

Gabriel Lozano-Morán
http://www.realdn.net
 
D

dbuchanan

Hi Gabriel,

I'm sorry!

I sent the code for the wrong table. Here it is again - the code and
error match this time right this time.

Here is the error message;
--------------------------
Column named rEditedBy cannot be found.
Parameter name: columnName
--------------------------


Here is the code where the error occurs
\\
Private Sub dgvLaborCostCodes_DefaultValuesNeeded(ByVal sender As
Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs)
Handles dgvLaborCostCodes.DefaultValuesNeeded
'enter default values into lkpLaborCostCodes
With e.Row
.Cells("rEditedBy").Value = My.User.Name '<< This
line
.Cells("rEditedOn").Value = Now
End With
End Sub
//


Here is the data table that is behind the dataGridView
\\
CREATE TABLE [lkpLaborCostCodeRate] (
[pkLaborCostCodeRateId] [smallint] IDENTITY (1, 1) NOT NULL ,
[LaborCostCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL ,
[HourlyRate] [smallmoney] NOT NULL ,
[rEditedOn] [smalldatetime] NOT NULL , --< I tried to give a
default value for this field
[rEditedBy] [smalldatetime] NOT NULL ,
CONSTRAINT [PK_lkpLaborCostCodeRates] PRIMARY KEY CLUSTERED
(
[pkLaborCostCodeRateId]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
//


Here is the error I get when I click into the datagrid and the
'DefaultValuesNeeded' event fires
\\
System.ArgumentException was unhandled
Message="Column named rEditedBy cannot be found.
Parameter name: columnName"
ParamName="columnName"
Source="System.Windows.Forms"
StackTrace:
at
System.Windows.Forms.DataGridViewCellCollection.get_Item(String
columnName)
at
QmsUI.f080AdminSetup.dgvLaborCostCodes_DefaultValuesNeeded(Object
sender, DataGridViewRowEventArgs e) in D:\DBuchanan MyDocuments\Visual
Studio 2005\Projects\Qms_01\QmsUI\AdminSetup.vb:line 286
at
System.Windows.Forms.DataGridView.OnDefaultValuesNeeded(DataGridViewRowEventArgs
e)
at
System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo
hti, Boolean isShiftDown, Boolean isControlDown)
at
System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs
e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs
e)
at System.Windows.Forms.Control.WmMouseDown(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at QmsUI.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
//


Is this a bug?
Is there some kind of work around?


dbuchanan
 
G

Guest

Hi There,

The same happened to me. I checked the datagridview properties at run time
(using the locals windows) and column names are all "" (an empty string?).

I solved it using the columns numbers instead of columns name.

i.e. use ".Cells(0).Value = False" instead of ".Cells("dtHide").Value = False"

I hope this helps,



--
Sergio Torres C.
(505) 897 2041
___________________
http://www.stcsys.com
___________________



dbuchanan said:
Hello,

Is this a bug?
Is there some kind of work around?

I want to add default values for a few columns in my datagridview

I found the "DefaultValuesNeeded" event for the datagridview

I gave it a try using the example given in

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.defaultvaluesneeded.aspx

Here is my code
\\
Private Sub dgvDeviceTypes_DefaultValuesNeeded(ByVal sender As
Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs)
Handles dgvDeviceTypes.DefaultValuesNeeded
'enter default values into lkpDeviceTypes
With e.Row
.Cells("dtHide").Value = False
.Cells("dtOrd").Value = 0
.Cells("bMachType").Value = True

End With
End Sub
//

I get the following error;
-----------------------
Column named rEditedBy cannot be found.
Parameter name: columnName
-----------------------


Here is my table;

pkDeviceTypeId smallint
DeviceName varchar(20)
Prefix char(4)
bMachType bit
dtOrd tinyint
dtHide bit


Below is the text of the error - Notice that the code cannot find the
column name. It seems that it thinks the column name is "columnName";

System.ArgumentException was unhandled
Message="Column named rEditedBy cannot be found.
Parameter name: columnName"
ParamName="columnName"
Source="System.Windows.Forms"
StackTrace:
at
System.Windows.Forms.DataGridViewCellCollection.get_Item(String
columnName)
at
QmsUI.f080AdminSetup.dgvLaborCostCodes_DefaultValuesNeeded(Object
sender, DataGridViewRowEventArgs e) in D:\DBuchanan MyDocuments\Visual
Studio 2005\Projects\Qms_01\QmsUI\Form1.vb:line 287
at
System.Windows.Forms.DataGridView.OnDefaultValuesNeeded(DataGridViewRowEventArgs
e)
at
System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo
hti, Boolean isShiftDown, Boolean isControlDown)
at
System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs
e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs
e)
at System.Windows.Forms.Control.WmMouseDown(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at QmsUI.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Is this a bug?
Is there some kind of work around?

thank you,
dbuchanan
 
D

dominic.ma

Hello,

To find the column name, select your datagridview and go into edit
columns. Pick a column on the left pane and check the (name) property
on the right pane. This is the name you need to use in the code and by
default it goes something like "columnnameDataGridViewTextBoxColumn". I
renamed each name to something shorter. ;)

Hope this helps,
Dom
 
D

dbuchanan

Dominic,

I can't get it to work. When I use this code as you instructed like
this..

\\
Private Sub dgvDeviceTypes_DefaultValuesNeeded(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles
dgvDeviceTypes.DefaultValuesNeeded
'enter default values into lkpDeviceTypes
With e.Row
'0' based, counting only displayed columns
.Cells("BMachTypeDataGridViewCheckBoxColumn").Value = 0
'bMachType
.Cells("DtHideDataGridViewCheckBoxColumn").Value = 0
'dtHide
End With
End Sub
//

....I get the following error
\\
System.IO.FileLoadException was unhandled
Message="The given assembly name or codebase was invalid. (Exception
from HRESULT: 0x80131047)"
Source="QmsUI"
StackTrace:
at QmsUI.f080AdminSetup.cboSelectTable_TextChanged(Object
sender, EventArgs e)
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at System.Windows.Forms.ComboBox.OnTextChanged(EventArgs e)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.ComboBox.set_Text(String value)
at System.Windows.Forms.ComboBox.UpdateText()
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr
wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr
hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at QmsUI.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
//


However, when I follow Sergio's suggestion (using the 0 based index of
displayed columns in the dgv) like this;
\\
Private Sub dgvDeviceTypes_DefaultValuesNeeded(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles
dgvDeviceTypes.DefaultValuesNeeded
'enter default values into lkpDeviceTypes
With e.Row
'0' based, counting only displayed columns
.Cells(2).Value = 0 'bMachType
.Cells(4).Value = 0 'dtHide
End With
End Sub
//

I get no error. and it works.

I would prefer following your suggestion if it worked, because then I
don't have to recount columns or change the code when I remove a column
from the dgv.

Please help me understand just what you mean.

Thank you,
dbuchanan
 
D

dbuchanan

Hi Dominic,

I appologize, - that error was unrelated to your solution.

I didn't understand it and it happened immediately after I tried your
solution.

Your method works! Thankyou for your input.

dbuchanan
 
J

jtsmith2

Was this issue ever fixed? I am having the same problem in my code. I
have a table and when I try to reference a column by its columnName it
throws the above error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top