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.
Related
I have two forms, in one I fill DataGridView with some rows, each row has two comboboxes.
I am able to get both value and formatted value from these cells, however when I try to copy all of this data into the next DataGridView that is in different form, I am unable to tell him which item from the ComboBox should be marked as selected.
When I was looking around I found these lines of code (unfortunately they were from 6+ years ago)
dataGridView.Rows[index].Cells[3].Value = ImageFormat.Items[1];
(dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Value = ImageFormat.Items[0];
DataGridViewComboBoxCell comboboxFormat = (DataGridViewComboBoxCell)(dataGridView.Rows[index].Cells[3]);
comboboxFormat.Value = ImageFormat.Items[0];
(dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Value = (dataGridView.Rows[index].Cells[3] as DataGridViewComboBoxCell).Items[0];
Unfortunately none of these worked and most if not all threw "DataGridViewComboBoxCell value is not valid" exception
Maybe it's worth to mention that the possible items are binded from database like so:
string stm = "SELECT * FROM colours";
using var cmd = new SQLiteCommand(stm, MainWin.con);
SQLiteDataAdapter rdr = new SQLiteDataAdapter(cmd);
DataTable dataTableColour = new DataTable();
rdr.Fill(dataTableColour);
stm = "SELECT * FROM formats";
using var cmdd = new SQLiteCommand(stm, MainWin.con);
SQLiteDataAdapter reader = new SQLiteDataAdapter(cmdd);
DataTable dataTableFormat = new DataTable();
reader.Fill(dataTableFormat);
ImageFormat.ValueMember = "id";
ImageFormat.DisplayMember = "name";
ImageFormat.DataSource = dataTableFormat;
ColourImage.ValueMember = "id";
ColourImage.DisplayMember = "name";
ColourImage.DataSource = dataTableColour;
Your datagridview should be bound to some datatable (let's say ImageFiles). Set ColourImage/ImageFormat combo's .DataPropertyName property to be the name of the column in the [ImageFiles] datatable that the combo should edit. Don't try to interact with the DGVCombo directly; just interact with the datatable to which the grid is bound
I'm a SQL DBA with low skill level in VS C# and Winforms. I've been struggling with adding a combo box to a DataGridView column for several days and have given up. I have a datatable dt1 and datagridview dg1. dg1.Datasource = dt1; dt1 is a member of dataset ds1. I am providing combo items from an array.
I have tried autogeneration true and false.
If autogeneration=true I get two columns of the same name with 1 combo box and it's in the wrong column position and I get correct data from dt1
If false and I programmatically define columns for dg1, I don't get any data from dt1.
What should my code look like and what possible bindings or properties am I missing so that I add a combo box for 'GRADE' in the 4th column position and dg1 populates from dt1 and combo from array.
Totally frustrated after reading dozens of blogs and trying things for several days. Please help.
private DataGridViewComboBoxColumn CreateComboBox()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
{
combo.Name = "comboColumn";
combo.HeaderText = "Grade";
ArrayList drl = new ArrayList();
drl.Add("GS1");
drl.Add("GS2");
drl.Add("WG1");
drl.Add("WG2");
combo.Items.AddRange(drl.ToArray());
combo.DataSource = drl;
//combo.ValueMember = "EmployeeID";
//combo.DisplayMember = "Grade";
//combo.DataPropertyName = "Grade";
}
return combo;
}
public Employee()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
Ds1 = new DataSet("ds1");
Dt1 = new DataTable("dt1");
ds1.Tables.Add(dt1);
dt1.Columns.Add("EmployeeID");
dt1.Columns.Add("FirstName");
dt1.Columns.Add("LastName");
dt1.Columns.Add("Grade");
dt1.Columns.Add("DOB");
//initialize datagridview
Dg1.AutoGenerateColumns = true;
//dg1.Columns.Add("column4", " EmployeeID ");
// dg1.Columns.Add("column4", " FirstName ");
// dg1.Columns.Add("column4", " LastName ");
Dg1.Columns.Add(CreateComboBox());
// dg1.Columns.Add("column5", " DOB ");
Dg1.DataSource = dt1;
}
Resolved: After several days hunting and trying, I tried a solution I didn't think would work because it mentioned an unbound column and appeared to require a SQL or some other connection make it a bound column. Turns out it isn't necessary to bind the columns. Here is what you do.
1.Open your Form designer an place Focus on your DataGridView and select properties.
2.Scroll down to the Columns collection (mine was at the bottom under Misc.) and expand the collection.
3.Add Column name and if binding to DataTable set the DataPropertyName to the dt column. In my case I set both the same.
Also there is a drop down to choose the ColumnType
4.Add your ComboBox column. This has a few more settings. You should look through all of them but I was interested in Items &
HeaderText only. I set HeaderText the same as ColumnName &
DataPropertyName. I then opened the Items and added my list.
There is also a DataSource. I presume that is for populating your
ComboBox from a database, service, or sharepoint but I'm not doing
that.
5.That's it.
In your .cs code file you need to insert two lines of code. I placed mine at the top of the form where I declare datatables I need available to all methods.
<yourdatagridview>.AutoGenerateColumns = false;
<yourdatagridview>.DataSource = <yourdatatable>;
I want to add a row in datagridview under my last row which shows summary of the records.So I Fill my dataset and then add a row in it, afterwards I Bind it to a datagridview and then when I try to assign my new row with value it gives me error that cannot convert date time to string as The fourth column datatype is datetime.SO my question is can we change column type of a specific row cell ,If no then how can I achieve what I want to do?
string SelectGroupQuery = "Select * From GroupMembers Where GID=#Id ";
using (SqlConnection conGroup = new SqlConnection(ConnectionString.ToString()))
{
using (SqlCommand commandGroup = new SqlCommand(SelectGroupQuery, conGroup))
{
commandGroup.CommandType = CommandType.Text;
commandGroup.Parameters.Add(new SqlParameter("Id", Id));
SqlDataAdapter da = new SqlDataAdapter(commandGroup);
DataSet ds = new DataSet();
da.Fill(ds);
d1 = new DataGridView();
this.Controls.Add(d1);
d1.Location = new Point(50,y);
d1.Size = new Size(600, 300);
dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
d1.DataSource = ds.Tables[0];
d1.Columns[4].ValueType = typeof(string);
d1.Rows[d1.Rows.Count-2].Cells[4].Value = "Total Amount";
y = y + 400;
}
}
Changing the type of a particular cell could be done for some of the column types. See this answer. But I wouldn't recommend it.
It is tricky to use a row in datagridview for displaying summary as it brings in some problems (more when bound to database). You could create your summary row using textboxes outside the datagridview and add the summary data to them. Check this codeproject link for working example.
Instead of assigning the value to the cell, you could fake it by handling the CellFormatting event. If e.ColumnIndex = 4 and e.RowIndex is the last row in the grid, set the formatted value to the label you want and set the property of the EventArgs to tell it you formatted the value.
If you aren't making the whole DataGridView read-only, you probably want to also handle the CellBeginEdit event and cancel the edit if it's the summary row.
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 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.