如题~~~很郁闷,数组不能达到我的要求。因为我要动态删除和插入,还有点疑问如果我用结构体作为 arraylist 的元素以后页面控件的绑定是否还是和普通数组一样处理?
解决方案 »
- IIS装了用不了?什么软件可以替代?
- 大家可以偷到这人天气预后吗?最好用ASP
- 请教高手,很奇怪,treeview 中参数传递的问题,高分求救各位大虾。
- 有没有跟asp.net相结合,讲UML的书啊?
- wmv格式的视频,怎么在360浏览器,ie,火狐,chorme等主流的浏览器兼容播放?在线等
- asp在asp.net平台下可以运行吧可以的话需要注意些什么问题呢?给分!!!!
- 关于数据库连接的问题?
- 谁有关于wap的东西啊?
- 100分,帮忙看看出现这种错误会是什么原因??!!
- .net网站发布的疑惑,还望指点!
- 请大家推荐一些关于asp.net各个控件技巧性的文章的网站,谢谢!
- 请教管理员的权限和用户登陆权限
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;
}
}
ArrayList list = new ArrayList();
但是强烈推荐你使用泛型
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进行包装会到来频繁的装箱和拆箱操作,势必给程序性能带来负面影响
建议使用泛型集合
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;
}
}
}
gridview可以这样绑定,可是listbox这样绑定的时候无任何提示却不成功
比如用1楼的结构
listbox.datasource=list;
listbox.dataTextField="A";
listbox.databind();
无效果
属性已经公开,绑定属性名,如同大写A
上源代码吧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
这样没有效果
gridview1.cloumns.clear();
gridview1.AutoGenerateColumns=true;
gridview1.DataSource = materialStrC;
gridview1.DataBind();//applyMaterialList为listbox
ArrayList是可以实现你的要求的
如:List<string> list=new List<string>();