0

I am trying to make some note app for myself.
I can successfully get the course name, start date, end date and worked hours.
The problem is when I click the stop Button, I can't figure out how to add other data to same row but to the different columns.

This is it. I cant add the end date and difference of the two time

private void button2_Click(object sender, EventArgs e) { ListViewItem item = new ListViewItem(); note.nameofcourse = textBox1.Text; item.Text = note.nameofcourse; listView1.Items.Add(item); DateTime date = DateTime.Now; note.dateofstart = date.ToString(); item.SubItems.Add(note.dateofstart); } private void button3_Click(object sender, EventArgs e) { DateTime time_2 = DateTime.Now; note.dateofend = time_2.ToString(); convertit(); } public void convertit() { DateTime dt = Convert.ToDateTime(note.dateofstart); DateTime d2 = Convert.ToDateTime(note.dateofend); TimeSpan totalsum = d2.Subtract(dt); note.totalsum = totalsum.ToString(); //item.SubItems.Add(note.totalsum); } 

Also this is my class.cs:

class noteInfos { public string nameofcourse; public string dateofstart; public string dateofend; public string totalsum; } 
3
  • ListView is much more awkward to work with than e.g. DataGridView; make a datatable with the relevant columns, bind it to the DGV (datasource property on the DGV) and then add rows to the table and they will appear in the grid Commented Sep 19, 2021 at 17:19
  • Also, please see - learn.microsoft.com/en-us/dotnet/csharp/fundamentals/… Commented Sep 19, 2021 at 18:05
  • Thank you for your informative answers. I am quite new to logic of OOP. Thats gonna help me for sure. Commented Sep 19, 2021 at 19:51

1 Answer 1

1

I fixed the problem somehow. First of all you should create the row if you dont even add something. Then you can edit these rows with that last piece of code.

private void button2_Click(object sender, EventArgs e) { ListViewItem item = new ListViewItem(); note.nameofcourse = textBox1.Text; item.Text = note.nameofcourse; listView1.Items.Add(item); DateTime date = DateTime.Now; note.dateofstart = date.ToString(); item.SubItems.Add(note.dateofstart); item.SubItems.Add(note.dateofend); item.SubItems.Add(note.totalsum); } 

Then i add some weird things too.

 int sayi = listView1.Items.Count; listView1.Items[sayi -1].SubItems[2].Text = note.dateofend; listView1.Items[sayi -1].SubItems[3].Text = note.totalsum; note.totalsum = ""; note.dateofend = ""; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.