对于GridView要想修改其列名,再网上查了一下好多是这样写的,这样写都是以失败告终.
GridView1.AutoGenerateColumns = false;
this.GridView1.DataSource = Dataview;
this.GridView1.DataBind();
this.GridView1.Columns[0].HeaderText = "序号";
在最后一句提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
正确的写法是:
BoundField field = new BoundField();
field.DataField = nds["name"].Value;
field.HeaderText = nds["Caption"].Value;
GridView1.Columns.Add(field);
原因是当GridView1.AutoGenerateColumns = false 后GridView会不自动匹配列名帮定,这个时候GridView1.Columns是一个空集合,所以需要重新动态添加BoundField .这样之后再重绑定数据.
GridView1.AutoGenerateColumns = false;
this.GridView1.DataSource = Dataview;
this.GridView1.DataBind();
this.GridView1.Columns[0].HeaderText = "序号";
在最后一句提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
正确的写法是:
BoundField field = new BoundField();
field.DataField = nds["name"].Value;
field.HeaderText = nds["Caption"].Value;
GridView1.Columns.Add(field);
原因是当GridView1.AutoGenerateColumns = false 后GridView会不自动匹配列名帮定,这个时候GridView1.Columns是一个空集合,所以需要重新动态添加BoundField .这样之后再重绑定数据.
本文介绍了一种正确修改ASP.NET GridView控件中列名的方法。针对常见的索引超出范围错误,文章详细解释了如何通过手动创建BoundField并将其添加到GridView的Columns集合中来解决这一问题。
3191

被折叠的 条评论
为什么被折叠?



