Changing a label based on selected item of combobox - c#

attempting to change text based on a combobox selection. C#, windows form, combobox loads on form load.
using an ADO entity data model to map the db, I have the tables added in. I have a combobox which loads the vendors, then a button that says get vendor. upon that I have 3 labels I want to display the vendor name, city and zip. I'm just having trouble figuring out how to make the label binded to the selected item from the combobox (the vendor selected) and make it change.
quick edit: I know the button code is wrong. but its where i'm at so I posted it. Thanks!
PayablesEntities payablesSet = new PayablesEntities();
private void Form1_Load(object sender, EventArgs e)
{
comboBoxVendor.DataSource = payablesSet.Vendors.ToList();
comboBoxVendor.DisplayMember = "Name";
comboBoxVendor.ValueMember = "VendorID";
}
private void buttonGetVendor_Click(object sender, EventArgs e)
{
label5.text = comboBoxVendor.SelectedValue;
}

Take a look at the SelectedIndexChanged & SelectionChangeCommitted events.

Figured it out! needed a query to store that selected item's info, duh!
private void buttonGetVendor_Click(object sender, EventArgs e)
{
int vendorID = (int)comboBoxVendor.SelectedValue;
var selectVendor =
(from vendor in payablesSet.Vendors
where vendor.VendorID == vendorID
select vendor).First();
label5.Text = selectVendor.Name;
label6.Text = selectVendor.City;
label7.Text = selectVendor.ZipCode;
}

Related

How to dynamically filter DataGridView using TextBox data?

I created a simple DataGridViewa with a single column and added a TextBox above.
Currently the text actually a DataTable (I though this would make things easier for filtering) with 2 columns, number and text (I hide the number in the DataGridView ). I can change it to any other class if required.
When the user enters a letter in the TextBox, I want to dynamically filter and show only the lines containing this text.
I load the data to the DataGridView like this:
private void PhrasesForm_Load(object sender, EventArgs e)
{
phrasesDataGridView.ReadOnly = true;
phrasesDataGridView.DataSource = _phrases.Phrases;
phrasesDataGridView.Columns[0].Visible = false;
this.phrasesDataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
When add another letter the filter will be readjusted.
What do I write here...
private void filterBox_TextChanged(object sender, EventArgs e)
{
}
If you have a DataGridView, or any Control with a DataSource, consider using Nuget Package BindingListView. It has functionality to sort the binding list by any column on a mouse click, but it also has functionality to filter the data.
List<Customer> customers = ...
BindingListView<Customer> customerView = new BindingListView<Customer>(customers);
dataGridView1.DataSource = customerView;
And presto, if shows all Customers. The columns depend on you column definition.
To filter the customers, for instance, show only customers with born before a certain date:
void ShowCustomers(DateTime limitDate)
{
customerView.ApplyFilter( customer => customer.BirthDay < limitDate));
}
Result: only the older Customers are shown.
You can do it similarly:
void ShowItemsWithPhraseStart(string phraseStart)
{
myView.ApplyFilter(row => row.Phrase.StartsWith(phraseStart));
}
And bingo: the datagrid shows only the row with a value for property Phrase that starts with phraseStart.
private void OnTextBocChanged(object sender, ...)
{
var text = this.TextBox1.Text;
ShowItemsWithPhraseStart(text);
}

Get value from selected row in WPF Datagrid

I have a DataGrid in my WPF projects
As you can see i'm able to select a row. I've made a double click method where i get the selected row. i want get just the ID part of that row.
This is how my method looks like
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow = sender as DataGridRow;
}
How to I just get the cell where I put the ID in it?
Many thanks in advance
If you are showing both XAML and cs code then only we can find the proper solution. Now I am assuming that you are displaying the contents by using binding from an observable collection of any class type. So you can easily get the ID field by,
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
ClassName classObj = dataGridName.SelectedItem as ClassName;
string id = classObj.ID;
}
This is the code I used to get the ID of the double clicked row. I used an exploit of the double click (When you double click a row, you also select it). In my case, the column containing the id was the first (Row[0])
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView dataRowView = (DataRowView)yourDataGridView.SelectedItem;
int ID = Convert.ToInt32(dataRowView.Row[0]);
}

Edit The listbox selected text in Runtime asp.net

I have a listbox in asp.net
I tried when user click on it's item in runtime can change the text of this item without change the order of the the items .
So I try to make Edit button when click on it get the item value
as
protected void btnEditListValue_Click(object sender, EventArgs e)
{
string listvalue = lstParameters.Items.IndexOf(lstParameters.SelectedItem).ToString();
string listText = lstParameters.SelectedItem.ToString();
}
then I will make textbox fill value from string listText to enable user to edit
what shall I do after that to keep listbox order as old without delete and insert again
please help
You can overwrite the ListItem.Text, f.e. when the user changed the text:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(lstParameters.SelectedIndex >= 0 && !String.IsNullOrWhiteSpace(TextBox1.Text))
{
ListItem selectedItem = lstParameters.Items[lstParameters.SelectedIndex];
selectedItem.Text = TextBox1.Text;
}
}

finding value of dynamically created combobox

I'm using VS2010,C#, I have a table that its data should be created dynamically (from an SQL server table), I have to add a combobox (with 3 items) to one of the columns, this combo box is also created dynamically, then I give each combo a unique ID, it has autopost back set to off and also enableviewstate and viewstatemode to true and enabled, when users changes values for some combo boxes (each row has a combox), and then presses the submit button, I want to have current state of my comboboxes but their selectedindex is 0 so I cannot use them, what should I do? what are my options? (I find each combobox using FindControl and unique ID of the combobox)
thanks
Please find below answer for your above questions
First of all you need to register onchange event of combobox in Javascript while create dynamic combobox.
Put one hidden field on page
And then put the code in onchange event, set the value in hidden field using Clientid from onchange event and then get the value of hidden field from server side.
you can use postback.. here is a sample code snippet.. if you want to work on postback then then follow this.. else you can follow the approach as Rahul told you..
public partial class DynamicCombo : System.Web.UI.Page
{
DropDownList list;
protected void Page_Init(object sender, EventArgs e)
{
Table table = CreateHtmlTable();
list = new DropDownList();
list.AutoPostBack = true;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.ID = "cbo";
list.Items.Add(new ListItem("value1", "1"));
list.Items.Add(new ListItem("value2", "2"));
list.Items.Add(new ListItem("value3", "3"));
table.Rows[0].Cells[0].Controls.Add(list);
pnl.Controls.Add(table);
}
private void list_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("<script>alert(\"" + list.SelectedIndex + "\");</script>");
}
protected void Page_Load(object sender, EventArgs e)
{
}
private Table CreateHtmlTable()
{
Table table = new Table();
table.Rows.Add(new TableRow());
table.Rows[0].Cells.AddRange(new TableCell[] { new TableCell(),
new TableCell(),
new TableCell()});
return table;
}
}

how to dynamically add combobox in windows forms(C#) and bound it to a column of a table in sql database

My windows form has an ADD button which adds a combo box to the form after each click. The problem is, i am not able to bind it to a table column at run time. Using an existing databinding source selects the same value in all the combo boxes. I am coding in C#
here is the sample code :
ComboBox ocbNext = new ComboBox();
//HAVE set the rest of the properties right, the problem is with the databinding
ocbNext.DataSource = this.dummysubjectBindingSource;
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
this.Controls.Add(ocbNext);
I added a DataSet to the solution and droped the Employees table (from Northwind) in the designer, which automatically created the employeesBindingSource. I dropped a combobox and a button on the Form and I set the DataSource and DataMember of the combo. Then I handled some events:
private void Form1_Load(object sender, EventArgs e)
{
this.employeesTableAdapter.Fill(this.dS.Employees);
}
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
ComboBox combo = new ComboBox();
combo.DataSource = this.employeesBindingSource;
combo.DisplayMember = this.dS.Tables[0].Columns[++_i].ColumnName;
combo.Location = new Point(comboBox1.Location.X, comboBox1.Location.Y + comboBox1.Height * _i);
this.Controls.Add(combo);
}
So on each click, a new combo is added onto the form dynamically right under the previous combo. The combo is also bound to the next column in the Employees table (no boundary checks however).
As you can see, this is pretty easy stuff. Hope this helps.
Okay, so here is a variation of the code that could help you with that other question you asked in the comments of this answer.
It assumes you have a Form with a button and a DataSet with table Employees. On button click it creates a combo, and fills it with data (the Name column of Employees). Each time you add a combo, it gets its own copy of the data (this is important to be able to remove items from one combo at a time). Then, every time you select a value in the combo, the combo is disabled and the other combos don't have that selected value in their list.
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
DataSet dataS = dS.Clone();
this.employeesTableAdapter.Fill((DS.EmployeesDataTable)dataS.Tables[0]);
BindingSource bindSource = new BindingSource(dataS, "Employees");
ComboBox combo = new ComboBox();
combo.Name = this.dS.Tables[0].Columns[0].ColumnName + (++_i).ToString();
combo.DataSource = bindSource;
combo.DisplayMember = this.dS.Tables[0].Columns[1].ColumnName; //This column is the Name of Employee
combo.Location = new Point(button1.Location.X, button1.Location.Y + combo.Height * _i);
combo.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
this.Controls.Add(combo);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is ComboBox && ctrl != sender && ctrl.Enabled)
{
((BindingSource)((ComboBox)ctrl).DataSource).RemoveAt(((ComboBox)sender).SelectedIndex);
}
}
((ComboBox)sender).Enabled = false;
}
This is pretty close to what you require, or easily adaptable to meet your expectations. Enjoy and please select an answer as the accepted one. Thanks!
Option 1: Fill the combobox with strings:
this.comboBox1.Items.Add("Syed");
this.comboBox1.Items.Add("Baqar");
Option 2: Fill the combobox with an array of strings:
this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" });
You need to add controls to the parent window first, and then set the data source.
ComboBox ocbNext = new ComboBox();
this.Controls.Add(ocbNext);
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
ocbNext.DataSource = this.dummysubjectBindingSource;
Should be fine if you create a new local ComboBox variable in the clickevent. If you use a global variable for the ComboBox this might explain your problems. But without a sample how you're doing it's hard to see what's really happening, so think this is just a rough guess

Categories