开发者

asp:LinqDataSource DataTable

开发者 https://www.devze.com 2023-04-04 01:38 出处:网络
I have a DataTable that I fill manually, ie, newrow = dt.NewRow(); dt.Rows.Add(newrow); and Im trying to benefit from the groupby features of LinqDataSource (as shown by Matt) by linking the LinqD

I have a DataTable that I fill manually, ie,

newrow = dt.NewRow();
dt.Rows.Add(newrow);

and Im trying to benefit from the groupby features of LinqDataSource (as shown by Matt) by linking the LinqDataSource to the DataTable, but its just not happening.

Has anyone any experience with this?

 <%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    public class MyData
    {
        public MyData(){}

        private DataTable dt = new DataTable("dt");
        public DataTable MyTable
        {
            get { return dt; }
        开发者_开发知识库    set { dt = value; }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        MyData mydata = new MyData();
        mydata.MyTable.Columns.Add("column1");
        DataRow dr = mydata.MyTable.NewRow();
        dr[0] = "some data";
        mydata.MyTable.Rows.Add(dr);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:LinqDataSource 
            ID="LinqDataSource1" 
            runat="server"
            ContextTypeName="MyData" 
            TableName="MyTable">
        </asp:LinqDataSource>

        <asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1">
        </asp:GridView>
    </form>
</body>
</html>


"dt" is not the name of the table try this

DataTable dt = new DataTable("dt");

and then try this would name the table as "dt".


No that just won't work at all. The ContextTypeName property needs to be that of a Context like a LinqtoSql DataContext and the TableName is the name of the database table that is mapped in your LinqToSql DBML file. Have you tried actually following the article you link to as it gives a pretty good overview of getting LinqToSQL going.

0

精彩评论

暂无评论...
验证码 换一张
取 消