2011-08-17 49 views
0

我不知道如何更新通用列表中的项目后,审查所有发布在这里的问题,我很抱歉。这里是我的问题:使用C#更新一个通用的列表项目

我有这样的结构:

List<LineInfo> Lines = new List<LineInfo>(); 
    LineInfo LInfo; 
    struct LineInfo 
    { 
     public int line; 
     public int WO; 
     public string Brand; 
     public string Model; 
     public string Customer; 
     public int Qty; 
     public int Target; 
     public string Leader; 
     public string Start; 
     public string End; 
     public int Percent; 
    }  

而且我想更新进入LInfo项目之一的领域“百分比”,我有当前位置(aCurrentLine)。

LInfo.Percent = Convert.ToInt32((RealProduced/DesireProd) * 100);     
Lines[aCurrentLine]....? 

请指教,谢谢。

回答

3

只是

LInfo.Percent = Convert.ToInt32((RealProduced/DesireProd) * 100); 
Lines[aCurrentLine] = LInfo; 

应该工作...但不要使用任何公共领域可变的结构。两者在可维护性和意想不到的效果方面都很糟糕。

您在C#中创建的大多数类型可能都是类 - 您希望创建值类型(结构)的情况相对较少。你应该确保你知道differences between the two

同样在C#中的字段应该几乎总是是私有的。它们应该是类型的实现细节,而不是其公共API的一部分。 Use properties instead - 在C#3中自动实现的属性使得它们几乎可以像字段一样紧凑,如果你只是想要一个平凡的属性。

+0

太棒了!谢谢,关于可变结构,我需要了解它;) – Somebody

1

我只有一个adivice。可变的结构是邪恶的。尽量避免它。

Lines[aCurrentLine] = LInfo;

您将无法访问Lines[aCurrentLine].Percent,因为它只是更新的临时副本。

+0

谢谢! @Ashley – Somebody

0

用于更新带网格视图的通用列表记录。放入此代码。

List<Address> people = (List<Address>)Session["People"]; 
     people[e.RowIndex].DoorNo = ((TextBox)grdShow.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
     people[e.RowIndex].StreetName = ((TextBox)grdShow.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
     people[e.RowIndex].City = ((TextBox)grdShow.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
     people[e.RowIndex].PhoneNo = ((TextBox)grdShow.Rows[e.RowIndex].Cells[5].Controls[0]).Text; 
     grdShow.EditIndex = -1; 
     grdShow.DataSource = people; 
     grdShow.DataBind(); 

并将此代码放入页面加载事件。

if (!IsPostBack) 
     { 

      List<Address> people = new List<Address>(); 
      Session["People"] = people; 
     } 

使用网格视图写上的按钮事件的代码创建泛型列表(从文本框中获取数据并保存在列表中)

GenerateList();//call the method GenerateList(); 

GenerateList的sytex();

private void GenerateList() 
    { 
     if (Session["People"] != null) 
     { 

      List<Address> people = (List<Address>)Session["People"]; 

      Address a1 = new Address(); 

      a1.DoorNo = txtDoorno.Text; 
      a1.StreetName = txtStreetName.Text; 
      a1.City = txtCityname.Text; 
      a1.PhoneNo = txtPhoneno.Text; 
      people.Add(a1); 
      Session["People"] = people; 
      grdShow.DataSource = people; 
      grdShow.DataBind(); 
     } 
    } 
相关问题