Adding Null Value to ComboBox bound to ArrayList - c#

I've seen a few similar questions to this issue I'm having, but they all don't seem to work out.
I currently have a method that populates an arraylist with items from a database query, and then returns the arraylist to be used to populate a ComboBox. The ComboBox will be used as a search filter for additional database queries.
I'd like to add a null value to the arraylist in order to allow for the user to "turn the filter off" when the null value is selected.
The issue comes up when adding the null value to the array list. When I add the null value before the database items are added, all of the items don't show up in the actual ComboBox. When I add the null value after the ArrayList has been populated, the null value never shows up as a row in the ComboBox, but everything else is there.
public static ArrayList DropDown()
{
ArrayList status = new ArrayList();
connect.Open();
// Finds the stuff to use in the COMBO BOX
cmd.CommandText = String.Format("query");
reader = cmd.ExecuteReader();
while (reader.Read())
{
status.Add(String.Format("{0}", reader[BANK_ID]));
}
string holder = null;
status.Add(holder);
connect.Close();
return status;
}
This results in the populated ComboBox, but no null row.
public static ArrayList DropDown()
{
ArrayList status = new ArrayList();
string holder = null;
status.Add(holder);
connect.Open();
// Finds the stuff to use in the COMBO BOX
cmd.CommandText = String.Format("query");
reader = cmd.ExecuteReader();
;
while (reader.Read())
{
status.Add(String.Format("{0}", reader[BANK_ID]));
}
connect.Close();
return status;
}
This results in nothing in the combo box.
Thanks for the help!

The items collection of a combobox can not contain null. Refer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx
I assume setting DataSource is internally checking for this and hence the behavior that you have noted (probably a more informed person can confirm this)
I suggest you add String.Empty instead and you can get rid of arraylist all together and use a string array/list instead.
comboBox.Items.Insert(0, string.Empty);

Related

Populating a ComboBox using list in C#

I have a code that reads the information from the database and assigns it to the list
class YaziciBilgiler
{
public string yaziciAdi;
public List<YaziciBilgiler> YaziciSec()
{
con.Open();
OracleCommand cmd = new OracleCommand("SELECT PRINTER_NAME FROM LABEL_PRINTER ");
DataSet dataset = new DataSet();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataset);
}
var yazici=dataset.Tables[0].AsEnumerable().Select(datarow=>new YaziciBilgiler
{
yaziciAdi = datarow.Field<string>("PRINTER_NAME")
}).ToList();
con.Close();
return yazici;
}
}
}
Then call the information in the printer list from the form
List<YaziciBilgiler>yazici;
yazici=yb.YaziciSec();
foreach (var item in yazici)
{
cbYazici.Items.Add(item);
}
but instead of the contents of the list, the location is as follows:
The basic problem with your code is that you have created a list full of objects with type YaziciBilgiler with a single string member called yaziciAdi
Combo doesn't know how to display this, it doesn't know to get the yaziciAdi property and show it, and you haven't told it so the only thing it can do when faced with an unknown object is call ToString() on it to get a string and display that string
Because you haven't overridden ToString it performs its default action which is to report the name of the class. This means your combo is full of repeated words of YaziciBilgiler
You have a few options:
override ToString so that it returns the yaziciAdi property
make your code that adds the items inside the loop add the yaziciAdi property instead of the whole object: cbYazici.Items.Add(item.yaziciAdi);
change the yaziciAdi member to be a property instead. bind the combobox to the list, or to the datatable- the discussion for this is in the comments to the question. I don't use WPF but reading around, I think the code might be something like:
cbYazici.SetBinding(
ItemsControl.ItemsSourceProperty,
new Binding { Source = yazici });
cbYazici.DisplayMemberPath = "yaziciAdi";
Or use an equivalent in the XAML (if the codebehind can't set these; I've no idea whether it can or not, and any experienced WPF guys feel free to edit this answer if I've got this wrong)
Note: to bind property yaziciAdi MUST be a property, not a field
You can try this :
cbYazici.Items.Add(item.yaziciAdi)

Map enum values on ComboBox

I want to use a C# Windows Forms combo box to select the value of an enum:
this.comboBoxColor.DataSource = System.Enum.GetValues(typeof(System.Drawing.KnownColor));
But when I put this in the InitializeComponent, it replaces this line with a static assignment of an array with all items in the enum. It does this twice, once for the Datasource, and once for the Items property.
But these things don't work together. When having a bound DataSource, adding items to the Items list causes an error and when doin it the other way around assigning the SelectedValue property doesn't work any more.
I've tried using a separate method to do this outside the InitializeComponent method. And simply setting the DataSource as above in the separate method produces the following error:
System.InvalidOperationException: 'Can't set the SelectedValue in a ListControl with an empty ValueMember.'
Edit: Microsoft says using a simple array as a data source should be possible: https://msdn.microsoft.com/nl-nl/library/x8160f6f(v=vs.110).aspx
It's possible to specify the data source in the designer, but there it only allows selecting classes. What must a class implement to make that work?
You can write a simple method that transforms your enum to a datatable and then use the result of the method as a DataSource with a pair of well known names for the ValueMember and DisplayMember properties of the combo
public DataTable CreateTableFromEnum(Type t)
{
DataTable dt = new DataTable();
if (t.IsEnum)
{
dt.Columns.Add("key", t);
dt.Columns.Add("text", typeof(string));
foreach(var v in Enum.GetValues(t))
dt.Rows.Add(v, v.ToString());
}
return dt;
}
and call it with
var colors = CreateTableFromEnum(typeof(KnownColor));
cbo.ValueMember = "key";
cbo.DisplayMember = "text";
cbo.DataSource = colors;
Now when you look at the selected value you will get the numeric value of the color selected

how to display list<> in drop down list?

I have a dropdownlist where I want to display a list of users. To call the users I use ChatUserDetails.GetPXPUsers()
Which brings me to this code:
public static List<ChatUserDetails> GetPXPUsers()
{
List<ChatUserDetails> Users = new List<ChatUserDetails>();
string SQL = SelectPXPUsers;
DataTable dtMainItems = ChatUserDetails.CustomFill(SQL, null);
foreach (DataRow dr in dtMainItems.Rows)
{
Users.Add(new ChatUserDetails(dr));
}
return Users;
}
But how to I display this list of users in my dropdownlist?
<asp:DropDownList runat="server" ID="DropDownListPXPUsers"></asp:DropDownList>
You can bind your list to your drop down at runtime by using the following code. You will need to specify which properties of the object are to be used.
DropDownListPXPUsers.DataSource = GetPXPUsers();
DropDownListPXPUsers.DateTextField = "PropertyOne"; // name of 'ChatUserDetails' property
DropDownListPXPUsers.DataValueField = "PropertyTwo"; // name of 'ChatUserDetails' property
DropDownListPXPUsers.DataBind();
Read more: See Examples in the DropDownList documentation.
First you'll need to set the DataSource for the DropDownList and then you will need to call DataBind().
if(!IsPostBack)
{
DropDownListPXPUsers.DataSource = GetPXPUsers();
DropDownListPXPUsers.DataBind();
}

Add 2 or more field to value member of C# coding

I have table with 4 primary key fields. I load that in to drop down list in my WinForm application created by using C#.
On the TextChanged event of drop down list I have certain TextBox and I want to fill the information recived by the table for the certain field I selected by the drop down list.
So as I say the table having 4 fields. Can I get those all 4 fields into value member from the data set, or could you please tell me whether is that not possible?
Thank you.
Datatable dt=dba.getName();
cmb_name.ValueMember="id";
cmb_name.DisplayMember="name";
cmb_name.DataSource=dt;
this is normal format.. but i have more key fields.. so i need to add more key fields..
You can use DataSource property to bind your source data to the ComboBox (e.g. a List of Entities, or a DataTable, etc), and then set the DisplayMember property of the ComboBox to the (string) name of the field you want to display.
After the user has selected an Item, you can then cast the SelectedItem back to the original row data type (Entity, DataRow, etc - it will still be the same type as you put in), and then you can retrieve your 4 composite keys to the original item.
This way you avoid the SelectedValue problem entirely.
Edit:
Populate as follows:
cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;
// Ignore ValueMember and Selected Value entirely
When you want to retrieve the selected item
var selectedRow = (cmb_name.SelectedItem as DataRowView );
Now you can retrieve the 4 values of your PK, e.g. selectedRow["field1"], selectedRow["field2"], selectedRow["field3"] etc
If however you mean that you want to DISPLAY 4 columns to the user (i.e. nothing to do with your Table Key), then see here How do I bind a ComboBox so the displaymember is concat of 2 fields of source datatable?
cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;
DataRowView selectedRow = (cmb_name.SelectedItem as DataRowView );
The result will be here:
MessageBox.Show(selectedRow.Row[0].ToString());
MessageBox.Show(selectedRow.Row[1].ToString());
MessageBox.Show(selectedRow.Row[2].ToString());
MessageBox.Show(selectedRow.Row[3].ToString());
.....
If you want to get some data from a ComboBox in to a List you can use something like this
List<string> ListOfComboData = new List<string>();
ListOfComboData = yourComboBox.Items.OfType<string>().ToList<string>();
I have no real idea if this is what you mean as the question is very poorly structured. I hope this helps...
Edit: To put the selected text in to some TextBox use
yourTextBox.Text = youComboBox.Text;
in the SelectedIndexChanged event of your ComboBox.
You could follow the approach here with the following class:
public class ComboBoxItem
{
public string Text { get; set; }
public object[] PrimaryKey { get; set; }
}
private void Test()
{
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.PrimaryKey = new object[] { primaryKey1, primaryKey2, primaryKey3, primaryKey4};
comboBox1.Items.Add(item);
comboBox1.SelectedIndex = 0;
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}

How do I load a list via a data set from SQL Server into ListView?

I have what seems to be a simple question but its killing me trying to find out.
I have a form in which I have a ListView. In this ListView I would like to populate it with data from a SQL Server 2008 database table.
public void LoadList()
{
DataTable dtable = budget_MainDataSetReceipt.Tables["Receipt"];
listView1.Items.Clear();
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
if (drow.RowState != DataRowState.Deleted)
{
ListViewItem lvi = new ListViewItem(drow["ReceiptID"].ToString());
lvi.SubItems.Add(drow["DateCleared"].ToString());
lvi.SubItems.Add(drow["CategoryID"].ToString());
lvi.SubItems.Add(drow["Amount"].ToString());
lvi.SubItems.Add(drow["Store"].ToString());
lvi.SubItems.Add(drow["DateEntered"].ToString());
listView1.Items.Add(lvi);
}
}
}
I keep getting an
Object reference not set to an instance of an object
error, and I can't figure out why. There are about 5 rows of data in my database, so in my mind, there should be 5 rows of data within the list view.
Can anyone tell me what I am missing? I can post more code if that would be helpful.
I have tried calling the LoadList() method in several ways:
Before the method itself
With the InitializeComponent() method
I have tried the following syntax
this.LoadList();
this.Form1.LoadList();`
I have also tried to initialize the DataTables type with the following:
DataTables dt = new DataTables //did not work
My hunch would be: you're assuming for all columns in your DataRow that they're present and not null - that's a bit of a dangerous assumption.
I would change your assignments to use a method that checks for DBNull before returning the string:
public string SafeGetString(DataRow row, string columnName)
{
if(row[columnName] != null && row[columnName] != DBNull.Value)
{
return row[ColumName].ToString();
}
return string.Empty;
}
so your could would look like:
ListViewItem lvi = new ListViewItem(SafeGetString(drow, "ReceiptID"));
lvi.SubItems.Add(SafeGetString(drow, "DateCleared"));
// and so forth
This way, if any of the columns should contain a NULL, you would get back an empty string - instead of running into a NULL.ToString() that causes the error you're seeing.

Categories