Add a blank item in a data binded combo, how?

K

Kay

Hi all,

Could you tell me the best way to add a blank item(as first item) in a data
binded combo box? Because I think I didn't do it right and it generate an
error if the second item(after the top blank item) is selected by using
Combo.SelectedValue :

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

Could you guys please comment? Here's my code:

/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection

myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()

theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")

tblTable = dsDataSet.Tables("Table")

''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****

dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////

Thanks!

Kay
 
N

NuTcAsE

You forgot to set the row's KeyField column to 0. It looks like that
the new data row's KeyField column is set to a int.MinValue which is
-2147483648.

- NuTcAsE
 
K

Kay

Hi NutcAsE & all,

Thanks for your advice! However, I've tried adding the KeyField without
success:

myRow = tblTable.NewRow
myRow("DisplayField") = ""
myRow("KeyField") = 0 '**** tried 999 & "0"
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****** error triggered here
dsDataSet = Nothing
tblTable = Nothing
myCCMSConn.Close()
gbProcessing = False


With the new line of code, it trigger the same error in THIS procedure, and
it's actually the line the set the selected item - which is the same as the
scenario I have before (i.e. where I have combo.SelectedValue = xx <---
second item).

Is it a .net bug? Or there is a way to get around it? Your advice would be
very appreciated!!

Thanks!

Kay
 
B

Bart Mermuys

Hi,

Kay said:
Hi NutcAsE & all,

Thanks for your advice! However, I've tried adding the KeyField without
success:

myRow = tblTable.NewRow
myRow("DisplayField") = ""
myRow("KeyField") = 0 '**** tried 999 & "0"
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****** error triggered here
dsDataSet = Nothing
tblTable = Nothing
myCCMSConn.Close()
gbProcessing = False


With the new line of code, it trigger the same error in THIS procedure,
and it's actually the line the set the selected item - which is the same
as the scenario I have before (i.e. where I have combo.SelectedValue = xx
<--- second item).

Is it a .net bug?

Yes. When you bind a DataTable to a ComboBox then internally a DataView is
used. The DataView crashes when it's not sorted and you used InsertAt on
the linked DataTable.
Or there is a way to get around it?

Not that i know. Maybe you can get away with not using InsertAt (Add
instead) and creating an explicit DataView and sorting it so that the empty
row is on top.
Your advice would be very appreciated!!

I don't have a good workaround, i just wanted to let you know there is a
bug.

HTH
Greetings
 
N

NuTcAsE

Actually the data table is being bound after the InsertAt function, so
the resulting data view should contain the added data row. Weird part
is it is working perfectly for me. I am guessing that there is
something wrong with the data being returned in the data table. Key,
could you take a look at the data being returned from your fill
operation and check if any Null values or suspicious data is being
returned?

Also are you using 1.1 and if yes have you applied the SP1 for .net
1.1?

- NuTcAsE
 
B

Bart Mermuys

Hi,

NuTcAsE said:
Actually the data table is being bound after the InsertAt function, so
the resulting data view should contain the added data row.

True. But it's not that the row isn't added, but that there is a bug when
you use InsertAt with an unsorted DataView (also when InsertAt is called
before creating the DataView).

I too can't reproduce the problem with a simple ComboBox and SelectedValue.
The example below doesn't prove that the bug is the OP's problem, but it
does show that there is a bug with DataView and DataTable.InsertAt. I'm
sure there are other situations where it fails too, causing different
Exceptions or bad data:

DataTable dt = new DataTable("test");
dt.Columns.Add("desc", typeof(string));
dt.Rows.Add(new object[] { "A" });
dt.Rows.Add(new object[] { "B" });
dt.AcceptChanges();

DataRow dr = dt.NewRow();
dr["desc"] = "empty";
dt.Rows.InsertAt(dr, 0);

DataView dv = new DataView(dt);
//dv[1]["desc"] = "somethingelse"; // enumerating dv shows an additional row
dv[0]["desc"] = "somethingelse"; // enumerating dv crashes

// enumerating DataView
foreach( DataRowView drv in dv )
Console.WriteLine( drv["desc"] );

This happens on NET1.1SP2.

Greetings
 
N

NuTcAsE

You're right... there are reports out there with simmilar bugs using
InsertAt and databinding. Funny part is I can't find any offical
notification from Microsoft that this bug exists.


Thanks,

- NuTcAsE
 
G

Guest

Kay, another option is to pass a Union statement in your SQL.

SELECT 0 AS KeyField, Null AS DisplayField
UNION
SELECT KeyField, DisplayField FROM MyTable
ORDER BY DisplayField

and then use
ListBox.Items.FindByText(DefaultText).Selected = True
or
ListBox.Items.FindByValue(0).Selected = True
to find the info you want displayed.
 
K

Kay

Hi all,

Now I modified the code and bind the combo box with a Dataview, it seems the
error is gone!!! :D
NuTsAsE I have 1.1 SP1 install so I think the error may related to binding
the combo with data table. Ok here's the update of my code, comments are
welcome!

///////////////////

theAdapter = New SqlDataAdapter(pSQL, myCCMSConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")
tblTable = dsDataSet.Tables("Table")

Dim DV As DataView = dsDataSet.Tables("Table").DefaultView
'*****

''Put a blank row at the top
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)
DV.Sort = "DisplayField" '*****

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = DV '*****
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0
///////////////////

Kay
 
B

Bart Mermuys

Hi,

Kay said:
Hi all,

Now I modified the code and bind the combo box with a Dataview, it seems
the error is gone!!! :D

Even when you bind to a DataTable, the DataTable's DefaultView is used, so i
doubt that what's helping but it's the sorting that probely helps, as i
mentioned in my previous post the InsertAt-bug doesn't appear when the
DataView is sorted.

Greetings
 
Top