I have a database attached to a C# project, with 3 tables Company, Product, and Inventory. Inventory lists CompanyID, ProductID, and Quantity. Combobox1 lists all the companyID. When you select one, I want listview1 to display only the rows in Inventory with that CompanyID.
//binding DB to memorycopy
this.DBContext = new DBEntities();
listView1.ItemsSource = this.SupPartContext.SPs;
GridViewColumn companyIDColumn = new GridViewColumn();
sNumColumn.Width = 90;
sNumColumn.Header = "companyID";
sNumColumn.DisplayMemberBinding = new Binding("COMPANYID");
GridViewColumn ProductIDColumn = new GridViewColumn();
pNumColumn.Width = 90;
pNumColumn.Header = "ProductID";
pNumColumn.DisplayMemberBinding = new Binding("PRODUCTID");
GridViewColumn quantityColumn = new GridViewColumn();
qtyColumn.Width = 90;
qtyColumn.Header = "quantity";
qtyColumn.DisplayMemberBinding = new Binding("QUANTITY");
GridView newGridview = new GridView();
newGridview.Columns.Add(companyIDColumn);
newGridview.Columns.Add(productIDColumn);
newGridview.Columns.Add(quantityColumn);
listView1.View = newGridview;
Any help, even a point in the right direction would be appreciated. Thank you for your time.
If you are wanting the list view to change automatically without writing any code you're probably not going to be able to get that to happen. However, if you rebind the list when the combo box value changes with the following line it will filter the list as you expect.
listView1.ItemsSource = this.SupPartContext.SPs.Where(sp => sp.CompanyID = {get the select company from the row here})
Related
I'm a noob to programming and having a bit of trouble with an assignment. I need to write data from an object that was captured from textboxes into a new row in my Members Table datagridview and then save it to the dataset. (On VS and C#)
AddMember newMember = new AddMember();
newMember.firstName = Firstname.Text;
newMember.lastName = Lastname.Text;
newMember.address = AddLine1.Text;
newMember.phone = Int32.Parse(PHnumber.Text);
newMember.membershipID = comboBox1.SelectedIndex + 1;
newMember.directDebit = paymentoptionID;
newMember.paymentFrequency = paymentFrequencyName;
newMember.extras = checkedListBox1.CheckedIndices.Cast<int> ().ToArray();
I want to set my data from my database into the CheckedListBox but with my code I only get error messages that say DataBinding only accepts List or ListSource.
Also with debugging mode I don't even get an error message the CheckedListBox just stays empty.
DataClasses1DataContext d = new DataClasses1DataContext();
////
var query = from pers in d.Person select pers;
BindingList<Person> personen = new BindingList<Person> { new Person { Name = "Name"} };
clVorfahr.DataSource = personen;
clVorfahr.DisplayMember = "Name";
clVorfahr.ValueMember = "Name";
clVorfahr.Refresh();
You are setting the wrong data source. It should be personen not Name.
clVorfahr.DataSource = personen;
clVorfahr.DisplayMember = "Name";
I have created a image grid view using ListView and ImageList.. image names come from database and images are stored in categories folder. I adding images imgList.Images.Add("", Image.FromFile(#"./categories/" + dr["image"]));
but when program runs 1st list view item not set to the correct image, 1stlistviewitem with second image, i think there is a indexing error. is something wrong with my code or any way to solve this?
private void LoadCategories() {
DataTable categories;
con = new Dbfunctions();
con.MysqlQuery("SELECT * FROM categories WHERE online = 1");
categories = con.QueryEx();
//ImageList
ImageList imgList = new ImageList();
//CLEAR listview_Category items
listView_Category.Items.Clear();
//set listview category items as Large icons
listView_Category.View = View.LargeIcon;
//ADD image list into Listview
listView_Category.LargeImageList = imgList;
int i = 0;
//ADD image into imagelist and Listview
foreach(DataRow dr in categories.Rows){
imgList.Images.Add("", Image.FromFile(#"./categories/" + dr["image"]));
ListViewItem category = new ListViewItem();
//bind listview item vwith image list item
category.ImageIndex = i;
//set Category name
category.Text = dr["name"].ToString();
//set font list items styles
category.Font = new System.Drawing.Font("Courier New", 15, System.Drawing.FontStyle.Regular);
category.ForeColor = System.Drawing.Color.FromArgb(252, 119, 123);
//ADD category items into Listview
listView_Category.Items.Add(category);
imgList.ImageSize = new Size(140, 140);
imgList.ColorDepth = ColorDepth.Depth32Bit;
i++;
}
}
Please provide shapes of DB rows. Maybe you have a missmatch in DB so you call not proper index.
I got two datasets with two datatables. Binded and displayed on two datagrids:
//fill datatable
companiesDataTable = new DataTable("Companies");
scheduleDataTable = new DataTable("Schedule");
adapterCompanies.Fill(companiesDataTable);
adapterSchedule.Fill(scheduleDataTable);
//set dataset
myDataSet = new DataSet();
myDataSet.Tables.Add(companiesDataTable);
myDataSet.Tables.Add(scheduleDataTable);
//add relation
myDataSet.Relations.Add("FullSchedule",
myDataSet.Tables["Companies"].Columns["id"],
myDataSet.Tables["Schedule"].Columns["CompanyID"]);
//set binding sources
bsComp = new BindingSource(myDataSet, "Companies");
bsSched = new BindingSource(bsComp, "FullSchedule");
//fill datagrid
dataGridViewCompanies.DataSource = bsComp;
dataGridViewTimes.DataSource = bsSched;
How can i filter companies datagrid without loosing relation to schedule datagrid?
I tried this, but relation seems to be lost:
var view = myDataSet.Tables["Companies"].DefaultView;
view.RowFilter = filterString;
dataGridViewCompanies.DataSource = view;
Well ok, this was embarrassingly simple:
bsComp.Filter = filterString;
dataGridViewCompanies.DataSource = bsComp;
I have a DataGridView with a DataGridViewComboBoxColumn in the first column that is bind whith a List<MyClass> and a DisplayMember="name" (I dont set the valeumember because i want to get myclass when I accesse to value). The second column is a DataGridViewComboBoxColumn too. It will be bound to some List<String> based on the first DataGridViewComboBoxColumn selectedValue.
All this work just fine, the problem starts when I change the first DataGridViewComboBoxColumn.DataSource. When I add a new Myclass to the datasource it works, but if I select the class I will get an error:
System.ArgumentException: DataGridViewComboBoxCell value is not valid
Code:
//Set columns to Datagridview
DataGridViewImageColumn btnEdit = new DataGridViewImageColumn();
Image gear = (System.Drawing.Image)Properties.Resources.gear;
btnEdit.Image = gear;
datagridview.Columns.Add(btnEdit);
//Table
DataGridViewComboBoxColumn cbTable = new DataGridViewComboBoxColumn();
cbTable.HeaderText = "Table";
cbTable.Name = "Table";
cbTable.DisplayMember = "NameToShow";
//tableDataSource = cbTable.DataSource;
//cbTable.DataSource = moduleClone.SqlQuery.Tables;
datagridview.Columns.Add(cbTable);
//...
//...
//...
//Load data
((DataGridViewComboBoxColumn)datagridview.Columns[1]).DataSource = (List<Table>)moduleClone.SqlQuery.Tables;
//Work just fine
//...
//...
//...
//Now if i reload de data
datagridview.Rows.Clear();
((DataGridViewComboBoxColumn)datagridview.Columns[1]).DataSource = (List<Table>)moduleClone.SqlQuery.Tables;
//This give me one message box with the exception several times when drawing the gridview.
Ok, I found a solution that works for me.
Instead use DataGridViewComboBoxColumn.DataSource, I directly change DataGridViewComboBoxColumn.Items. Has a probleme because you cant change DataGridViewComboBoxColumn.Items if DataGridViewComboBoxColumn.DataSource is set. So I define DataGridViewComboBoxColumn.DataSource = null before edit DataGridViewComboBoxColumn.Itens.
Code:
//Set columns to Datagridview
DataGridViewImageColumn btnEdit = new DataGridViewImageColumn();
Image gear = (System.Drawing.Image)Properties.Resources.gear;
btnEdit.Image = gear;
datagridview.Columns.Add(btnEdit);
//Table
DataGridViewComboBoxColumn cbTable = new DataGridViewComboBoxColumn();
cbTable.HeaderText = "Table";
cbTable.Name = "Table";
cbTable.DisplayMember = "NameToShow";
datagridview.Columns.Add(cbTable);
//...
//...
//...
//Load data
((DataGridViewComboBoxColumn)datagridview.Columns[1]).Itens = (List<Table>)moduleClone.SqlQuery.Tables;
//Work just fine
//...
//...
//...
//Now I reload de data
datagridview.Rows.Clear();
((DataGridViewComboBoxColumn)datagridview.Columns[1]).DataSource = null;
((DataGridViewComboBoxColumn)datagridview.Columns[1]).Items.clear();
((DataGridViewComboBoxColumn)datagridview.Columns[1]).Items= (List<Table>)moduleClone.SqlQuery.Tables;