i have a third party combobox and is bound to the stocklocationBindingSource's location field. however because it is a thirdparty component,devexpress they advised that i populate the combobox manually because by just binding it, it only shows the first row in the table.
now: i have about 100 comboboxes and thought it best to have the code in a seperate class and the call that class and pass the parameters using a constructor...but not sure how to go about it. i have the following:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WinUI.BaseForms; using WinUI.DataSets; namespace WinUI.Forms.BackOffice { using WinUI.DataSets; using WinUI.DataAccessLayers; using WinUI.HelperClasses; public partial class frmItemMaintenance : frmButtons { public frmItemMaintenance() { InitializeComponent(); CenterToScreen(); btnOk.Hide(); } private void frmItemMaintenance_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'stockLocationData.STOCKLOCATION' table. You can move, or remove it, as needed. // TODO: This line of code loads data into the 'itemData.ITEMMASTER' table. You can move, or remove it, as needed. //comboBoxEditLocation.DataSource = stockLocationData.Tables[0]; //comboBoxEditLocation.DisplayMember = "Location"; //will not suffice
GetComboBoxRows getRows = new GetComboBoxRows(stockLocationData, comboBoxEditLocation,"Location",0); //thought i can do it like this??? } } }
|
and then my manual code to populate:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Configuration; using System.IO; using System.Data.SqlClient; namespace WinUI.HelperClasses { class GetComboBoxRows { private DataSet dsLocal; private DevExpress.XtraEditors.ComboBoxEdit comboBoxLocal; private string table; int rowLocal; //contructor public GetComboBoxRows(DataSet ds,DevExpress.XtraEditors.ComboBoxEdit name,string tableName,int rowno) { dsLocal = ds; comboBoxLocal = name; table = tableName; rowLocal = rowno; } public void GetRows() { foreach (DataTable dt in dsLocal.Tables) { int i = 0; foreach (DataRow dr in dt.Rows) //loop through rows { comboBoxLocal.Properties.Items.Add(dsLocal.Tables[0].Rows[i][table]); i++; } } } } }
|
theLizardPosted Oct 29, 2009, 4:35 AM
call the function like this;
GetRows(
stockLocationData,comboBoxEditLocation);I dont think you need this [ string tableName,int rowno ]
stockLocationData- this should already have the table you want to fill the combo box with.Sending
comboBoxEditLocationto the function fills the combo box you send so no need to return anything...The code I posted is generic so it could be used in any application you write to fill combo boxes, this is the way I do things, I write code once and use it in just about every application I write. No point doing things more than once.
If you decide to use SqlClient you could simplify your code by many factors.
eg
you could write code like this
common.FillCombo(
stockLocationData, SELECT * FROM Customers ORDER BY name", "customerName");public void FillCombo(ComboBox Sender, string sql, string fieldName)
{
sqlCon q = new sqlCon(); //these are from a class that handles ALL my SQL connections
q.connect();
q.command(sql);
q.ExecuteReader();
while (q.read())
{
Sender.Items.Add(q.get(fieldName));
}
q.terminate();
}
this one function could then be used to fill every combo box you have in your application that gets it's data from a database table.
it is more direct and you specify the field you want by name. throw away third party databound controls they cause more headaches than solve problems.
Hope this is clearer for you.
helloise smitPosted Oct 29, 2009, 3:54 AM
theLizardPosted Oct 28, 2009, 6:10 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace WinUI.HelperClasses
{
class common
{
public common() //you don't need anything in the constructor
{
}
//just send the combo box you want to fill with the dataset and any other parameter value
//sending a reference to the combo box you want filled executes the actions of the combo box on your form.
//eg name.Items.Add("whatever") is the same as doing
comboBoxEditLocation.Items.Add("whatever") on the parent form.public void GetRows(DataSet ds,DevExpress.XtraEditors.ComboBoxEdit name,string tableName,int rowno)
{
foreach (DataTable dt in ds.Tables)
{
int i = 0;
foreach (DataRow dr in dt.Rows) //loop through rows
{
name.Items.Add(ds.Tables[0].Rows[i][table]);
i++;
}
}
}
}
}
Is this what you are after?
helloise smitPosted Oct 28, 2009, 7:09 AM
theLizardPosted Oct 28, 2009, 6:13 AM