C# - Update a subitem within a listview - c#

I am working on an application where users enter information that is then added to a listview. That works great. The only problem is, the application connects to a web site that updates the "Points" field in that listview for each account. I am not sure how I can update a single subitem within a listview.
Here is an example screenshot:
How can I select a specific subitem in a specific row to update?

Ok, I'm going to assume Windows Forms.
WinForms' ListViewItem class has a Name property, which you can use to look up a specific item in a list. So as you populate the list, assign a unique value to the Name of each:
var item = new ListViewItem("Text");
item.Name = "foo"; // some unique id string
listView1.Items.Add(item);
That way you can locate the item in the ListView later, using its Items.Find method.
var fooItem = listView1.Items.Find("foo", false);

To expand on Matt's answer, it looks like each row has a unique email address, so you could assign that as the Name property for each ListViewItem. Once you've located the row to update using the Find method, you can update that row's Points like this:
fooItem.SubItems[2] = "450";

Related

Select only part of combobox string or store two values per index possible?

Here's what I have so far:
I have a ComboBox that I'm filling via pulling some playlists from a C# Spotify API.
I get the values - playlist.Id and playlist.Name from a playlist object.
Each playlist populates its Id and Name into a row in my ComboBox dropdown so the user can see a list of their personal playlists.
Next, the user chooses their selected playlist in the ComboBox and can pull a ListView of all the tracks (artists etc).
This step requires selecting the user's ComboBox selection but I can only use the playlist.Id value. However, I need to include the playlist.Name in the box so the user knows what playlist to select.
Obviously, this is pulling the name AND Id from the selected ComboBox option and I can't use these two values for the tracklist pull. I only want the Id string.
Is there any way to select only part of the ComboBox string (just the ID) or assign a value and a text separately to one row in my ComboBox?
Appreciate I'm probably doing this a silly way.
I've also tried:
Creating a separate array with just the playlist Id's and trying to use the selected index of ComboBox to match with the array index (only containing the id and not the name).
userPlaylists.Items.ForEach(playlist =>
{
playlistArray[i] = playlist.Id;
comboPlaylists.Items.Add(playlist.Name + playlist.Id);
i++;
});
There are ways to extract part of the ComboBox text but this isn't the right way. Instead, you should use Data Binding. Set the DataSource, DisplayMember, and ValueMember properties of the ComboBox so that you can later access the SelectedValue which would be the ID.
Here's an example:
comboPlaylists.DisplayMember = "Name";
comboPlaylists.ValueMember = "Id";
// I'm assuming that `userPlaylists.Items` is a generic list.
// Otherwise, you might want to use `userPlaylists.Items.ToList()` instead.
comboPlaylists.DataSource = userPlaylists.Items;
Now, when you want to get the Id of the selected PlayList, you can use something like this:
var id = (int)comboPlaylists.SelectedValue; // Assuming the ID is an integer.

Select a row in listview

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.
I have two list views on my page and a remove button that works for both.
Problems.
I am not able to select a row in both the list view when I run my program, may be some property needed for it?
If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
I have three columns and have bound the data by using the code below.
listView1.Columns.Add("ID",20);
listView1.Columns.Add("Name",40);
listView1.Columns.Add("Mobile",40);
foreach (var item in dataList)
{
newItem = new ListViewItem();
newItem.SubItems.Add(item.ID.ToString());
newItem.SubItems.Add(item.Name);
newItem.SubItems.Add(item.Mobile.ToString());
listView1.Items.Add(newItem);
}
but the ID column is left blank and the data starts to bind in these sense.
ID Name Mobile
1 abc
2 xyz
So how do I properly show the data?
Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?
See ListView.FullRowSelect property.
See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
If you want to delete the column, just remove it from the columns collection.
Set
listView1.Items[index].Selected=true;

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.
Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?
You can set the HeaderStyle member of the ListView to None.
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
Checkout the ListView HeaderStyle property. It has the following options:
None
Nonclickable
Clickable
From MSDN:
The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns
You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.
You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.
Easy way is using the ColumnWidthChanging event
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}
I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

Read all item from listbox in windows application

I have two listbox in windows Form c#, the first one has all valid rooms at this time & the 2nd has the current rooms reserved to the Guest, & the user can change this data by transfer between this listboxex.
as example:
list1: 1d ,5d ,6d , 1r, 12r
list2: 2d, 4d
the user can delete 2d and put instead of it 6d.
what I need how can I read all data from list2 after user update it, & use this to update my table in sql server.
To copy a selected item from one list box to another you could use the following code. Ensure that the SelectionMode for the listbox is Single. This bit of code reads the selected item, adds it to the other ListBox and removes it from the current ListBox. You do not need to cast it to a string as there is no reason to read the actual data. This also allows you to have complex objects as listBoxItems.
object obj = listBox1.SelectedItem;
listBox2.Items.Add(obj);
listBox1.Items.Remove(obj);
If you link this to left/right buttons, you can ensure that the user is not able to duplicate rooms or add room numbers which are outside the scope.
Try code below:
string TimeslotItems = "";
foreach (ListItem item in listBox2.Items)
{
TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
}
There is no 'item added' event on ListBox. When you use a BindingList as your datasource you can register events to the BindingList itself and respond to those. If you want an item added event on the List itself look at Item added events question for ListBox
To iterate over your list items:
foreach (string s in listBox1.Items)
{
string name = s;
}
In regards to your database transfer, what way do you want to go? Using Entity Framework? Direct SQL execution?

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

Categories

Resources