Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
problem setting automatic serial number in listview, can add the serial number but while deleting ex. serial no like 1 2 3 4 5, if i delete the row having serial no 3, serial number 4 5 doesn't convert into 3 and 4 automatically


C#
private void btn_Addtocart_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtProductName.Text == "")
                {
                    MessageBox.Show("Please retrieve product name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (txtSaleQty.Text == "")
                {
                    MessageBox.Show("Please enter no. of sale quantity", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtSaleQty.Focus();
                    return;
                }
                count = ListView1.Items.Count + 1;
                int SaleQty = Convert.ToInt32(txtSaleQty.Text);
                ListViewItem lst = new ListViewItem(count.ToString());
                if (ListView1.Items.Count == 0)
                {
                    lst.SubItems.Add(txtProductName.Text);
                    lst.SubItems.Add(Company.Text);
                    lst.SubItems.Add(txtPrice.Text);
                    lst.SubItems.Add(txtSaleQty.Text);
                    lst.SubItems.Add(txtTotalAmount.Text);
                    ListView1.Items.Add(lst);
                    Company.Text = "";
                    txtProductName.Text = "";
                    txtPrice.Text = "";
                    txtAvailableQty.Text = "";
                    txtSaleQty.Text = "";
                    txtTotalAmount.Text = "";
                    textBox1.Text = "";
                    txtSubTotal.Text = subtot().ToString();
                }
                else
                {
                    lst.SubItems.Add(txtProductName.Text);
                    lst.SubItems.Add(Company.Text);
                    lst.SubItems.Add(txtPrice.Text);
                    lst.SubItems.Add(txtSaleQty.Text);
                    lst.SubItems.Add(txtTotalAmount.Text);
                    ListView1.Items.Add(lst);
                    Company.Text = "";
                    txtProductName.Text = "";
                    txtPrice.Text = "";
                    txtAvailableQty.Text = "";
                    txtSaleQty.Text = "";
                    txtTotalAmount.Text = "";
                    textBox1.Text = "";
                    txtSubTotal.Text = subtot().ToString();

                }
                ListView1.Sort();
                ListView1.Refresh();
                ListView1.Update();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



  private void removeFromListToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            foreach (ListViewItem listviewitem in ListView1.SelectedItems)
            {
                listviewitem.Remove();
            }                
	    }

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 4-Feb-15 5:43am
v2
Comments
Avik Ghosh22 4-Feb-15 23:33pm    
use <%# Container.DataItemIndex + 1%> in aspx page or bind listview after delete operation.
eric2world 20-Jan-21 21:43pm    
Hello, I'm in same with you. please another way.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900