i have this code for fill combo-box
SQL = "SELECT DISTINCT Name,Num FROM MyTbl order by Name";
adp = new OracleDataAdapter(SQL, Conn);
adp.Fill(dsNa, "MyTbl");
adp.Dispose();
comFna.DataSource = dsNa.Tables[0];
comFna.DisplayMember = dsNa.Tables[0].Columns[0].ColumnName;
comFna.ValueMember = dsNa.Tables[0].Columns[1].ColumnName;
but after inserting new Name - i dont see hem
and after i run this code again - i see duplicity records (only in the combobox)
how to solve this ? (i work on C# Winforms)
thank's in advance
when you add new name to database you have two choices:
1) create a new item with given name (and num value) and insert it into combobox items collection.
2) re-load combobox from database (which you are using)
the only problem is that you need to clear combobox items or set its datasource to null before re-binding it.
adp.Dispose();
comFna.DataSource = null; //ADD THIS LINE HERE OR comFna.Items.Clear();
comFna.DataSource = dsNa.Tables[0];
comFna.DisplayMember = dsNa.Tables[0].Columns[0].ColumnName;
comFna.ValueMember = dsNa.Tables[0].Columns[1].ColumnName;
comFna.Items.Clear();
before filling your comboBox, use this code to remove old items, and then re-fill it.
Related
Edit: I am using window forms
So, I want to change a value of NumericUpDown if the selected value in combo box changes.
I placed a data table items with the columns ID, itemName, itemPrice and Stock and set the DisplayMember property to itemName.
I used this code:
cmb.DisplayMember = "itemName";
cmb.DataSource = items;
Then to get the whole row of the selected item I used
DataRow dataRow = ((DataRowView)cmbItems.SelectedItem).Row;
The problem is that in the UI, the combo box's selected item does not changes no matter what I do but the value of the selected item changes.
Like this.
I first thought that my unit is just lagging but its not. How do I fix this?
You can try this code to check if the combobox will get the selected item when you make your option.
dbConn.Open();// this allows you to edit the database
string sql = "Select * from database1";
SqlCommand dbComm = new SqlCommand(sql, dbConn);
SqlDataAdapter dbAdapter = new SqlDataAdapter(dbComm);
DataTable dt = new DataTable();
dbAdapter.Fill(dt);
cmbDescription.DataSource = dt;
cmbDescription.DisplayMember = "itemName";
cmbDescription.ValueMember = "Enter the column name here";
cmbDescription.Text = "";
cmbDescription.Items.Add(dt);
cdbConn.Close(); //close connection to save all your inputs,calculations to the database
I found out that my code is very confusing and the system seem to be having problems while executing, I've rewritten the whole code for this window form and it is working now.
I have created following function to call for delete the selected record from database
and also from the listbox, which will get call on button Delete Record.
Function:
void DeleteListBox2()
{
string connstring = String.Format("Server=localhost;Port=***;" +
"User Id=****;Password=****;Database=****;");
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
for (int i = 0; i < listBox2.SelectedItems.Count; i++)
{
var itemname = listBox2.SelectedValue;
NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM Table WHERE Value = '" + itemname + "'", conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Selected record deleted from database");
listBox2.Items.Remove(listBox2.SelectedItems[i]); //ERROR LINE
listBox2.Update();
}
conn.Close();
}
ERROR:
Items collection cannot be modified when the DataSource property is set.
It would help to provide additional details such as what frameworks you are using, WPF, Winforms etc.
Your ListBox should have its data source bound to a List object. Then instead of deleting directly from the list view component, you should delete from the List that the view is bound to and then notify the UI that there are changes and it will update accordingly.
Here is a good read on binding your list view to a list object:
Binding Listbox to List
A quick google search will return results on how to update UI once changes have been made, here is one for winforms:
How to refresh DataSource of a ListBox in C# WinForms
I would suggest after you delete the items in the database to read it again. Since you probably want to have a recent mapping from database to the display.
After that just reattach the DataSource property to the new record collection.
I am working on an application which has DataGridView bound to a database.
Done simply by setting datasource property of datagridview.
As now I have to work on search feature so
I get the data in a new data set and tried to bind the data source with the newly created dataset.
gridviewobj.datasource = newdataset;
but the data grid always appear as empty.
my data set contents the appropriate data.
still the problem arrives.
Please help
To show the data we have to bind it as well after setting up the datasource (as "Naveen" mentioned in the comment):
gridviewobj.datasource = newdataset;
gridviewobj.DataBind();
Hope it helps you.
private void GridBind(string StrQry="")
{
string Qry = string.Empty;
//StrQry = "Select * from tbl_Emp where Dept='Acc'";
if (StrQry != string.Empty)
{
Qry = StrQry;
}
else
{
Qry = "Select * from tbl_Emp";
}
Conn();
Cmd = new SqlCommand(Qry,con);
da = new SqldataAdapter(cmd);
da.fil(dt);
if (dt != null && dt.Rows.Count > 0)
{
gridviewobj.datasource = dt;
}
}
You may have to set the Table index, This will work if the columns are created dynamically.
gridviewobj.datasource = newdataset.Tables[0];
If you have already created the Grid columns manually through the Grid Edit wizard then you will have to go to properties of each column and set the DataPropertyName to relevant Database table column name to bind.
I have a .MDB file that has a n_groups table, and three columns I need to reference, those being NUID, strName and ntype. The query works as intended, however the listbox just appears empty with no results.
How can I make the results populate a listbox?
This is what I have so far:
var conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" +
"data source=C:\\menus\\newmenus\\menu.mdb;Jet " +
"OLEDB:Database Password=######");
var ds = new DataSet();
var adapter = new OleDbDataAdapter(
"SELECT nUID, strName FROM n_groups where ntype=1", conn);
conn.Open();
adapter.Fill(ds);
conn.Close();
var value = ds.Tables[0].Rows[0]["strName"].ToString();
listBox1.DataSource = value;
Say I wanted to reorder the list using a second list box (like a picklist sorter) is there a way to call the position of an item within a listbox?
I need to be able to re order the contents of the results from top to bottom for sorting.
I was going to use a picklist sorting method like this :
But I can't get the left list to populate. How do I use C# to get data from MDB and put the results into a listbox?
There's an easier way to do it... instead try it this way
How can I make the results populate a listbox?
This is semi-psuedocode:
dt DataTable
dt = ds.Tables[0]
listbox1.datasource = dt
listbox1.datamember = "nameoffield"
listbox1.databind()
This basically takes the entire datatable and binds it to the control. You can reference each object with .SelectedIndex or .SelectedItem
I think what you are trying to look for is this:
in the click event of add:
listbox2.items.add(listbox1.selecteditem)
In the click event of remove
listbox2.items.removeat(listbox2.selectedindex)
I have a combobox that is filled with data field JobCode from database. There are 1000s of jobcode and when the user needs to select one jobcode he has to scroll down through all the jobcodes in the combobox. Can I do it in such a way that if the user types some letter of jobcode it will show the jobcodes which start with that letter in the combobox at the top of list so the user can select easily. For example, like adding some code in keypressevent in combobox.
The user must still choose from jobcodes in the list, not keep partially or incorrectly entered data that will cause wrong data entry at insert and update time.
public void jobcomboboxload()
{
OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode from jobcodemastertable", oleDbConnection1);
OleDbDataReader reader = oleDbCommand1.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("jobpk", typeof(int));
dt.Columns.Add("jobcode", typeof(string));
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt.DefaultView;
oleDbConnection1.Close();
}
jobcode is an unique field.
Use cmbjobcode.AutoCompleteMode = AutoCompleteMode.Suggest (or other
values of the enum)
Use cmbjobcode.AutoCompleteSource = AutoCompleteSource.ListItems
Change your query including the clause ORDER BY on the field jobcode
Please, don't forget the using statement around your OleDbConnection, OleDbCommand and OleDbDataReader. This will assure the proper dispose of the before mentioned variables.
For the checking on incomplete values, you should add the Validating event and, in that event, check if the text entered is present in your strings.
The combobox has a method called FindStringExact() that can help.
Set your ComboBox AutoCompleteMode properties to Suggest AND AutoCompleteSource to ListItems or else you won't see the suggestion.
Like Steve said you can change your query and add ORDER BY on your request field to set the order in which you want them in your SELECT statement.
Hope this helps don't hesitate if you have any questions.