I can't update ComboBox that is linked to another ComboBox. I have one ComboBox that has countries and the other has cities. When I select a country, cityComboBox shows me the cities inside it, but when I change Country it still shows me the same cities. Here is my code
//method to get the cities
public IEnumerable<Ciudad> GetCP(int a) {
string SelectCP = "SELECT [ciudadID],[nombre] FROM [proyectoZoo].[dbo].[Ciudad] where [paisID]=" + a + "";
List<Ciudad> Ciudades = new List<Ciudad>();
foreach (IDataReader reader in this.Execute(SelectCP)) {
Ciudad ciudad = new Ciudad();
ciudad.ciudadID = reader.GetInt32(0);
ciudad.nombre = reader.GetString(1);
Ciudades.Add(ciudad);
}
return Ciudades;
}
Windows form:
IEnumerable<Country> Countries = server.GetCountries();
cmbPais.DataSource = Countries;
IEnumerable<Ciudad> Ciudades = server.GetCP((int)cmbPais.SelectedValue);
cmbCiudad.DataSource = Ciudades;
You need to add this code for the SelectedIndexChanged event handler of cmbPais
void cmbPais_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbPais.SelectedValue != null)
{
IEnumerable<Ciudad> Ciudades = server.GetCP((int)cmbPais.SelectedValue);
cmbCiudad.DataSource = Ciudades;
}
}
Related
public partial class DataGrid_HBD : UserControl
{
public DataGrid_HBD()
{
InitializeComponent();
// 2 Seconds Timer before connecting to the Database.
// This improves UI rendering on button click
DataGrid_Data();
}
/// <summary>
/// Loading Data Grid
/// </summary>
public void DataGrid_Data()
{
// 2 second delay before loading DataGrid
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) };
timer.Start();
timer.Tick += (sender, args) =>
{
timer.Stop();
// Attempt to connect to SQL Server database and populate DataGrid with database tables.
try
{
string connectionString = ("Data Source=\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT [hb_Disputes].[DSP_ID], [hb_disputes].[ACCOUNT], [Users].[TX_EMPLOYEE], [hb_CsrNames].[NM_USER], [hb_disputes].[CUST_NAME],[hb_disputes].[PREM_ADDR], [hb_status].[Status], [hb_disputes].[OPENED], [hb_disputes].[DEADLINE], [hb_disputes].[DATERSLVD], [hb_rpttype].[ReportType], [hb_ratetype].[RateType], [hb_Disputes].[FR_DT_FIRSTREV], [hb_Disputes].[FR_TS_LATESTUPD], [hb_Disputes].[COMMENT], [hb_Disputes].[FR_DSP_CLSF], [hb_Disputes].[FR_CUST_CNTCT], [hb_Disputes].[FR_WRK_REQ], [hb_Disputes].[FR_OPN_ERR], [hb_Disputes].[FR_SO_TP], [hb_Disputes].[FR_SO_DTLS], [hb_Disputes].[FR_SO_DT_WNTD], [hb_Disputes].[FR_SO_ISSD_BY], [hb_Disputes].[FR_CMMNT] FROM [hb_disputes]" +
" LEFT JOIN [Users] ON [hb_disputes].[ASSGNTO] = [Users].[KY_USER_ID] LEFT JOIN [hb_CsrNames] ON [hb_disputes].[WFMUSER] = [hb_CsrNames].[KY_USER_ID] LEFT JOIN [hb_status] ON [hb_disputes].[STATUS] = [hb_status].[STSID] LEFT JOIN [hb_rpttype] ON [hb_disputes].[RPTTYPE] = [hb_rpttype].[RPTID] LEFT JOIN [hb_ratetype] ON [hb_disputes].[REV_CLS] = [hb_ratetype].[RTID]", connection);
connection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connection.Close();
dtGrid.DataContext = dt;
}
catch
{
MessageBox.Show("Database connection is not available at this time. Please contact your database administrator ");
}
};
}
private void dtGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// User double clicks on DataGrid Row
// Open new Window
// Populate selected textboxes with selected datarow
DataGrid gd = (DataGrid)sender;
DataRowView row_selected = gd.SelectedItem as DataRowView;
var windowToOpen = new Window1();
if(gd !=null )
{
// Textboxes
windowToOpen.txt_RowRecrd.Text = row_selected["DSP_ID"].ToString();
windowToOpen.txt_acctnumber.Text = row_selected["ACCOUNT"].ToString();
windowToOpen.txt_analyst.Text = row_selected["TX_EMPLOYEE"].ToString();
windowToOpen.txt_custname.Text = row_selected["CUST_NAME"].ToString();
windowToOpen.txt_address.Text = row_selected["PREM_ADDR"].ToString();
windowToOpen.txt_Status.Text = row_selected["Status"].ToString();
windowToOpen.txt_opened.Text = row_selected["OPENED"].ToString();
windowToOpen.txt_deadline.Text = row_selected["DEADLINE"].ToString();
windowToOpen.txt_DateResolved.Text = row_selected["DATERSLVD"].ToString();
windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();
windowToOpen.txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();
windowToOpen.txt_firstreview.Text = row_selected["FR_DT_FIRSTREV"].ToString();
windowToOpen.txt_Latestupdate.Text = row_selected["FR_TS_LATESTUPD"].ToString();
windowToOpen.txt_reviewNotes.Text = row_selected["FR_CMMNT"].ToString();
windowToOpen.txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();
// Comboboxes
windowToOpen.cmb_UtilityRptTyp.SelectedItem = row_selected["ReportType"].ToString();
windowToOpen.Show();
}
else
{
return;
}
}
When the user double clicks on a row in the Datagrid, it opens a new window and populates the textboxes with the selected cells. However, it does not work for CombinationBoxes. I attached an image of the new window (Window1) that the information is populating to. The image shows the code behind for the combobox with the populated table from the SQL Server database.
First of all, You must set Datasource of cmb_UtilityRptTyp with a list of available report types like this:
// Define ReportType Class
class ReportType {
public int ID { get; set; }
public string Title { get; set; }
public ReportType(int id, string title)
{
ID = id;
Title = title;
}
}
Then set DataSource in first line of dtGrid_MouseDoubleClick:
ReportType[] list = new ReportType[] {
new ReportType(1, "Type 1"),
new ReportType(2, "Type 2"),
};
windowToOpen.cmb_UtilityRptTyp.DataSource = list;
windowToOpen.cmb_UtilityRptTyp.DisplayMember = "Title";
windowToOpen.cmb_UtilityRptTyp.ValueMember = "ID";
After that you must use the SelectedText instead of Text in ComboBox, like this:
windowToOpen.cmb_UtilityRptTyp.SelectedText = row_selected["RPTTYPE"].ToString();
Also, You can use SelectedIndex, to find item index you could IndexOf like this
string rptType = row_selected["RPTTYPE"].ToString();
int index = windowToOpen.cmb_UtilityRptTyp.Items.IndexOf(rptType );
windowToOpen.cmb_UtilityRptTyp.SelectedIndex = index;
Or use FindStringExact
string rptType = row_selected["RPTTYPE"].ToString();
int index = windowToOpen.cmb_UtilityRptTyp.FindStringExact(rptType );
windowToOpen.cmb_UtilityRptTyp.SelectedIndex = index;
I have this table structure
patients:
PK_Patient
PatientID
PatientName
items:
PK_Item
ItemID
ItemDesc
enrolleditems:
PK_EnrollMeds
FK_User_Add
DateTimeAdded
FK_Item
I have a form which has the user included on the form, and datagridview which displays the items enrolled to patient (binded to enrolleditems but items datamember is only displayed on grid), then add and delete button.
The entry on the screenshot is manually added on the database ([MED0001] enrolleditems table) for sample purposes.
On the add button will open another forms which will load all the items and a checkbox to select which item to be added, then a select button which copy the selected datarows to datatable.
I have the below code for the Add button on Enroll Medication form
M3dEntities m3d = new M3dEntities();
enrollmeds _enrollmeds = new enrollmeds();
EnrollMedSelectionFrm enrollselectfrm;
public DataTable SelectedItems { get; set; }
private void AddBtn_Click(object sender, EventArgs e)
{
enrollselectfrm = new EnrollMedSelectionFrm();
var pxdetails = (from adm in m3d.admission
join pxDC in m3d.datacenter
on adm.FK_DC_Patient equals pxDC.PK_Datacenter
where adm.admissionNo == SelectedAdmNo
select new
{
adm,
pxDC
}).FirstOrDefault();
if (enrollselectfrm.ShowDialog() == DialogResult.OK)
{
if (SelectedItems == null)
{
enrollmedsBindingSource.Clear();
}
else
{
enrollmedsBindingSource.Clear();
foreach (DataRow dr in SelectedItems.Rows)
{
_enrollmeds = new enrollmeds();
_enrollmeds.items = new items();
_enrollmeds.FK_DC_Patient = pxdetails.pxDC.PK_Datacenter;
_enrollmeds.FK_DC_userAdd = mainfrm.PK_DC_UserLoggedIn;
var svrDT = ((IObjectContextAdapter)m3d).ObjectContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime currdatetime = svrDT.AsEnumerable().First();
_enrollmeds.AddDateTime = currdatetime;
_enrollmeds.FK_Admission = pxdetails.adm.PK_Admission;
int pkItems = int.Parse(dr.Field<string>("PK_Items").ToString());
var itemdtls = (from i in m3d.items
where i.PK_Items == pkItems
select i).FirstOrDefault();
_enrollmeds.FK_Items = pkItems;
_enrollmeds.items.ItemID = itemdtls.ItemID;
_enrollmeds.items.ItemDesc = itemdtls.ItemDesc;
_enrollmeds.items.GenericName = itemdtls.GenericName;
_enrollmeds.items.ItemGroup = itemdtls.ItemGroup;
enrollmedsBindingSource.Add(_enrollmeds);
}
}
}
}
My problem with this is only the first selected item on the itemselection form.
I want to display all the items selected on the itemselection form to the enrollmedsDataGridView of enrollmedicines form.
Thank You in advance..
I have two dropdownlist controls on an add/edit form page, one for countries (called CountryCode) and one for regions/states (called ProvinceCode). The code has been set up so that, when users select "United States" from the country dropdownlist, then the region dropdownlist is populated with all valid US states. If the user selects "Canada" from the country dropdownlist, then the region dropdownlist is populated with valid provinces. This functionality works fine, but when "Canada" is selected, for some reason, the region dropdownlist's value is never saved to the database. I am not sure why this is, especially since I can save US states just fine.
This is the code that is meant to populate the country and region dropdownlists:
protected void Page_Init(object sender, EventArgs e)
{
CountryCode.DataSource = CountryDataSource.LoadAll("Name");
CountryCode.DataBind();
InitCountryAndProvince();
}
private void InitCountryAndProvince()
{
//MAKE SURE THE CORRECT ADDRESS IS SELECTED
Address address = this.Address;
bool foundCountry = false;
if (!string.IsNullOrEmpty(address.CountryCode))
{
ListItem selectedCountry = CountryCode.Items.FindByValue(address.CountryCode);
if (selectedCountry != null)
{
CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
foundCountry = true;
}
}
if (!foundCountry)
{
Warehouse defaultWarehouse = AbleContext.Current.Store.DefaultWarehouse;
ListItem selectedCountry = CountryCode.Items.FindByValue(defaultWarehouse.CountryCode);
if (selectedCountry != null) CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
}
//MAKE SURE THE PROVINCE LIST IS CORRECT FOR THE COUNTRY
UpdateCountry();
//NOW LOOK FOR THE PROVINCE TO SET
ListItem selectedProvince = ProvinceCode.Items.FindByValue(address.Province);
if (selectedProvince != null) ProvinceCode.SelectedIndex = ProvinceCode.Items.IndexOf(selectedProvince);
}
private void UpdateCountry()
{
//SEE WHETHER POSTAL CODE IS REQUIRED
string[] countries = AbleContext.Current.Store.Settings.PostalCodeCountries.Split(",".ToCharArray());
//PostalCodeRequired.Enabled = (Array.IndexOf(countries, Country.SelectedValue) > -1);
//SEE WHETHER PROVINCE LIST IS DEFINED
IList<Province> provinces = ProvinceDataSource.LoadForCountry(CountryCode.SelectedValue);
if (provinces.Count > 0)
{
IEnumerable<Province> sortedProvinces = provinces.OrderBy(f => f.Name);
provinces = sortedProvinces.ToList();
ProvinceCode.Visible = false;
ProvinceCode.Visible = true;
ProvinceCode.Items.Clear();
ProvinceCode.Items.Add(string.Empty);
foreach (Province province in provinces)
{
string provinceValue = (!string.IsNullOrEmpty(province.ProvinceCode) ? province.ProvinceCode : province.Name);
ProvinceCode.Items.Add(new ListItem(province.Name, provinceValue));
}
ListItem selectedProvince = FindSelectedProvince();
if (selectedProvince != null) selectedProvince.Selected = true;
ProvinceCode.Enabled = true;
ProvinceCode.Text = string.Empty;
}
else
{
ProvinceCode.Visible = true;
ProvinceCode.Visible = false;
ProvinceCode.Items.Clear();
//Province2Required.Enabled = false;
}
}
private ListItem FindSelectedProvince()
{
string defaultValue = ProvinceCode.Text;
if (string.IsNullOrEmpty(defaultValue)) defaultValue = Request.Form[ProvinceCode.UniqueID];
if (string.IsNullOrEmpty(defaultValue)) return null;
defaultValue = defaultValue.ToUpperInvariant();
foreach (ListItem item in ProvinceCode.Items)
{
string itemText = item.Text.ToUpperInvariant();
string itemValue = item.Value.ToUpperInvariant();
if (itemText == defaultValue || itemValue == defaultValue) return item;
}
return null;
}
protected void Country_Changed(object sender, EventArgs e)
{
//UPDATE THE FORM FOR THE NEW COUNTRY
UpdateCountry();
}
This is the section of the save method that contains the country and region dropdownlist controls:
private void SaveCustomerInfo(int CustID)
{
int currentUserID = AbleContext.Current.UserId;
string editQuery = "UPDATE Customers SET BillToRegion = #BillToRegion, BillToCountry = #BillToCountry WHERE CustomerID = #CustomerID";
string addQuery = "INSERT INTO Customers (BillToRegion, BillToCountry) VALUES(#BillToRegion, #BillToCountry)";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Parameters.Add(new SqlParameter("#BillToRegion", ProvinceCode.SelectedValue));
cmd.Parameters.Add(new SqlParameter("#BillToCountry", CountryCode.SelectedValue));
cmd.ExecuteNonQuery();
cn.Close();
}
}
catch (Exception exception)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - SaveCustomerInfo", exception);
}
And this is the code that is called when the page loads to populate fields with values from the database (if the user is editing an existing entry):
protected void Page_Load(object sender, EventArgs e)
{
_CustomerID = AlwaysConvert.ToInt(Request.QueryString["CustomerID"]);
int.TryParse(Request.QueryString["CustomerID"], out _CustomerID);
if (_CustomerID == 0)
{
AddBtn.Visible = true;
EditBtn.Visible = false;
}
else
{
custIDHidden.Value = _CustomerID.ToString();
AddBtn.Visible = false;
EditBtn.Visible = true;
}
if (!Page.IsPostBack)
{
if (_CustomerID != 0)
{
string selectQuery = "BillToRegion, BillToCountry FROM Customers WHERE CustomerID = #CustomerID";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, cn);
cmd.Parameters.Add(new SqlParameter("#CustomerID", custIDHidden.Value));
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ProvinceCode.SelectedValue = reader["BillToRegion"].ToString();
CountryCode.SelectedValue = reader["BillToCountry"].ToString();
}
}
cn.Close();
}
}
catch (Exception x)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - Page_Load", x);
}
}
}
}
I have a combobox for an item list that I populate using the following code:
List<string> comboboxItems = new List<string>();
foreach (var p in products)
{
var x = p.Product;
foreach (var pn in x)
{
comboboxItems.Add(pn.name + " :Qty " + pn.quantity_available
+ " :Tax " + pn.default_tax_tier);
}
}
cmbItems.DataSource = comboboxItems;
What should I do in order to get the value, pn.name only when the combobox item is selected?
Using WinForms.
You have to handle the event DataGridView.EditingControlShowing event, in there you can access the actual combobox and register the SelectedIndexChanged event handler like this:
//EditingControlShowing event handler for your dataGridView1
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e){
if(dataGridView1.CurrentCell.OwningColumn == cmbItems){
var combo = e.Control as ComboBox;
combo.SelectedIndexChanged -= cmbItems_SelectedIndexChanged;
combo.SelectedIndexChanged += cmbItems_SelectedIndexChanged;
}
}
private void cmbItems_SelectedIndexChanged(object sender, EventArgs e){
var combo = sender as ComboBox;
//Note that SelectedItem may be null
var s = Convert.ToString(combo.SelectedItem);
int i = s.IndexOf(" :Qty");
var selectedName = i == -1 ? "" : s.Substring(0,i+1).TrimEnd();
//other code ...
}
you should create an item such as
public class CboItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
then you can create easily using something like
CboItem item = new CboItem();
item.Text = "My Item";
item.Value = "Anything";
in the Value you can store your var pn whatever it is.
then you can retrieve like so :
((CboItem)comboBox1.SelectedItem).Value;
You will need to cast the result above to the proper type you stored inside as the Value is of type object.
We can also regular expressions to extract data from string.
Create string variable in the below format
string inputPattern = "{0} :Qty {1} :Tax {2}";
While inserting the data into combo box,
comboboxItems.Add(string.Format(inputPattern, p.Name, p.Quantity_Available, p.Tax));
After you added it, to extract the strings we can use Regex, code snippet below.
string extractPattern = "(?<Name>.*) :Qty (?<Qty>.*) :Tax (?<Tax>.*)";
foreach (var item in (comboBox1.DataSource as List<string>))
{
var matches = Regex.Match(item, extractPattern);
if (matches.Groups["Name"].Success)
{
MessageBox.Show(matches.Groups["Name"].Value);
}
if (matches.Groups["Qty"].Success)
{
MessageBox.Show(matches.Groups["Qty"].Value);
}
if (matches.Groups["Tax"].Success)
{
MessageBox.Show(matches.Groups["Tax"].Value);
}
}
I have a datagridview in my form. It fills by selecting country with cities of country.I have set the property (AllowUsersToAddRow = True)
but when i run my project user can't add or edit or delete any row.I checked it.It is not readonly(readonly = false) and It is enable (Enabled = true)
What's the problem?
Code of fill datagridview:
private void cmbCountryValues_SelectedIndexChanged(object sender, EventArgs e)
{
dgvCityValues.Enabled = cmbCountryValues.SelectedIndex>=0;
if (!dgvCityValues.Enabled)
{
dgvCityValues.DataSource = null;
return;
}
int CountryId = int.Parse(cmbCountryValues.SelectedValue.ToString());
dgvValues.DataSource = from record in Program.dal.Cities(CountryId) select new { record.City};
}
If you find this question useful don't forgot to vote it.
To give a simplified example, if I do the equivalent of your query, such as:
var cities = new City[] { new City("New York","NY"), new City("Sydney","SY"), new City("London","LN") };
dataGridView.DataSource = cities;
I get the same result as you - no option to add new rows, but if I change to BindingList<T> and set this to AllowNew it all works:
var cities = new City[] { new City("New York","NY"), new City("Sydney","SY"), new City("London","LN") };
var citiesBinding = new BindingList<City>(cities);
citiesBinding.AllowNew = true;
dataGridView.DataSource = citiesBinding;
EDIT - with a solution for your particular example:
private class City
{
public string Name { get; set; }
}
private void cmbCountryValues_SelectedIndexChanged(object sender, EventArgs e)
{
dgvCityValues.Enabled = cmbCountryValues.SelectedIndex >= 0;
if (!dgvCityValues.Enabled)
{
dgvCityValues.DataSource = null;
return;
}
int CountryId = int.Parse(cmbCountryValues.SelectedValue.ToString());
var queryResults = from record in Program.dal.Cities(CountryId) select new City { Name = record.City };
var queryBinding = new BindingList<City>(queryResults.ToList());
queryBinding.AllowNew = true;
dgvValues.DataSource = queryBinding;
}
Note that a) I had to change the anonymous type in the query select into a concrete type City and also change the IEnumerable<T> returned by Linq query to an IList<T> compatible type to create the BindingList<T>. This should work, however :)