1
ID      Item          Code
1       Item a        1565
2       Item x        **2565**
3       Item w        1245
4       Item f        1345

I have a ListView Details format like above and I want to change the value of a cell content which is in between ** mark. How can I change it such a way that it will appear in same row and without need of updating other values in a row or a table.

Please Guide me in C#.net

3
  • How do you wish to identify the row that needs changing? Commented Jan 22, 2011 at 11:20
  • How do you fill the listview ? Changing the value is basically the same, but instead of adding a new item, you just modify a property of it... Commented Jan 22, 2011 at 11:22
  • Just assign the entire row. ListView is smart enough to only do work when the value changed. Commented Jan 22, 2011 at 14:15

1 Answer 1

4

Basically, it depends on how you fill your ListView.

Anyway, a code like this, should work in almost every situations:

var idIdx = listView1.Columns["ID"].Index;
var codeIdx = listView1.Columns["Code"].Index;

foreach (ListViewItem item in listView1.Items)
{
    if (item.SubItems[idIdx].Text == "2")
    {
        item.SubItems[codeIdx].Text = "new value...";
        break;
    }
}

Just a caveat:

to assure the first 2 lines of this code work, you must properly initialize your columns' Name property, when you create them:

either by using the proper add overload:

listView1.Columns.Add("ID", "ID");

or by setting it later:

var col = listView1.Columns.Add("ID");
col.Name = "ID";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.