I have a comboBox and I can save an item to it in runtime, but, every time I close the application the items disappear, I have searched for it, but, i couldn't find anything to do so.
I have the following code:
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
It's a Windows form application, it uses C# and SQL for managing a DB and it's tables, I would like to use o comboBox so the user don't need to type the same thing over and over when is going to put a value.
Is there a way to "permanently" save the items? Maybe using an SQL DB ou something like that?
You can either define the ComboBox items in a database table or in your application. If you're looking for a quick solution and know what all of the ComboBox options should be, you can define them in an array and then assign that array to the ComboBox DataSource:
string[] items = new string[] {"--Select--", "Item1", "Item2"};
comboBox1.DataSource = items;
comboBox1.SelectedIndex = 0;
Related
I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).
In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
// Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}
I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.
However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.
If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.
I have a datagridview that has a list of objects built from a DB bound to it. The default sorting of a datagridview wasnt working and I find out that its because a list doesn't support sorting.
I really don't care what kind of collection I use as long as I can
A: sort the list by column on header click
B: search the list by a specific column
C: Get the object back when the user selects a row.
I have C working fine with a list but A and B wont work.
private BindingSource rollingStockSource = new BindingSource();
dgvInventoryList.AutoGenerateColumns = false;
List<RollingStock> rollingStockList =RollingStockDAO.GetRollingStocks();
rollingStockSource.DataSource = rollingStockList;
dgvInventoryList.DataSource = rollingStockSource;
This is how im setting up my datagridview now. Im setting my columns and tying them to object attributes in the form editor rather than in the code with the DataPropertyName field.
Is there no list that comes with sorting/searching by default?
I have built a program in WPF using Visual Basic that allows users to select from a series of items. The list of items may change as I add more items or remove older ones. Originally, I would just add and remove these items in the project code, and rebuild the executable.
Over time, however, the list of items grew and became tedious and impractical to maintain in the code. It occurred to me that it would be easier to store the items in a database table which the program can read, and the list can just be updated by adding and removing items from this database.
I have the database built, and I've added it as a Data Source. I have the data connection set up and everything is ready to go.
What I need to know is how to set the Content property of a label to a cell from a table in the database. I want to do this in the codebehind, not in markup (XAML).
My guess is that I will need to set up a sort of query with a for loop to find the cell I want.
The name of the database is ItemsDB. It contains a table called ItemsTable, and the fields in the table have a unique ID (key) with an AutoNumber data type. The column containing the data I want is named ItemLabel.
So, to sum it all up, I want a label to show the content from a single cell in the database, specified in the codebehind. Either C# or VB is fine.
EDIT
I guess I should've mentioned that the labels themselves actually function as buttons, and I don't really want to display a ListView if I don't have to. I know it's not helpful to not have any code posted, but I don't even know where to start, so there's no code to post.
Took a while longer than I expected, hope this is still useful. So, what you want to do is iterate through a collection and create a new Label for each row.
You will need a container for the Labels, I used WrapPanel, but you can use any Panel you want. I'm replacing your Data Source with a simple DataTable, you'll get the point from this:
foreach (DataRow row in YourItemsTable.Rows)
{
Label newLabel = new Label();
newLabel.Content = row["ItemLabel"].ToString();
// If you need some events, attach the handlers here
yourParentContainerPanel.Children.Add(newLabel);
}
This should be all you need.
I have a List of FLECk.IWebSocketConnection
I am trying to display this list in a listbox using the IWebSocketConnection.ConnectionInfo.ClientIpAddress property, but I must be doing something wrong
Sockets= new List<IWebSocketConnection>();
ListBox1.DataSource=Sockets;
ListBox1.DisplayMember="ConnectionInfo.ClientIpAddress";
When I add to Sockets using
Sockets.Add(socket);
I have to rebind the datasource
ListBox1.DataSource=Sockets;
ListBox1.DisplayMember="ConnectionInfo.ClientIpAddress";
But the display in the lisbox is "Fleck.WebSocketConnection" as opposed to the expected Ip number.
Just to clarify, The write number of connections are listed. I am simply getting the wrong display information. If I create a label and set its text to
label1.Text =((IWebSocketConnection)SocketList.Items[0]).ConnectionInfo.ClientIpAddress;
it displays the IP number
Can someone tell me what I am doing wrong here.
There may be an alternative approach, but one thing you could do is flatten out the list members you need to display and bind to that.
For example:
var SocketInfos = sockets.Select((s) => new {ClientIpAddress = s.ConnectionInfo.ClientIpAddress,
Socket = s}).ToList();
ListBox1.DataSource=SocketInfos ;
ListBox1.DisplayMember="ClientIpAddress";
You'd need to regenerate the list whenever you change the source list though.
This way you can still get back to the selected Socket Instance from the selected ListBox Item.
I am using C#, Visual Studio 2005
I have created textbox on runtime/dynamic way and inserted/saved sql data on database but the problem is how to retrieve the same data on same way for edit / update.
e.g.
col1 col2
mahesh 1000
kirti 2000
The above data is stored in sql server. I don't know how to retrieve the same on dynamic way by creating dynamic textbox and retrieve the same.
Suggest proper code please.
Create datasource. Read the data into the datasource. Add the controls and bind the dynamic controls to that data source.
I'm not quite sure i follow 100% but you could have a drop down that is populated from the database, Select the item in this drop down, Enter the details in to your textboxes and have a button to activate the update.
In steps
Get your data into a local list.
Populate the drop down using this list.
You now still have your local list with the appropriate data.
Get the user to select an item from the drop down.
Use the drop downs selected index to get the item from the list.
Change the values within this item and commit to the database
I would have thought it would be far easier using the appropriate control for displaying the information such as a listview or datagrid
List<String> objInformationList = new List<String>();
objInformationList = "Your database query function";
foreach(String objCurrentString in objInformationList){
// Create your text box
// Set your text within your textbox
// Add the text box to the screen or a table structure
}
I personally would be doing this another way