I want to be able to edit the items that I have added to my listview.
The listview items were added through a textbox, datetimepicker, and a combobox.
The listview has three columns. What I want is: When I click on the listview item, (the listview selects all the columns) and then click on the edit button, then the textbox is replaced with column one, the datetimepicker is replaced with the date of column 2, and the combobox is replaced with column 3.
Then I can edit the textbox, date, or the combobox items and when I click the save button, then the listview item is updated.
Since you want to use separate edit controls and buttons to update there is no need to overlay the listview subitems with controls.
Here is example code to load the controls from the 1st selected item and to update that Item in the ListView:
private void lv_edit_SelectedIndexChanged(object sender, EventArgs e)
{
if (lv_edit.SelectedIndices.Count > 0)
{
var lvi = lv_edit.SelectedItems[0];
tb_col1.Text = lvi.SubItems[0].Text;
date_col2.Value = Convert.ToDateTime(lvi.SubItems[1].Text);
combo_col3.SelectedIndex = combo_col3.FindStringExact(lvi.SubItems[2].Text);
}
}
private void cb_updateItem_Click(object sender, EventArgs e)
{
if (lv_edit.SelectedIndices.Count > 0)
{
var lvi = lv_edit.SelectedItems[0];
lvi.SubItems[0].Text = tb_col1.Text;
lvi.SubItems[1].Text = date_col2.Value.ToString("dddd, dd. MMMM yyyy");
lvi.SubItems[2].Text = combo_col3.SelectedItem.ToString();
}
}
Note that SubItems[0].Text is the same as Items[0].Text.
Also note that the code assumes that all items have all three fields and their values are all valid, ie that the conversion and the find will work.
Related
I have a form with some textboxes and some other items specially combobox.
When I selected an item from combobox that has label "Land" in screenshot, I can't do anything with other elements in form!
that combobox turns blue (means selected and focused) and user cant fill other boxes!
private void cb_land_DropDownClosed(object sender, EventArgs e)
{
foreach (Lands j in lands)
{
if (j.landName.Equals(cb_land.Text))
{
tb_countrykz.Text = j.landKZ;
break;
}
}
}
code for fill combobox from database :
this.länderTableAdapter.Fill(this.test_tab.länder);
I have 3 other combobox in my form and they work fine.
this combobox locks other boxes and does not let user to click on any other elements in form.
I am populating a list-view with passwords.
Then I want to take the selected items text and pass it to a text box when it is clicked.
So far I have:
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem = passwordListView.SelectedItems[0];
passwordTextBox.Text = listViewItem.Text;
}
It works the first time I press it and it populates the textbox but then if I click a different password in the list-view it throws an exception.
Have I left out something blatantly obvious?
When the Selected Index is changed in a winforms ListView the first event is for the item that has now been deselected. So at that point SelectedItems is empty.
Check for this through if (passwordListView.SelectedItems.Count == 0) return;
After this you will get a second event, which will be for the new selection, and you can act on this.
Btw, you don't need to make a new ListViewItem as you are in your snippet, this will save the extra unnecessary creation:
ListViewItem listViewItem = passwordListView.SelectedItems[0];
Maybe you could try this :
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
if(passwordListView.SelectedItems.Count > 0)
passwordTextBox.Text = passwordListView.SelectedItems.First().Text;
}
I've got a form that contains a listview which pulls in ticket info from a database. The database objects are all abstracted into a class library. There is a tabpage below the listview which displays various details of the tickets.
My problem is that I've implemented a search at the top of this form which isn't updating that tabpage, only the listview gets updated. After typing in keywords the listview refreshes properly and any items that dont' contain the keywords are removed until the text from the search box is cleared. But I can't get the tabpage to exhibit the same behavior. The tabpage still always contains all tickets.
For example, if I were to search for something where only 1 ticket was returned in the listview and say that ticket was the 10th ticket on record; the tabpage would show me details for the very first ticket. How can I get the tabpage to exhibit the same behavior as my listview after a search is made?
The tabpage currently gets filled with this function:
private void FillTicket()
{
try
{
if (listView1.SelectedIndices.Count > 0)
{
CTicket thistkt = comp.Tickets[listView1.SelectedIndices[0]];
dedit1.DocumentHTML = thistkt.LineItems.GetCombinedProblem();
dedit2.DocumentHTML = thistkt.LineItems.GetCombinedResolution();
lvAssignmentHistory.Items.Clear();
foreach (CInc_AssignmentHistory a in thistkt.AssignmentHistory)
{
ListViewItem itm = new ListViewItem();
itm.Text = a.pAsgn_Datetime.ToString();
itm.SubItems.Add(a.pAsgn_Group_fr);
itm.SubItems.Add(a.pAsgn_from);
itm.SubItems.Add(a.pAsgn_Group_to);
itm.SubItems.Add(a.pAsgn_to);
itm.SubItems.Add(a.pChanged_By);
lvAssignmentHistory.Items.Add(itm);
}
when this is called:
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//FillTicket();
if (txtBox_TicketSearch.Text != "")
{
FillTicketNothing();
}
else
{
FillTicket();
}
It seems to me you only update the tab page if the user selects different items in your ListView.
If your listView1_SelectedIndexChanged method is only a handler for the ListView.SelectedIndexChanged event, it is only called when the selection in listView1 changes, not when it's content is changed (without changing selection).
So you should call FillTicket when you changed the content of listView1.Items after your search.
Also, your FillTicket method only updates the tabpage if there are selected items in your ListView:
if (listView1.SelectedIndices.Count > 0)
I don't know if there is an else-branch for that if. If not, there will nothing change on your tab page if no items were selected. You may want to use listView1.Items.Count.
i have one combobox and one listview
i want to select item of listbox and click on one clumn of listview multi clumn
then clumn name that i clicked be equal whit item name of listbox
tanks for answer
i want to select one item of listbox,then click on one clumn of listview's clumns,
then this clumn name be equal to selected item in listbox
private void DataValuelst_ColumnClick(object sender, ColumnClickEventArgs e)
{
DataValuelst.Columns[?].Text = Schemacmb.SelectedItem.ToString();
}
the index of clumn that i clicked must be replace whit ?
You can use the SelectedIndex property... So it should be something like this...
private void DataValuelst_ColumnClick(object sender, ColumnClickEventArgs e)
{
DataValuelst.Columns[Schemacmb.SelectedIndex].Text = Schemacmb.SelectedItem.ToString();
}
My windows form has an ADD button which adds a combo box to the form after each click. The problem is, i am not able to bind it to a table column at run time. Using an existing databinding source selects the same value in all the combo boxes. I am coding in C#
here is the sample code :
ComboBox ocbNext = new ComboBox();
//HAVE set the rest of the properties right, the problem is with the databinding
ocbNext.DataSource = this.dummysubjectBindingSource;
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
this.Controls.Add(ocbNext);
I added a DataSet to the solution and droped the Employees table (from Northwind) in the designer, which automatically created the employeesBindingSource. I dropped a combobox and a button on the Form and I set the DataSource and DataMember of the combo. Then I handled some events:
private void Form1_Load(object sender, EventArgs e)
{
this.employeesTableAdapter.Fill(this.dS.Employees);
}
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
ComboBox combo = new ComboBox();
combo.DataSource = this.employeesBindingSource;
combo.DisplayMember = this.dS.Tables[0].Columns[++_i].ColumnName;
combo.Location = new Point(comboBox1.Location.X, comboBox1.Location.Y + comboBox1.Height * _i);
this.Controls.Add(combo);
}
So on each click, a new combo is added onto the form dynamically right under the previous combo. The combo is also bound to the next column in the Employees table (no boundary checks however).
As you can see, this is pretty easy stuff. Hope this helps.
Okay, so here is a variation of the code that could help you with that other question you asked in the comments of this answer.
It assumes you have a Form with a button and a DataSet with table Employees. On button click it creates a combo, and fills it with data (the Name column of Employees). Each time you add a combo, it gets its own copy of the data (this is important to be able to remove items from one combo at a time). Then, every time you select a value in the combo, the combo is disabled and the other combos don't have that selected value in their list.
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
DataSet dataS = dS.Clone();
this.employeesTableAdapter.Fill((DS.EmployeesDataTable)dataS.Tables[0]);
BindingSource bindSource = new BindingSource(dataS, "Employees");
ComboBox combo = new ComboBox();
combo.Name = this.dS.Tables[0].Columns[0].ColumnName + (++_i).ToString();
combo.DataSource = bindSource;
combo.DisplayMember = this.dS.Tables[0].Columns[1].ColumnName; //This column is the Name of Employee
combo.Location = new Point(button1.Location.X, button1.Location.Y + combo.Height * _i);
combo.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
this.Controls.Add(combo);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is ComboBox && ctrl != sender && ctrl.Enabled)
{
((BindingSource)((ComboBox)ctrl).DataSource).RemoveAt(((ComboBox)sender).SelectedIndex);
}
}
((ComboBox)sender).Enabled = false;
}
This is pretty close to what you require, or easily adaptable to meet your expectations. Enjoy and please select an answer as the accepted one. Thanks!
Option 1: Fill the combobox with strings:
this.comboBox1.Items.Add("Syed");
this.comboBox1.Items.Add("Baqar");
Option 2: Fill the combobox with an array of strings:
this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" });
You need to add controls to the parent window first, and then set the data source.
ComboBox ocbNext = new ComboBox();
this.Controls.Add(ocbNext);
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
ocbNext.DataSource = this.dummysubjectBindingSource;
Should be fine if you create a new local ComboBox variable in the clickevent. If you use a global variable for the ComboBox this might explain your problems. But without a sample how you're doing it's hard to see what's really happening, so think this is just a rough guess