-1

我正在尝试在 GridView 中显示 excel 工作表和 sql server 数据表之间的重复行。

这是我的代码:

var matched = (from table1 in dt4.AsEnumerable()
                join table2 in oltlb.AsEnumerable() on
                table1.Field<int>("ID") equals table2.Field<int>("ID")
                where table1.Field<string>("Data") == table2.Field<string>("Data")
                select table1);
DataTable dthi5 = new DataTable();
dthi5 = matched.CopyToDataTable();

我可以在 GridView 中显示 var 的值吗?

4

3 回答 3

1

是的,它可以用你的变量填充。(在这种情况下,您必须强制转换它.ToList()

你要明白那var是强类型!它不像在 php 中,您可以在其中切换变量的类型。

尝试写作

var x = null;

在 Visual Studio 中。会有错误!

您的var 每次都必须声明类型安全。这只是一个简短的咒语,让程序员不必总是在变量前面写长类型名。所以你var的情况是一个IEnumerable<T>

于 2012-10-25T09:03:29.790 回答
1

尝试这个

var matched = (from table1 in dt4.AsEnumerable()
                  join table2 in oltlb.AsEnumerable() on
                table1.Field<int>("ID") equals table2.Field<int>("ID")
                  where table1.Field<string>("Data") == table2.Field<string>("Data")
                  select table1);
      DataTable dthi5 = new DataTable();
      dthi5 = matched.CopyToDataTable();

myGridView.DataSource = dthi5;
myGridView.DataBind();

或者

myGridView = matched.ToList();
myGridView.DataBind();
于 2012-10-25T09:12:37.733 回答
0

You can definitely bind var to gridview.

Let's assume there's an employee table with three fields

emp_id emp_fname emp_lname

First way: if you give the whole table or a particular record to var then to list would work...

employees is a data context class once you've established a successful connection to sql server..

var result = from alias in dc.employees
          where alias.emp_id == id --(this is the passed parameter)

You can also manually specify the id, for example:

where alias.emp_id == 5
          select alias;

The whole employee record having id = 5 would get selected.

You can now simply bind this to gridview

gridview1.datasource = result.tolist();
gridview1.databind();

If you give the whole table, then also tolist would work

var result  = from alias in dc.employess
              select alias;

gridview1.datasource = result.tolist();
gridview1.databind();

If you select multiple columns then tolist won't work. You need to return object.

the method has been defined in class1.

public static object returnquery()
{

dcdatacontext dc = new dcdatacontext();

var result = from alias in dc.employees
             where alias.emp_id == 5
             select new
             {
                alias.emp_fname,
                alais.emp_lname
             };

 return result;
}

You need to catch the object.

object obj = new class1.returnquery();

gridview1.datasource = obj;
gridview1.databind();

Or you can simply try this and see if it works. I haven't tried this actually.

var result = from alias in dc.employees
             where alias.emp_id == 5
             select new
             {
                alias.emp_fname,
                alias.emp_lname
             };

gridview1.datasource= result.object();
gridview1.databind();
于 2013-08-15T02:39:36.197 回答