Click here to Skip to main content
12,302,647 members (71,167 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET Forms
Now i've add row number in database already

but i've problem when i've delete row

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
       {
           for (int i = 0; i <= listView1.SelectedItems.Count - 1; i++)
           {
               ListViewItem lvi = listView1.SelectedItems[i];
               listView1.Items.Remove(lvi);
           }
       }

Example i've delete row number 3 and i've 5 row

first row number is 12345
When i've delete it's show 1245
i want it gonna be 1234

i find solution for this Problem but it's in VB

on this link http://www.pcreview.co.uk/forums/row-number-listview-t1417082.html[^]

I've try for long time but i haven't got a solution

Please anyone to Help me

Thankyou so much
Posted 18-Feb-12 8:13am
Edited 18-Feb-12 9:19am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

as i think, you are using datatable to bind list view. then you can remove from datatable not from list view. after removing bind datatable to list view again.

It will solve your problem.

Don't forget to mark if it is your solution.

With you for you always Smile | :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Perhaps a bind after remove should do it.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Thank you for Rahul Rajat Singh now and Sarvesh Kumar Gupta I've fix for my problem by apply from your solution

This's my code when MouseDoubleclick Event will Delete Row and Sort Row number again

Hope this code help for another who have same question
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
       {
           DataTable dt1 = new DataTable();//<< datatable เปล่า
           string[] data_new = new string[7];//<< ตาม column
           int countnum = 0;
           dt1.Columns.Add("No.", typeof(string));
           dt1.Columns.Add("ID", typeof(string));
           dt1.Columns.Add("NAME", typeof(string));
           dt1.Columns.Add("Price", typeof(string));
           dt1.Columns.Add("Quantity", typeof(string));
           dt1.Columns.Add("Summary", typeof(string));
           dt1.Columns.Add("Lot.", typeof(string));
 
           for (int i = 0; i < listView1.Items.Count; i++)
           {
               if (listView1.Items[i].Selected == false) //<< inverse Select
               {
                   countnum += 1;
                   data_new[0] = (countnum).ToString();
                   data_new[1] = listView1.Items[i].SubItems[1].Text;
                   data_new[2] = listView1.Items[i].SubItems[2].Text;
                   data_new[3] = listView1.Items[i].SubItems[3].Text;
                   data_new[4] = listView1.Items[i].SubItems[4].Text;
                   data_new[5] = listView1.Items[i].SubItems[5].Text;
                   data_new[6] = listView1.Items[i].SubItems[6].Text;
 
                   dt1.Rows.Add(data_new); //datatabel ใหม่ที่ลบแล้ว
               }
           }
           int fc = dt1.Columns.Count;
           listView1.Items.Clear();
           foreach (DataRow row in dt1.Rows)
           {
               string[] subitems = new string[fc];
               object[] o = row.ItemArray;
               for (int i = 0; i < fc; i++)
               {
                   subitems[i] = o[i].ToString();
               }
               ListViewItem item = new ListViewItem(subitems);
               listView1.Items.Add(item);
           }
           for (int i = 1; i <= listView1.SelectedItems.Count ; i++)
           {
               ListViewItem lvi = listView1.SelectedItems[i];
               listView1.Items.Remove(lvi);
           }
           CalculateNet();
       }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

private Button withEventsField_bt = new Button();
internal Button bt {
get { return withEventsField_bt; }
set {
if (withEventsField_bt != null) {
withEventsField_bt.Click -= bt_Click;
}
withEventsField_bt = value;
if (withEventsField_bt != null) {
withEventsField_bt.Click += bt_Click;
}
}
}
internal ListView lv = new ListView();
internal ColumnHeader ch1 = new ColumnHeader();
internal ColumnHeader ch2 = new ColumnHeader();
private void Form1_Load(object sender, System.EventArgs e)
{
ListViewItem lvi1 = new ListViewItem(new string[] {
"1",
"One"
}, -1);
ListViewItem lvi2 = new ListViewItem(new string[] {
"2",
"Two"
}, -1);
ListViewItem lvi3 = new ListViewItem(new string[] {
"3",
"Three"
}, -1);
ListViewItem lvi4 = new ListViewItem(new string[] {
"4",
"Four"
}, -1);
ListViewItem lvi5 = new ListViewItem(new string[] {
"5",
"Five"
}, -1);
lv.Columns.AddRange(new ColumnHeader[] {
ch1,
ch2
});
lv.Items.AddRange(new ListViewItem[] {
lvi1,
lvi2,
lvi3,
lvi4,
lvi5
});
lv.Location = new System.Drawing.Point(10, 10);
lv.Name = "ListView1";
lv.Size = new System.Drawing.Size(150, 150);
lv.TabIndex = 0;
lv.View = View.Details;
bt.Location = new System.Drawing.Point(10, 200);
bt.Text = "Click Me";
Controls.Add(lv);
Controls.Add(bt);
}
private void bt_Click(System.Object sender, System.EventArgs e)
{
lv.Items.RemoveAt(2);
for (int i = 2; i <= lv.Items.Count - 1; i++) {
lv.Items(i).Text = Convert.ToString(Convert.ToInt32(lv.Items(i).Text) - 1);
}
}
  Permalink  
Comments
Sandeep Mewara 13-Jul-12 10:09am
   
What is this and why posted now after 4 months since the question was asked ?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160530.1 | Last Updated 13 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100