Multimobile Development: Building Applications for any Smartphone
ComboBox Databinding problems
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.windowsforms.databinding.


guoliang
GOOD ANSWER
When i try to assign a value ComboBox.SelectedValue, i get the follwing
message.

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.

Error will occur when all the following condition is true

1. call Datatable.Rows.InsertAt(newRow,0) AND
2. there is more than 10 rows in datatable AND
3. assign the ComboBox.SelectedValue to value which is belongs to second
record (which is next record of newly added record).

Anybody know why?

Sample code:

Private Sub LoadCombo()
Dim tbl As New DataTable()
Dim row As DataRow
Dim iCount As Integer

tbl.Columns.Add("id", GetType(Integer))
tbl.Columns.Add("name", GetType(String))

For iCount = 1 To 10 'change to 9 then you won't get error
row = tbl.NewRow()
row("id") = iCount
row("name") = iCount.ToString() + " - Number" + iCount.ToString()
tbl.Rows.Add(row)
tbl.AcceptChanges()
Next

row = tbl.NewRow()
row("id") = DBNull.Value
row("name") = "All"
tbl.Rows.InsertAt(row, 0)
tbl.AcceptChanges()

ComboBox1.ValueMember = "id"
ComboBox1.DisplayMember = "name"
ComboBox1.DataSource = tbl
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadCombo()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ComboBox1.SelectedValue = 1
End Sub

Reply to this message...
Vote that this is a GOOD answer... (5 votes from other users already)
 
 
    
Hussein Abuthuraya(MSFT)
GOOD ANSWER
Hi,

Use ComboBox1.SelectedIndex = 1 instead of ComboBox1.SelectedValue = 1 and the problem goes away.

Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

Reply to this message...
Vote that this is a GOOD answer... (4 votes from other users already)
 
 
    
Inge
GOOD ANSWER
HANG ON!

Yes, you are quite right that this works fine as long as the item is
inserted at the beginning of the datatable.

But it does mean that we have to use nasty clunky code like this:

if (theValue==0) {
ComboBox1.SelectedIndex = 0 //we use 0 for the empty item we added
} else {
ComboBox1.SelectedValue = theValue
}
---

But I would like an explanation as to WHY this doesn't work in the first
case... so please give us some background information!!
Is this a bug?

-Inge

"Hussein Abuthuraya(MSFT)" <Click here to reveal e-mail address> wrote in message
news:fUq5kUOvCHA.1340@cpmsftngxa09...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (7 votes from other users already)
 
 
    
Doug Lott
GOOD ANSWER
I get the same error on just one value (which happens to be 2nd in the list) in my combobox.

The combobox in question is bound to a datatable and later in the load event, the selectedvalue property is assigned a value from a custom object.

cboCurrentStatus.SelectedValue = _projectList.CurrentStatus

This works as long as the value is not the 2nd item in the collection. If it is the 2nd item, I get the "Specified argument was out of the range of valid values" error. On the other hand, if I replace the above code with:

For Each item As Object In Me.cboCurrentStatus.Items
If CType(item, DataRowView)(0).ToString() = projectList.CurrentStatus.ToString Then
Me.cboCurrentStatus.SelectedItem = item
End If
Next

it works just fine even when currentSatus is the 2nd item in the collection. Can anyone explain this?

--------------------------------
From: Doug Lott
Reply to this message...
Vote that this is a GOOD answer... (4 votes from other users already)
 
 
    
Arun Kumar
GOOD ANSWER
(Type your message here)

--------------------------------
From: Arun Kumar

It's strange but the following code works

        da.Fill(ds)
        ds.Tables(0).Rows.InsertAt(ds.Tables(0).NewRow, 0)
        Dim dt As DataTable = ds.Tables(0).Copy
        ComboBox1.DataSource = dt

So, just copying your dataset/datatable to another datatable and binding that to your combobox will not give this error. Must be a bug!

Arun
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Thomas Brabender
GOOD ANSWER
Hi Doug,

I was having similar problems until I copied the table into a new table (code below). That seemed to fix the problem so perhaps it has something to do with the inserted value not being committed propertly?

private DataTable AddDefaultComboValue(DataTable dt)
{
    DataRow dr    = dt.NewRow();
    dr[0]        = "0";
    dr[1]        = "Select...";
    dt.Rows.InsertAt(dr, 0);

    System.Data.DataTable m_dt = dt.Copy();
    
    return m_dt;
}        

--------------------------------
From: Thomas Brabender
Reply to this message...
Vote that this is a GOOD answer... (4 votes from other users already)
 
 
    
Vlad E
GOOD ANSWER
This is crazy but this fixed the problem for me, use the COPY() method on the dataset (see code below).
Here is the thread in Spanish! "Perfectamente" is the key word!!! :)

http://groups-beta.google.com/group/microsoft.public.es.dotnet.vb/browse_thread/thread/c1deaa1b857bb799/bca7ac3290f758f8?q=.SelectedValue+2147483648+.NET#bca7ac3290f758f8

-------------
DataTable tbl = ds.Tables[0];
DataRow newRow = tbl.NewRow();            
tbl.Rows.InsertAt(newRow, 0);
            
cbxBox.DisplayMember = dataTextField;
cbxBox.ValueMember = dataValueField;
cbxBox.DataSource = ds.Tables[0].Copy();
--------------------------------
From: Vlad E
Reply to this message...
Vote that this is a GOOD answer... (5 votes from other users already)
 
 
 
System.ArgumentOutOfRangeException
System.Data.DataRow
System.Data.DataRowView
System.Data.DataTable
System.DBNull
System.EventArgs
System.Object
System.Windows.Forms.ComboBox




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734