I want to add item into the listview, the problem I'm facing is that when I add an item that I type in on the text box. It is able to insert the value in the listview column that I want. But when it update, all the data that it loaded from the textfile is missing. Only the value that I type in is left.
Before add data:
http://www.hostpic.org/images/1508121529460086.png
After insert the data:
http://www.hostpic.org/images/1508121531010086.png
private void button2_Click(object sender, EventArgs e)
{
//add button
string s;
listView1.Items.Clear();
listView1.BeginUpdate();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
if ((comboBox1.SelectedIndex + 1) + "" == proDetail[i].id)
{
//proDetail[i].estimation = double.Parse(textBox3.Text);
proDetail[i].estimation = textBox3.Text;
s = textBox4.Text;
proDetail[i].pre = s.Split(',');
}
}
for (int j = 0; j < comboBox1.Items.Count; j++)
{
if (proDetail[j].pre != null)
{
project = listView1.Items.Add(proDetail[j].id);
project.SubItems.Add(proDetail[j].activity);
if (proDetail[j].pre.Length <= 1)
{
foreach (string words in proDetail[j].pre)
{
preValue = words;
}
}
else
{
preValue = string.Empty;
foreach (string words in proDetail[j].pre)
{
preValue += words + ",";
}
}
project.SubItems.Add(proDetail[j].estimation.ToString());
project.SubItems.Add(preValue);
}
else
{
project = listView1.Items.Add(proDetail[j].id);
project.SubItems.Add(proDetail[j].activity);
}
listView1.EndUpdate();
listView1.Refresh();
}
}
I forgot to include the
project.SubItems.Add(proDetail[j].estimation);
in the else statement.
Thanks for the help.
This is the culprit:
listView1.Items.Clear();
That has removed all existing items from the control.
Related
enter image description hereI'm trying to make a program that shows the prime numbers of the numbers added to the listbox from the textbox and then written in the listbox, with a message box, can anyone help me, where am I wrong?
private void button2_Click(object sender, EventArgs e)
int deneme = 0;
int sayilarim = Convert.ToInt32(textBox1.Text);
for (int i = 2; i < sayilarim; i++) {
if (sayilarim % i == 0)
deneme++;
}
if (deneme != 0){
listBox1.Items.Add(textBox1.Text + " Asal Sayi değildir.");
MessageBox.Show("Bu Bir Asal Sayi Değildir.");
} else {
listBox1.Items.Add(textBox1.Text + " sayidir.");
MessageBox.Show("Bu Bir Asal Sayi.");
}
textBox1.Clear();
}
MessageBox.Show(listBox1.Items.Count.ToString() + " Adet asal sayı var.");
First of all, your button2 is not getting value of Listbox1 ,
it takes value of textbox.
you have to take items of Listbox1 and put them in a list or etc.
and make your algorithm for them.
here is some sample code.
öncelikle buton 2 nin altında sayıları listbox içinden değil textbox'tan almışsın, onu düzeltmen gerek. listenin elemanlarında dönüp int bir listeye atama yapabilirsin. Bu adımları düzenledikten sonra altta verdiğim örnek kodu bir dene bakalım,
StringBuilder str = new StringBuilder();
str.AppendLine("Asal Olan Sayilar:");
List<int> lst = new List<int>(); // { 3, 4, 5, 10 };
for (int i = 0; i < ListBox1.Items.Count; i++)
{
lst.Add(Convert.ToInt32(ListBox1.Items[i].ToString()));
}
bool asalMi = true;
foreach (int value in lst)
{
asalMi = true;
for (int i = 2; i < value; i++)
{
if (value % i == 0)
{
asalMi = false;
}
}
if (asalMi)
str.AppendLine($"{value.ToString()} asaldir.");
}
MessageBox.Show(str.ToString);
Basically I have two categories:
Category - A
Category - B
I will select category - A and generate number serials from 1 to 10.
display in combobox 2 and save it.
I will then select category B and generate number serials from 1 to 10, display in combobox 2 and save it.
when I close and open back the application, I want to see only those serials generated in category A in comboBox2.
I have been trying to do it by adding a datagridview but it merges both category serials. Screenshot
Category - A -- > Serials{1,2,3,4,5,6,7,8,9,10}
Category - B -- > Serials{11,12,13,14,15,16,17,18}.
so when I select Category A, I want to see only A serials in comboBox2 and nothing else.
when I select Category B, I want to see only B serials in comboBox2 and nothing else.
private void GenSerialBookButton_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
from = int.Parse(textBox2.Text);
to = int.Parse(textBox3.Text);
result = to - from;
for (int i = 0; i <= result; i++)
{
comboBox2.Items.Add(from + i);
this.SerialBookDataBaseBindingSource.AddNew();
dataGridView1.Rows[i].Cells[1].Value = from + i;
}
MessageBox.Show("Serial Book Generated Success", "Success");
}
if (comboBox1.SelectedIndex == 1)
{
from = int.Parse(textBox2.Text);
to = int.Parse(textBox3.Text);
result = to - from;
for (int i = 0; i <= result; i++)
{
comboBox2.Items.Add(from + i);
this.SerialBookDataBaseBindingSource.AddNew();
dataGridView1.Rows[i].Cells[1].Value = from + i;
}
MessageBox.Show("Serial Book Generated Success", "Success");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Clear previous list
a: if (comboBox2.Items.Count > 0)
{
comboBox2.Items.RemoveAt(0);
goto a;
}
if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
comboBox2.Items.Add(row.Cells[1].Value);
MessageBox.Show("Adding: " + (row.Cells[1].Value.ToString()));
comboBox2.Refresh();
}
}
if (comboBox1.SelectedIndex == 1)
{
comboBox2.Items.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
comboBox2.Items.Add(row.Cells[1].Value);
MessageBox.Show("Adding: " + (row.Cells[1].Value.ToString()));
comboBox2.Refresh();
}
}
}
}
}
Here is a quick sample for you;
I tried to set a form like you (not have textboxes to decide "to and from", I gave it myself directly)
Here is the trick, I defined it on global,
List<KeyValuePair<int, string>> vals = new List<KeyValuePair<int, string>>();
In FormLoad I add 2 string categories,
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("CategoryA");
comboBox1.Items.Add("CategoryB");
}
And I have a button to generate serials,
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
for (int i = 0; i <= 10; i++)
{
string item = i + "A"; // Given"A" to seperate from each other
comboBox2.Items.Add(item);
vals.Add(new KeyValuePair<int, string>(0, item)); // CatA has 0 key value
}
MessageBox.Show("Serial Book Generated Success", "Success");
}
if (comboBox1.SelectedIndex == 1)
{
for (int i = 0; i <= 5; i++)
{
string item = i + "B"; // Given "B" to seperate from each other
comboBox2.Items.Add(item);
vals.Add(new KeyValuePair<int, string>(1, item)); // CatB has 1 key value
}
MessageBox.Show("Serial Book Generated Success", "Success");
}
}
And combobox's selectedindexchanged event, (Category's combobox)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Clear();
foreach (var item in vals)
{
if (item.Key == 0) //If Key value is 0, if it is CategoryA
{
comboBox2.Items.Add(item.Value);
// MessageBox.Show("Adding: " + (item.Value.ToString()));
comboBox2.Refresh();
}
}
}
if (comboBox1.SelectedIndex == 1)
{
comboBox2.Items.Clear();
foreach (var item in vals)
{
if (item.Key == 1) //If Key value is 1, if it is CategoryB
{
comboBox2.Items.Add(item.Value);
//MessageBox.Show("Adding: " + (item.Value.ToString()));
comboBox2.Refresh();
}
}
}
}
Outputs,
Hope Helps,
I have this two list-box In that first list-box is fill on Combo-box Selected index changed, so List-box 1 is Bounded. Now when I press the > button all selected item in List-box 1 is display in List-box 2.
But instead of Names, I get System.Data.DataRowView
so my question is I want Names instead of this System.Data.DataRowView
my code is this
private void btnSelect1ItemFrom_Click(object sender, EventArgs e)
{
if (listBoxSelectToLedger.Items.Count > 0)
{
for (int i = 0; listBoxSelectToLedger.Items.Count > i; )
{
listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
}
}
if (listBoxSelectFromLedger.SelectedItem != null)
{
** for (int i = 0; i < listBoxSelectFromLedger.SelectedItems.Count; i++)
{
listBoxSelectToLedger.Items.Add(listBoxSelectFromLedger.SelectedItems[i].ToString());
} **
}
else
{
MessageBox.Show("No item Selected");
}
* I think I am some where Wrong in Second IF Condition in my Code *
Plz Help me
Thanks in Advance
Try this...
private void button1_Click(object sender, EventArgs e)
{
if(listBoxFrom.SelectedItems.Count>0)
{
for (int x = listBoxFrom.SelectedIndices.Count - 1; x >= 0; x--)
{
int idx = listBoxFrom.SelectedIndices[x];
listBoxTo.Items.Add(listBoxFrom.Items[idx]);
listBoxFrom.Items.RemoveAt(idx);
}
}
}
Hiii.. Deep, use the below code to add ListItem.
foreach (ListItem LI in listBoxFrom.Items)
{
if (LI.Selected)
listBoxTo.Items.Add(LI);
}
To add in to 2nd listbox and remove that from the first listbox you can use below code:
int[] indices = listBoxFrom.GetSelectedIndices();
for (int i = indices.Length - 1; i >= 0; i--)
{
ListItem LI = listBoxFrom.Items[indices[i]];
listBoxTo.Items.Add(LI);
listBoxFrom.Items.RemoveAt(indices[i]);
}
put your No items selected message where you require.
I got answer of my own question.
i have to set my DataRowView object
if (listBoxSelectToLedger.Items.Count > 0)
{
for (int i = 0; listBoxSelectToLedger.Items.Count > i; i = 0)
{
listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
}
}
if (listBoxSelectFromLedger.SelectedItem != null)
{
foreach (DataRowView objDataRowView in listBoxSelectFromLedger.SelectedItems)
{
listBoxSelectToLedger.Items.Add(objDataRowView["item_name"].ToString());
}
}
else
{
MessageBox.Show("No Item selected");
}
This is how I currently add info to my listview:
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
int totItems = Seq3.Count - 1;
if (PercentPopTolerance1.Count - 1 > totItems) totItems = PercentPopTolerance1.Count - 1;
for (int i = 0; i <= totItems; i++)
{
ListViewItem lvi = new ListViewItem();
string item1 = "";
string item2 = "";
if (Seq3.Count - 1 >= i) item1 = Seq3[i].ToString();
if (PercentPopTolerance1.Count - 1 >= i) item2 = PercentPopTolerance1[i].ToString();
lvi.SubItems.Add(item1);
lvi.SubItems.Add(item2);
listView2.Items.Add(lvi);
}
}
View of empty listview:
Now how would I clear the contents of any particular column? Say I want to add to column YYMM but before I do this, I want to clear that particular column. How would this be done?
You can clear column values like this
var items = listView1.Items;
foreach (ListViewItem item in items)
{
a.SubItems["YYWW"].Text = "";
}
You should specify a column by its name (a column corresponds to a subitem in a ListViewItem), note that this Name is not ColumnHeader.Name, I mean the corresponding ListViewSubItem.Name:
public void ClearColumn(string colName){
foreach(ListViewItem item in listView1.Items){
var cell = item.SubItems[colName];
if(cell != null) cell.Text = "";
}
}
The following code will work for ColumnHeader.Name passed in instead of ListViewSubItem.Name as the code above does:
public void ClearColumn(string columnHeaderName){
int i = listView1.Columns.IndexOfKey(columnHeaderName);
if(i == -1) return;
foreach(ListViewItem item in listView1.Items){
item.SubItems[i].Text = "";
}
}
You can try the following code to make it work for Text instead of Name:
public void ClearColumn(string colText){
if(listView1.Items.Count == 0) return;
var col = listView1.Columns.Cast<ColumnHeader>()
.Select((x,i)=>new{x,i})
.FirstOrDefault(a=>a.x.Text == colText);
if(col == null) return;
foreach(ListViewItem item in listView1.Items){
item.SubItems[col.i].Text = "";
}
}
I have been saving into the ComboBox a value out of the selected column in datagridview with below code.
My question is:How can I prevent duplicate records when I save the values into the ComboBox? How can I do that?
Code:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
Please try this
private void dgvServerList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 1)
{
string id = dgvServerList[e.ColumnIndex, e.RowIndex].Value.ToString();
int duplicaterow = 0;
for (int row = 0; row < dgvServerList.Rows.Count; row++)
{
if (row != e.RowIndex && id == dgvServerList[e.ColumnIndex, row].Value.ToString())
{
duplicaterow = row + 1;
MessageBox.Show("Duplicate found in the row: " + duplicaterow);
this.dgvServerList[e.ColumnIndex, e.RowIndex].Value = "";
break;
}
}
}
}
catch
{
}
}
you could first transfer your datagridview items to a dictionary (which guarantees uniqueness) and then transfer that dictionary content to the combobox. or you could check for uniqueness yourself using a 'Contains' method on the combobox. you could even tie the dictionary to the combobox as a source for the combobox items.
Dictionary<string,bool> d = new Dictionary<string,bool>();
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
d[dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()] = true;
}
CmbAra.Items.AddRange(d.Keys);
Use a set:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
HashSet<string> set = new HashSet<string>();
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
string s = dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString();
if(!set.Contains(s)) {
CmbAra.Items.Add(s);
set.Add(s);
}
}
by using the following check and then determine to add or not
if(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()))
You can use the following code part.
if(!(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString())))
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
else
{
MessageBox.Show("Value Already exists , not added");
}