I'm trying to add a second BindingList to a ListBox but can't figure out how. Here is my code.
private Entitycontext context = new Entitycontext();
private BindingList<TblProduct> products = new BindingList<TblProduct>();
private BindingList <TblProductItem> SubProduct = new BindingList <TblProductItem>();
public form1()
{
InitializeComponent();
// here in listbox i want to add the second BindingList but I don't Know how??
// Could some One help me with this please..
ListBox.DataSource = Products;
}
/// here the listbox is format as FormaLIstItem
private void FormaLIstItem(object sender, ListControlConvertEventArgs e)
{
string currentDescription = ((TblProduct)e.ListItem).Description.PadRight(25);
string currentPrice = string.Format("{0:C}"((TblProduct)e.ListItem).Price).PadRight(25);
e.Value = currentDescription + currentPrice;
string currentDescriptions = ((TblProductItem)e.Value).Description.PadRight(25);
string currentPrices = string.Format("{0:C}",((TblProductItem)e.Value).Price).PadRight(25);
e.Value = currentDescriptions + currentPrices;
}
Related
I'm trying to make that theirs a check if a textbox is empty, it wont pass the information on multiple textboxes on a button press, example:
private void button1_Click(object sender, EventArgs e)
{
if (this.Controls.OfType<TextBox>().Any(t => string.IsNullOrWhiteSpace(t.Text)))
{
// what can i put here to exclude multiple
}
else
{
//if any of thouse are empty i dont want them to do this but if they are not empty i do want
lablCinnamonset.Text = textBox1.Text;
lablMallardset.Text = textBox2.Text;
lablAxisdeerSet.Text = textBox3.Text;
lablBlackbuckSet.Text = textBox4.Text;
lablMuledeerSet.Text = textBox5.Text;
lablReddeerSet.Text = textBox6.Text;
lablPumaSet.Text = textBox7.Text;
lablWaterbuffaloSet.Text = textBox8.Text;
lablJackrabbitSet.Text = textBox9.Text;
lablCoyoteSet.Text = textBox10.Text;
lablWhitetailSet.Text = textBox11.Text;
lablBlacktailSet.Text = textBox12.Text;
lablBlackbearSet.Text = textBox13.Text;
lablRooseveltSet.Text = textBox14.Text;
lablMooseSet.Text = textBox15.Text;
}
I don't want to do, a if statement for each textbox, it has to be a better way.
Thank you guys
Why don't just extract a method?
private static void AssignIfNotEmpty(Control target, Control source) {
if (!string.IsNullOrWhiteSpace(source.Text))
target.Text = source.Text;
}
Then use it
private void button1_Click(object sender, EventArgs e) {
AssignIfNotEmpty(lablCinnamonset, textBox1);
AssignIfNotEmpty(lablMallardset, textBox2);
AssignIfNotEmpty(lablAxisdeerSet, textBox3);
AssignIfNotEmpty(lablBlackbuckSet, textBox4);
AssignIfNotEmpty(lablMuledeerSet, textBox5);
AssignIfNotEmpty(lablReddeerSet, textBox6);
AssignIfNotEmpty(lablPumaSet, textBox7);
AssignIfNotEmpty(lablWaterbuffaloSet, textBox8);
AssignIfNotEmpty(lablJackrabbitSet, textBox9);
AssignIfNotEmpty(lablCoyoteSet, textBox10);
AssignIfNotEmpty(lablWhitetailSet, textBox11);
AssignIfNotEmpty(lablBlacktailSet, textBox12);
AssignIfNotEmpty(lablBlackbearSet, textBox13);
AssignIfNotEmpty(lablRooseveltSet, textBox14);
AssignIfNotEmpty(lablMooseSet, textBox15);
}
You may want to organize your controls e.g.
public partial class MyForm : Form {
private Dictionary<Label, TextBox> m_Correspondence =
new Dictionary<Label, TextBox>();
public MyForm() {
InitializeComponent();
m_Correspondence.Add(lablCinnamonset, textBox1);
m_Correspondence.Add(lablMallardset, textBox2);
...
m_Correspondence.Add(lablMooseSet, textBox15);
}
In this case button1_Click will be very simple:
private void button1_Click(object sender, EventArgs e) {
foreach (var pair in m_Correspondence)
AssignIfNotEmpty(pair.Key, pair.Value);
}
Supposing you can change the names of various "labl..." with something like "labl1", "labl2" or mapping them in some way (maybe using a Dictionary), then you can use the
FindName function.
Example:
for(int i=1; i<16; i++){
if( (TextBox)this.FindName("textBox" + i.ToString()).Text != ""){
//Here you can do what you want
}
}
Reference here: https://learn.microsoft.com/it-it/dotnet/api/system.windows.frameworkelement.findname?view=netframework-4.8
I'm working on a school project with C# Winforms where I have to create a vehicle sales invoice and ope a new form with information about the vehicle that was selected from a combobox. How do I get the Vehicle object or its properties based on the SelectedItem in the combobox?
The Vehicle objects are in a list which is bound to a BindingSource which is bound to the combobox.
I was able to pass static strings through to a new form in another component of this assignment, but I can't figure out how to retrieve the object information.
My list of vehicles bound to the combobox. DataRetriever is a class we were given to provide us with the Vehicle objects. They have auto-implemented properties (make, model, id, colour, etc.)
List<Vehicle> vehicles = DataRetriever.GetVehicles();
BindingSource vehiclesBindingSource = new BindingSource();
vehiclesBindingSource.DataSource = vehicles;
this.cboVehicle.DataSource = vehiclesBindingSource;
this.cboVehicle.DisplayMember = "stockID";
this.cboVehicle.ValueMember = "basePrice";
I want to be able to pass information to this form and display information about the selected vehicle with labels.
private void vehicleInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
VehicleInformation vehicleInformation = new VehicleInformation();
vehicleInformation.Show();
}
On Form_Load
List<VecDetails> lstMasterDetails = new List<VecDetails>();
private void frmBarcode_Load(object sender, EventArgs e)
{
VechicleDetails();
BindingSource vehiclesBindingSource = new BindingSource();
vehiclesBindingSource.DataSource = lstMasterDetails;
this.comboBox1.DataSource = vehiclesBindingSource;
this.comboBox1.DisplayMember = "stockID";
this.comboBox1.ValueMember = "basePrice";
}
In VechicleDetails() Method i'm just generating the sample value so i can i them to ComboBox
private void VechicleDetails()
{
//Sample Method to Generate Some value and
//load it to List<VecDetails> and then to ComboBox
for (int n = 0; n < 10; n++)
{
VecDetails ve = new VecDetails();
ve.stockID = "Stock ID " + (n + 1).ToString();
ve.basePrice = "Base Price " + (n + 1).ToString();
lstMasterDetails.Add(ve);
}
}
Now on comboBox1_SelectedIndexChanged event i'm getting the value of the item selected
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string strStockId = comboBox1.Text.ToString();
string strBasePrice = (comboBox1.SelectedItem as dynamic).basePrice;
label1.Text = strStockId + " - " + strBasePrice;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
How can i get the value of outlet ID it on a comboBox?
here is my code to getting the values from Database and store it on a combobox.
public partial class Addstock : Form
{
String connectionString = ConfigurationManager.ConnectionStrings["TCConnectionString"].ConnectionString;
List<BOStockTransfer> StockList = new List<BOStockTransfer>();
int updateIndex = -1;
public Addstock()
{
InitializeComponent();
}
private void Addstock_Load(object sender, EventArgs e)
{
loadstock();
GetOutlets();
Getproduct();
GetGetWH();
cmdoutletID.Visible = false;
lbloutid.Visible = false;
cmdwh.Visible = false;
lblwh.Visible = false;
}
private void GetOutlets()
{
BOStockTransfer obj_StockTransfer = new BOStockTransfer();
DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);
if (ds_OutletList.Tables[0].Rows.Count != 0)
{
cmdoutletID.DataSource = ds_OutletList.Tables[0];
cmdoutletID.DisplayMember = "outletId";
}
}
Thank you for your help!
you are setting :
cmdoutletID.Visible = false;
what makes the combobox invisible
you have to set it as follows :
cmdoutletID.Visible = true;
After you added the images , the column name is outletID and Not outletId
so change your code as follows :
private void GetOutlets()
{
BOStockTransfer obj_StockTransfer = new BOStockTransfer();
DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);
if (ds_OutletList.Tables[0].Rows.Count != 0)
{
cmdoutletID.DataSource = ds_OutletList.Tables[0];
cmdoutletID.DisplayMember = "outletID";
cmdoutletID.ValueMember = "outletID";
cmdoutletID.Enabled = true;
}
}
The combo box has two properties which determine where it loads data from:
DisplayMember
ValueMember
The "ValueMember" property defines which named field populates the ListItem's "ValueProperty". That way, when you do combobox.SelectedItem.Value, you will get the value stored in the named field you specified for "ValueProperty".
Assuming that you are certain that your query is returning rows, perhaps try adding the items "manually" rather than relying on automatic databinding.
private void GetOutlets()
{
BOStockTransfer obj_StockTransfer = new BOStockTransfer();
DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);
cmdoutletID.DisplayMember = "outletId";
cmdoutletID.ValueMember = "pkID";
cmdoutletID.BeginUpdate();
try
{
cmdoutletID.Items.Clear();
foreach (var row in ds_OutletList.Tables[0].Rows)
cmdoutletID.Items(new { outletid = row["outletid"], pkID = row["primaryKeyIDFieldName"] });
}
finally { cmdoutletID.EndUpdate(); }
}
Try this:
BOStockTransfer obj_StockTransfer = new BOStockTransfer();
DataSet ds_OutletList = obj_StockTransfer.GetOutlets(connectionString);
if (ds_OutletList.Tables[0].Rows.Count != 0)
{
cmdoutletID.DataSource = ds_OutletList.Tables[0];
cmdoutletID.DisplayMember = "outletname";
cmdoutletID.ValueMember = "outletId";
}
To capture selected value, for example on button click:
protected button1_Click(object o, EventArgs e)
{
var selectedId = cmboutletID.SelectedValue;
MessageBox.Show(selectedId);
}
I have XtraTabControl with two pages, both of them has one LookUpEdit,
when page load which on the secondpage does not work,
void Frm1_Load(object sender, EventArgs e)
{
lookUpEditA.Properties.DataSource = datasource. . . . .
lookUpEditA.Properties.ValueMember = "ID";
lookUpEditA.Properties.DisplayMember = "xxxx";
lookUpEditA.Properties.PopulateColumns();
lookUpEditA.Properties.Columns["ID"].Visible = false;
lookUpEditB.Properties.DataSource = datasource. . . . .
lookUpEditB.Properties.ValueMember = "ID";
lookUpEditB.Properties.DisplayMember = "xxxx";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["ID"].Visible = false;
}
I can see the issue only with setting visibility of 'ID' column on second LookUpEdit.
The reason of this issue is that the LookUpEdit can't operate with datasource representation (perform populating columns, operating with column's visibility and etc.) until it's handle been created. The second LookUpEdit will create it's handle only when the second tab page has been shown.
To avoid the issue you can use the following approach:
if(!lookUpEditB.IsHandleCreated)
lookUpEditB.HandleCreated += lookUpEditB_HandleCreated;
else InitLookUpEditDataSource();
//...
void lookUpEditB_HandleCreated(object sender, EventArgs e) {
lookUpEditB.HandleCreated -= lookUpEditB_HandleCreated;
InitLookUpEditDataSource();
}
void InitLookUpEditDataSource() {
lookUpEditB.Properties.DataSource = this.categoriesBindingSource;
lookUpEditB.Properties.DisplayMember = "CategoryName";
lookUpEditB.Properties.ValueMember = "CategoryID";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["CategoryID"].Visible = false;
}
As #DmitryG said, you can not use lookUpEditB.Properties.PopulateColumns() statement until control's UI handlers are not created.
As per my understanding these are created only when the second tab page shown. Except creating conditional statement to create handlers etc, you can use the XtraTabControl.SelectedPageChanged Event where you can bind the data source of the lookUpEditB by checking condition that XtraTabControl.SelectedTabPage Property is set with Page2 which contain the lookUpEditB.
Check the tested Code Snippet below:
public partial class TabControlTest : Form
{
List<Category> dataSource = new List<Category>();
public TabControlTest()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
dataSource.Add(new Category { ID = i + 1, Name = "Category" + (i + 1) });
}
}
private void TabControlTest_Load(object sender, EventArgs e)
{
lookUpEditA.Properties.DataSource = dataSource;
lookUpEditA.Properties.ValueMember = "ID";
lookUpEditA.Properties.DisplayMember = "Name";
lookUpEditA.Properties.PopulateColumns();
lookUpEditA.Properties.Columns["ID"].Visible = false;
}
private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
{
if (xtraTabControl1.SelectedTabPage == xtraTabPage2)
{
lookUpEditB.Properties.DataSource = dataSource;
lookUpEditB.Properties.ValueMember = "ID";
lookUpEditB.Properties.DisplayMember = "Name";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["ID"].Visible = false;
}
}
}
Hope this help.
I generated Personel[] type array from Biz.Bal.GetPersonelById("1") but if I want to add winforms, I do that like below. Is there a simpler method? Like GridView.dataSource=myArray?
private void Form1_Load(object sender, EventArgs e)
{
Form_init();
Model.Personel[] list = new Model.Personel[0];
list = Biz.BAL.GetPersonelByID("1");
dataGridView1.Rows[0].Cells[0].Value = list[0].ID;
dataGridView1.Rows[0].Cells[1].Value = list[0].Ad;
dataGridView1.Rows[0].Cells[2].Value = list[0].SoyAd;
dataGridView1.Rows[0].Cells[3].Value = list[0].Maas;
dataGridView1.Rows[0].Cells[4].Value = list[0].Departman;
}
Use databindings: Example here