|
| 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
|
|
|
| |
|
|
| |
| |
| 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.
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
|
|
| |
| 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
|
|
|
| |
|
|
| |
| |
| 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
|
|
|
| |
|
|
| |
|
| |
| 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
|
|
|
| |
|
|
| |
|
|
| | |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|