使用LINQ to SQL自定义Gridview中的列

时间:2014-01-01 19:39:11

标签: c# asp.net sql linq gridview

作为编程爱好者,我试图复制一些代码from a video I found。该表单使用LINQ to SQL将数据从托管共享数据库绑定到Gridview。页面工作正常,直到我在后面的代码中创建一些C#代码,这与我在视频中看到的非常相似。 Transaction_ID是主键和数据表。我至少看过十几次视频,无法识别我的问题。我在这个网站上看到过类似的问题,但还没有为我找到解决方案。如果我使用选择方法1,则gridview会填充,但不会在我使用方法2时填充。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{

    DataClasses2DataContext db = new DataClasses2DataContext();

    var q = from t in db.Transactions

            //selection method 1
            //select t;

            //selection method 2
            select new
            {
                //ID2 = t.Transaction_ID,
                Date = t.Date,
                Type = t.Transaction_Type,
                Group = t.Group,
                Name = t.Name,
                Effective_Session = t.Effective_Session

            };

    e.Result = q;
}

}

这是ASPX代码:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="True" DataKeyNames="Transaction_ID" 
    DataSourceID="LinqDataSource1">

</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="DataClasses2DataContext" EntityTypeName="" 
    onselecting="LinqDataSource1_Selecting" TableName="Transactions">
</asp:LinqDataSource>
</asp:Content>

这是一段dbml代码:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Transactions")]
public partial class Transaction : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new           PropertyChangingEventArgs(String.Empty);

private int _Transaction_ID;
private string _League;
private string _Season;
private string _Session;
private System.Nullable<System.DateTime> _Date;
private string _Effective_Session;
private string _Team;
private string _Transaction_Type;
private string _Group;
private System.Nullable<double> _Round;
private System.Nullable<double> _Pick;
private System.Nullable<double> _Overall;
private System.Nullable<double> _Player_ID;
private string _Name;
private System.Nullable<double> _F15;
private System.Nullable<double> _F16;
private System.Nullable<double> _F17;
private System.Nullable<double> _F18;
private System.Nullable<int> _F19;
private System.Nullable<double> _F20;
private System.Nullable<int> _F21;
private System.Nullable<int> _F22;
private System.Nullable<double> _F23;
private System.Nullable<double> _F24;
private string _F25;
private System.Data.Linq.Binary _upsize_ts;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnTransaction_IDChanging(int value);
partial void OnTransaction_IDChanged();
partial void OnLeagueChanging(string value);
partial void OnLeagueChanged();
partial void OnSeasonChanging(string value);
partial void OnSeasonChanged();
partial void OnSessionChanging(string value);
partial void OnSessionChanged();
partial void OnDateChanging(System.Nullable<System.DateTime> value);
partial void OnDateChanged();
partial void OnEffective_SessionChanging(string value);
partial void OnEffective_SessionChanged();
partial void OnTeamChanging(string value);
partial void OnTeamChanged();
partial void OnTransaction_TypeChanging(string value);
partial void OnTransaction_TypeChanged();
partial void OnGroupChanging(string value);
partial void OnGroupChanged();
partial void OnRoundChanging(System.Nullable<double> value);
partial void OnRoundChanged();
partial void OnPickChanging(System.Nullable<double> value);
partial void OnPickChanged();
partial void OnOverallChanging(System.Nullable<double> value);
partial void OnOverallChanged();
partial void OnPlayer_IDChanging(System.Nullable<double> value);
partial void OnPlayer_IDChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
partial void OnF15Changing(System.Nullable<double> value);
partial void OnF15Changed();
partial void OnF16Changing(System.Nullable<double> value);
partial void OnF16Changed();
partial void OnF17Changing(System.Nullable<double> value);
partial void OnF17Changed();
partial void OnF18Changing(System.Nullable<double> value);
partial void OnF18Changed();
partial void OnF19Changing(System.Nullable<int> value);
partial void OnF19Changed();
partial void OnF20Changing(System.Nullable<double> value);
partial void OnF20Changed();
partial void OnF21Changing(System.Nullable<int> value);
partial void OnF21Changed();
partial void OnF22Changing(System.Nullable<int> value);
partial void OnF22Changed();
partial void OnF23Changing(System.Nullable<double> value);
partial void OnF23Changed();
partial void OnF24Changing(System.Nullable<double> value);
partial void OnF24Changed();
partial void OnF25Changing(string value);
partial void OnF25Changed();
partial void Onupsize_tsChanging(System.Data.Linq.Binary value);
partial void Onupsize_tsChanged();
#endregion

public Transaction()
{
    OnCreated();
}

[global::System.Data.Linq.Mapping.ColumnAttribute(Name="[Transaction ID]",     Storage="_Transaction_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]
public int Transaction_ID
{
    get
    {
        return this._Transaction_ID;
    }
    set
    {
        if ((this._Transaction_ID != value))
        {
            this.OnTransaction_IDChanging(value);
            this.SendPropertyChanging();
            this._Transaction_ID = value;
            this.SendPropertyChanged("Transaction_ID");
            this.OnTransaction_IDChanged();
        }
    }
}

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_League", DbType="NVarChar(255)", UpdateCheck=UpdateCheck.Never)]
public string League
{
    get
    {

这是错误:

DataBinding: '<>f__AnonymousType1`6[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, 
    PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],
    [System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, 
    mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, 
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, 
    Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Transaction_ID'.

1 个答案:

答案 0 :(得分:0)

据推测,当您取消注释此行时,您的代码是否有效?

//ID2 = t.Transaction_ID,

如果您要从要显示的数据源中选择哪些值,则需要继续在GridView标记中设置列(并设置AutoGenerateColumns="false")。

您的列集合(在GridView声明中)看起来像这样:

<columns>
    <asp:BoundField DataField="Date" HeaderText="Date"/>
    <asp:BoundField DataField="Type" HeaderText="Type"/>
    <asp:BoundField DataField="Group" HeaderText="Group"/>
    <asp:BoundField DataField="Name" HeaderText="Name"/>
    <asp:BoundField DataField="Effective_Session" HeaderText="Effective Session"/>
</columns>