如何使用自动生成字段将 GridView2.Columns 操作为 BoundField?
如何使用自动生成字段将 GridView2.Columns 操作为 BoundField? 索引超出范围?GridView2.Columns[6] as BoundField 我使用自动生成字段。
JobPostDataContext db = new JobPostDataContext();
var query = from j in db.JobLists
join u in db.UserLists
on j.UserID equals u.UserID
where j.JobTitle.Contains(this.TextBox1.Text)
select new
{
j.JobID,
j.JobTitle,
j.Summary,
j.Details,
j.CompanyName,
j.CompanyEmail,
j.DatePosted,
j.UserID,
u.City,
u.State,
u.Country
};
GridView2.DataSource = query;
GridView2.DataBind();
BoundField DatePosted = GridView2.Columns[6] as BoundField;
DatePosted.DataFormatString = "{0:MMM,dd yy}";
How to manipulate GridView2.Columns as BoundField with auto-generate field?
Index outof range?GridView2.Columns[6] as BoundField I use Automatic generate field.
JobPostDataContext db = new JobPostDataContext();
var query = from j in db.JobLists
join u in db.UserLists
on j.UserID equals u.UserID
where j.JobTitle.Contains(this.TextBox1.Text)
select new
{
j.JobID,
j.JobTitle,
j.Summary,
j.Details,
j.CompanyName,
j.CompanyEmail,
j.DatePosted,
j.UserID,
u.City,
u.State,
u.Country
};
GridView2.DataSource = query;
GridView2.DataBind();
BoundField DatePosted = GridView2.Columns[6] as BoundField;
DatePosted.DataFormatString = "{0:MMM,dd yy}";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中提到的,您没有从查询中得到任何结果,因此没有 (0) 列。这就是您无法访问
Columns[6]
的原因。Columns.Count 返回 0。即零列。您无法访问其中任何一个 - Columns.Count 是从 1 开始的,与 Columns[Index] 是从 0 开始的不同。我想这就是你感到困惑的地方。
As I mentioned in my comments, you're not getting any results back from your query, and therefore have no (0) columns. That's why you can't access
Columns[6]
.Columns.Count is returning 0. That's zero columns. You can't access any of them - Columns.Count is 1 based, unlike Columns[Index] which is 0 based. I think that's where you're getting confused.