简体   繁体   English

将LINQ附加到C#中存在的数据源/网格视图

[英]Appending LINQ to exist datasource/gridview in C#

I have the following in my .aspx page: 我的.aspx页中包含以下内容:

<asp:GridView ID="Table1" runat="server" AutoGenerateColumns="False"    BorderColor="Black" OnRowCreated="Table1_RowCreated" OnRowDataBound="Table1_RowDataBound" CellPadding="3" CellSpacing="0" DataSourceID="Table1DataSrc" DataKeyNames="Field1">
<Columns>
<asp:BoundField DataField="Field1" HeaderText="Field1" SortExpression="Field1">
     <ItemStyle HorizontalAlign="Left" Font-Size="8" Wrap="false" />
     <HeaderStyle CssClass="header" Font-Size="8" />
 </asp:BoundField>
 </Columns>
 </asp:GridView>

Where the gridview has a DataSource that loads a few fields. 在gridview的数据源中加载了几个字段。 In my C# code behind, I have the following function which runs a LINQ query to process a stored procedure (on a different server than my other datasource) 在后面的C#代码中,我具有以下函数,该函数运行LINQ查询来处理存储过程(在与其他数据源不同的服务器上)

    private void RenderTables()
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataTable dt = new DataTable();

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("sp_special", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@AmIY", "Y");
            adapter.SelectCommand = cmd;
            adapter.Fill(dt);
            adapter.Dispose();
            conn.Close();
        }
        var enumvar = dt.AsEnumerable();
        var query = from x in enumvar
                    orderby x.Field<string>(3)
                    select new 
                    { 
                        Field3 = x.Field<string>(3),
                        Field4 = x.Field<int>(4),
                    };
        //I do some data manipulation using LINQ, but omitted here
        //do something with query.....
    }

How would I add new columns Field3 and Field4 to my Gridview and bind these new records? 如何将新列Field3和Field4添加到Gridview并绑定这些新记录? I do not want to store anything in temp tables on either servers 我不想在任何一台服务器的临时表中存储任何内容

it will work like this... 它会像这样...

<asp:BoundField DataField="Field3" HeaderText="Field3" SortExpression="Field3"> 
<asp:BoundField DataField="Field4" HeaderText="Field4" SortExpression="Field4">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM