如题~~~很郁闷,数组不能达到我的要求。因为我要动态删除和插入,还有点疑问如果我用结构体作为 arraylist 的元素以后页面控件的绑定是否还是和普通数组一样处理?

解决方案 »

  1.   


            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    List<StructTest> list = new List<StructTest>();
                    list.Add(new StructTest(1, false, DateTime.Now, "Hello"));
                    this.GridView1.DataSource = list;
                    this.GridView1.DataBind();
                }         }
            struct StructTest
            {
                int a;
                public int A
                {
                    get { return a; }
                    set { a = value; }
                }
                bool b;            public bool B
                {
                    get { return b; }
                    set { b = value; }
                }
                DateTime c;            public DateTime C
                {
                    get { return c; }
                    set { c = value; }
                }
                string d;            public string D
                {
                    get { return d; }
                    set { d = value; }
                }            public StructTest(int a, bool b, DateTime c, string d)
                {
                    this.a = a;
                    this.b = b;
                    this.c = c;
                    this.d = d;
                }
            }
      

  2.   

    如果非要用ArrayList的话,就把List<StructTest> list = new List<StructTest>();改为
    ArrayList list = new ArrayList();
    但是强烈推荐你使用泛型
      

  3.   


    ArrayList al = new ArrayList();
    al.Add(1); // 将1加入数组
    al.Add(3); // 将3加入数字
    al.Insert(1, 2); // 在1和3直接插入一个元素
    al.Remove(3); // 删除数组中的第一个3
    al.Sort(); // 给数组排序
    foreach (object o in al) { 
        // 遍历整个数组
    }
    顺便说下:
    C#中没有“结构体”,倒是有个“结构”,或者称之为“值类型”
    值类型使用ArrayList进行包装会到来频繁的装箱和拆箱操作,势必给程序性能带来负面影响
    建议使用泛型集合
      

  4.   

    ArrayList并不是类型安全的,在循环遍历时是会执行拆箱操作 偶也建议使用泛型来做
      

  5.   

    建议用List<T>
    ArrayList是可以实现你的要求的.
      

  6.   

    还有点疑问如果我用结构体作为 arraylist 的元素以后页面控件的绑定是否还是和普通数组一样处理?
    =================
    一样处理
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Default11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList();
            for (int i = 0; i < 5; i++)
            {
                TestUser u = new TestUser();
                u.Name = "test"+i.ToString();
                u.ID = i;
                al.Add(u);
            }
            this.GridView1.DataSource = al;
            GridView1.DataBind();
        }    
    }struct TestUser
    {
        private string _name;
        private int _id;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }    public int ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
    }
      

  7.   


    gridview可以这样绑定,可是listbox这样绑定的时候无任何提示却不成功
    比如用1楼的结构
    listbox.datasource=list;
    listbox.dataTextField="A";
    listbox.databind();
    无效果
    属性已经公开,绑定属性名,如同大写A
     
      

  8.   

    是很奇怪绑定gridview 没问题,可是listbox没效果
    上源代码吧public class StringControl
    {
     public struct materialInfor 
        {
            public  string materialNum;
            public  string applyPersonNum;
           // public string materialName;
           
            public int numOfMaterial;
            public  decimal priceOfMaterial;        public string materialNumberAndNum 
            {
                get 
                {
                    if (materialNum == null || materialNum == "")
                    { return ""; }
                    else 
                    {
                        return "材料:"+materialNum+",数量:"+numOfMaterial+"个" ;
                    }
                }
            
            }
            public string materialNumber 
            {            get
                {
                    if (materialNum == null || materialNum == "")
                    { return ""; }
                    else
                    {
                        return materialNum;
                    }
                }
            
            
            }
    }
     materialStrC.Add(new StringControl.materialInfor(materialInforGridView.Rows[Convert.ToInt32(editNumber.Value)].Cells[9].Text, Convert.ToInt32(applyNumber.Text), Convert.ToDecimal(materialInforGridView.Rows[Convert.ToInt32(editNumber.Value)].Cells[8].Text)));//给list<填充数据>
     List<StringControl.materialInfor> materialStrC = new List<StringControl.materialInfor>();
     applyMaterialList.DataSource = materialStrC;
     applyMaterialList.DataTextField = "materialNumberAndNum";
     applyMaterialList.DataValueField = "materialNumber";
     applyMaterialList.DataBind();//applyMaterialList为listbox
    这样没有效果
      

  9.   

    当把最后一段绑定listbox的代码改为gridview时却可以在gridview中显示List<StringControl.materialInfor> materialStrC = new List<StringControl.materialInfor>();
    gridview1.cloumns.clear();
    gridview1.AutoGenerateColumns=true;
    gridview1.DataSource = materialStrC;
    gridview1.DataBind();//applyMaterialList为listbox
      

  10.   

    建议用List <T>
    ArrayList是可以实现你的要求的
      

  11.   

    不好意思,由于list<T>放在最顶端~没有设置为静态每次页面提交的时候都被重新创建所以丢失了数据,已经修改。后面的问题是我自己的错误。着就给分结贴~~感谢各位高人相助
      

  12.   

    那你就用泛型来加以限制
    如:List<string> list=new List<string>();