如何使用自动生成字段将 GridView2.Columns 操作为 BoundField?

发布于 2024-10-21 18:30:01 字数 1051 浏览 3 评论 0原文

如何使用自动生成字段将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-10-28 18:30:01

正如我在评论中提到的,您没有从查询中得到任何结果,因此没有 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文