Can any of you on stackoverflow explain me how come my combobox is not displaying any items. I have a combobox that get values from another so it only shows specific items, in this case the items are seat rows. I have tried to set a breakpoit and see if its null, and its not it gets the right value. But for some reason the combobox dont display the items ? can any of you help me.
Here is the first combobox selectIndex event:
private void cbBookedSeatMovDate_SelectedIndexChanged(object sender, EventArgs e)
{
ServiceReferenceMovieRunTime.MovieRunTimeServiceClient movRunService = new ServiceReferenceMovieRunTime.MovieRunTimeServiceClient();
string _selectedMovName = Convert.ToString(cbBookedSeatMovInfo.SelectedValue);
string _selectedMovDate = Convert.ToString(cbBookedSeatMovDate.SelectedValue);
cbBookedSeatMovTime.DataSource = movRunService.GetRunTimeOnMovNameAndDate(_selectedMovName, _selectedMovDate);
cbBookedSeatMovTime.ValueMember = "id";
cbBookedSeatMovTime.DisplayMember = "startTime";
}
and here is the combobox event thats not working right:
private void cbBookedSeatMovTime_SelectedIndexChanged(object sender, EventArgs e)
{
ServiceReferenceMovieRunTime.MovieRunTimeServiceClient movRunService = new ServiceReferenceMovieRunTime.MovieRunTimeServiceClient();
ServiceReferenceSeats.SeatsServiceClient seatService = new ServiceReferenceSeats.SeatsServiceClient();
string _selectedMovTime = Convert.ToString(cbBookedSeatMovTime.SelectedValue);
string _selectedMovName = Convert.ToString(cbBookedSeatMovInfo.SelectedValue);
string _selectedMovDate = Convert.ToString(cbBookedSeatMovDate.SelectedValue);
string _runTimeId = Convert.ToString(movRunService.GetRunTimeOnNameDateAndTime(_selectedMovName, _selectedMovDate, _selectedMovTime));
cbRow1.DataSource = seatService.GetRowsOnRunTime(Convert.ToInt32(_runTimeId.First()));
cbRow1.ValueMember = "id";
cbRow1.DisplayMember = "rowId";
}
Here is the LINQ query:
public List<TheaterSeat> GetRowsOnRunTime(int RunTime)
{
var queryResult = (from x in db.TheaterSeats
where x.runTime == RunTime
select x);
return queryResult.ToList();
}
Hope you guys can help me out!
Related
I have to show the grid selected row value into textboxes.I'm using this code, but it's not working. Any help will be appreciated.
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.SelectedRows[0].Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.SelectedRows[1].Cells[1].Value.ToString();
txtBoxName.Text = CRUD.SelectedRows[2].Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.SelectedRows[3].Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.SelectedRows[4].Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.SelectedRows[5].Cells[5].Value.ToString();
}
You're indexing your selected rows. if you have less than 6 selected rows, then you will go out of bounds. You probably want to fetch data from only one row. Check if only one row is selected and then use index 0. Make sure you set CRUD.MultiSelect = false
Alternatively use CRUD.CurrentRow which will only ever get you one row.
Form.Designer.cs:
this.CRUD.SelectionChanged += new System.EventHandler(this.CRUD_SelectionChanged);
Form.cs:
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.CurrentRow.Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.CurrentRow.Cells[1].Value.ToString();
txtBoxName.Text = CRUD.CurrentRow.Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.CurrentRow.Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.CurrentRow.Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.CurrentRow.Cells[5].Value.ToString();
}
I have this ListBox that displays items I dragged from my DataGridView. Items in ListBox displays their MenuCode. What I want to happen is I want to show the MenuPrice of each item I have on ListBox on a TextBox. I tried to do this but the MenuCode displays on that TextBox. Please see the image below.
Is it possible to have the MenuPrice displayed on that TextBox? I have done this code but I dont think this is right.
private void menuDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) //this code triggers when I dragged an item from datagridview to listbox.
{
menuDataGrid.DoDragDrop(menuDataGrid.CurrentRow.Cells[3].Value.ToString(), DragDropEffects.Copy);
}
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
pricetxtbox.Text = menuListBox.SelectedItem.ToString();
}
private void ShowDataToGrid() //datagrid code
{
db_connection();
MySqlDataAdapter datagrid = new MySqlDataAdapter();
string selectAll = "SELECT * FROM Menu";
datagrid.SelectCommand = new MySqlCommand(selectAll, connect);
DataTable tbl = new DataTable();
datagrid.Fill(tbl);
BindingSource bs = new BindingSource();
bs.DataSource = tbl;
menuDataGrid.DataSource = bs;
menuDataGrid.Columns[6].Visible = false;
menuDataGrid.Columns[7].Visible = false;
}
I'm going to assume that the MenuPrice is also present on the GridView at a different cell, say Cells[4].
Then you can do something like this
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
foreach (DataGridViewRow row in menuDataGrid.Rows)
{
if (row.Cells[3].Value.ToString().Equals(menuListBox.SelectedItem.ToString()))
{
pricetxtbox.Text = row.Cells[4].Value.ToString();
break;
}
}
}
You should pack your data into an object class with properties you want to display and hold. For example:
public class MenuItem()
{
public float MenuPrice { get; set; }
public string MenuCode { get; set; }
}
If you store data of type MenuItem in your ListView, you will be able to do:
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) textbox
{
var item = menu.ListBoxItem.SelectedItem as MenuItem;
if(item == null) return;
pricetxtbox.Text = item.MenuPrice;
}
This would be one way to do it, if you want to enhance your code you should probably consider using databinding for you textboxes.
I using a System.Windows.Forms.ComboBox and I'm getting some weird unexpected behavior. In c#, I am dynically adding a few comboBoxes to my form and binding them to a list. The only fields I'm setting are DataSource,ValueMember, and DisplayMember. For some reason, after I bind to the list, the first item is selected. I cannot figure out what's going on.
My code looks like this:
Control c = new System.Windows.Forms.ComboBox();
Looping through all my controls,
if (c?.GetType() == typeof (ComboBox))
{
BindComboBox((ComboBox) c);
}
private void BindComboBox(ComboBox sender)
{
DataTable table = DataGateway.GetTables(1);
sender.DataSource = table;
sender.ValueMember = "ID";
sender.DisplayMember = "Name";
//sender.SelectedIndex = -1; I tried with this and without this
}
I also tried a second method but the same thing is happening -
private void BindComboBox(ComboBox sender)
{
List<string> hiStrings = new List<string>() {"hi", "hello", "whats up"};
sender.DataSource = hiStrings;
}
First value is selected but is default behavior when You don't nothing change in ComboBox class settings
Modify the second method:
private void BindComboBox(ComboBox sender)
{
List<string> hiStrings = new List<string>() {"hi", "hello", "whats up"};
sender.DataSource = hiStrings;
sender.SelectedItem = null;
}
And this give You empty ComboBox on Form
This solution is working and I tested.
Some helps links:
How to deselect the text of a combobox
Test method:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_MouseLeave(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
this.TestMethod(comboBox);
}
private void TestMethod(ComboBox d)
{
var list = new List<string>() {"hi", "hello", "whats up"};
d.DataSource = list;
d.SelectedItem = null;
}
}
I have a form that allows a user to add players to a roster, by entering the player name and selecting, from a combo box, the division to which the player belongs.
When time comes to add the player to my TreeView control, the node that should display the division selected displays this text instead: System.Data.DataRowView
I got the code to implement this through MSDN here: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx
Here's the code in the load function of the form, to fill the combo box:
private void frm_add_players_Load(object sender, EventArgs e)
{
Divisions divs = new Divisions();
Players players = new Players();
DataTable dtDivisions = divs.GetActiveDivisions(); //divisions combo box
DataTable dtPlayers = players.GetPlayersByTourID(this.tourID);
//set the forms datatable
this.dt_players = dtPlayers;
//fill the combo box
this.cmbo_divisions.DataSource = dtDivisions;
this.cmbo_divisions.DisplayMember = "title";
this.cmbo_divisions.ValueMember = "ID";
this.cmbo_divisions.SelectedIndex = -1;
this.cmbo_divisions.Text = "Select a Division";
//set treeview imagelist
this.tview_roster.ImageList = tview_imagelist;
this.tview_roster.ImageIndex = 1; //division icon
//fill treeview
foreach (DataRow dr in dtPlayers.Rows)
{
FillPlayerTreeview(dr);
}
//expand treeview
this.tview_roster.ExpandAll();
this.ActiveControl = this.txt_player_name;
}
Here I call the function to add the player to the TreeView:
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, selItem.ToString());
}
And here is the function that adds the player:
private void AddPlayerToTreeView(string playerName, string division)
{
TreeNode[] tns = this.tview_roster.Nodes.Find(division, false); //try to find the division, if exists
TreeNode tn = new TreeNode();
if (tns.Length > 0) //division exists - add player
{
tn = this.tview_roster.Nodes[tns[0].Index].Nodes.Add(playerName, playerName);
tn.ImageIndex = 0; //player icon
}
else //division doesn't exist - add division, then add player
{
tn = this.tview_roster.Nodes.Add(division, division);
tn.ImageIndex = 1; //division icon
AddPlayerToTreeView(playerName, division);
}
}
And the result is this:
I'm not sure why it won't work.. and I'm at a loss. Any help would be appreciated.
Well, well... maybe something like the following.
Access the combo's data source, which is a DataTable, and extract selected row and column value using selected index. Maybe add some error handling, too.
private void btn_add_Click(object sender, EventArgs e)
{
var data = cmbo_divisions.DataSource as DataTable;
var row = data.Rows[cmbo_divisions.SelectedIndex];
var selected = row["title"].ToString();
AddPlayerToTreeView(txt_player_name.Text, selected);
}
Try this :
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.SelectedItem as string);
}
ToString() will get the type name, but in that case the SelectedItem is a string.
Try with:
private void btn_add_Click(object sender, EventArgs e)
{
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.Items[cmbo_divisions.SelectedIndex].Text);
}
EDIT: Updated to a better way
In this form user update data about Client in client table. When form is loaded it fills textboxes whis data from client table by value in combobox. When user change value in combobox data int textboxes must change.
But when I am trying to get Client ID from selected value in combobox event and assign it to getClientID variable compiler gives me error:
System.FormatException : Input string was not in a correct format.
private void ClientUpdateForm_Load(object sender, EventArgs e)
{
ClientComboBox.DataSource = AgencyContext.Client.ToList();
ClientComboBox.DisplayMember = "ClientName";
ClientComboBox.ValueMember = "ClientID";
Invalidate();
int getClientID = Convert.ToInt32(ClientComboBox.SelectedValue.ToString());
var fillTextBoxes = (from t in AgencyContext.Client where t.ClientID == getClientID select t).Single();
ClientNametextBox.Text = fillTextBoxes.ClientName;
ClientBirthtextBox.Text = fillTextBoxes.Birth.ToString(CultureInfo.CurrentCulture);
ClientPhonetextBox.Text = fillTextBoxes.Phone.ToString();
ClientPassporttextBox.Text = fillTextBoxes.Passport;
AddresstextBox.Text = fillTextBoxes.Address;
ClientStatetextBox.Text = fillTextBoxes.State;
ClientCitytextBox.Text = fillTextBoxes.City;
}
private void SaveNewClientButton_Click(object sender, EventArgs e)
{
}
private void ClientComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var getClientID = Convert.ToInt32(ClientComboBox.SelectedValue.ToString());
var fillTextBoxes = (from t in AgencyContext.Client where t.ClientID == getClientID select t).Single();
ClientNametextBox.Text = fillTextBoxes.ClientName;
ClientBirthtextBox.Text = fillTextBoxes.Birth.ToString(CultureInfo.CurrentCulture);
ClientPhonetextBox.Text = fillTextBoxes.Phone.ToString();
ClientPassporttextBox.Text = fillTextBoxes.Passport;
AddresstextBox.Text = fillTextBoxes.Address;
ClientStatetextBox.Text = fillTextBoxes.State;
ClientCitytextBox.Text = fillTextBoxes.City;
}
During the load event ... there is no selected value ... it is null and cannot be converted to int. You have to wait until after databinding before you pull the selected value.