C#中的Gridview不显示数据和大错误

时间:2016-05-17 07:35:02

标签: c#

我正在尝试使用网格视图显示我的数据表

<tbody ng-hide="loading" ng-repeat="student in students | filter:searchText">
<tr>
    <td>@{{ student.rollno }}</td>
    <td>@{{ student.name }}</td>
    <td>@{{ student.fname }}</td>
    <td>@{{ student.obtainedmarks }}</td>
    <td>@{{ student.totalmarks }}</td>
    <td>@{{ student.percentage }}</td>
    <td>
        {!! Form::open(array('url' => '/result/@{{ student.id }}/edit' , 'method' => 'GET')) !!}
        {!! Form::hidden('id',  '@{{ student.id }}') !!}
        <button type="submit" class="btn btn-default">Edit</button>
        {!! Form::close() !!}
    </td>
    <td>
        {!! Form::open(array('url' => '/result/destroy' , 'files'=>true, 'method' => 'delete')) !!}
        {!! Form::hidden('id', '@{{student.id}}' ) !!}
        <button type="submit" class="btn btn-danger">Delete</button>
        <hr>
        {!! Form::close() !!}
    </td>
</tr>
</tbody>

错误:

  

发生了'System.InvalidOperationException'类型的异常   System.Web.dll但未在用户代码中处理

     

其他信息:数据源是无效类型。肯定是   IListSource,IEnumerable或IDataSource。

用户表:

 protected void show_data(object sender, EventArgs e)
    {
        string str = "Data Source=(LocalDB)\\MSSQLLocalDB;";
        str += "AttachDbFilename=|DataDirectory|DinoData.mdf;";
        str += "Integrated Security= True";
        SqlConnection c;
        c = new SqlConnection(str);
        GV.DataSource = User;
        GV.DataBind();
    }

我该怎么办? 如果我只想用gridview显示表格的一部分怎么做?

1 个答案:

答案 0 :(得分:0)

您无需查询数据库中的行,您必须具有以下选择语句:

   protected void show_data(object sender, EventArgs e)
        {
            string str = "Data Source=(LocalDB)\\MSSQLLocalDB;";
            str += "AttachDbFilename=|DataDirectory|DinoData.mdf;";
            str += "Integrated Security= True";
            SqlConnection c;
            c = new SqlConnection(str);
            DataTable dt = new DataTable();
            //For exemple t select all rows in you Table User,you can insert a condition here 
            String req = "SELECT * FROM [User]";
            SqlDataAdapter da = new SqlDataAdapter(req, c);
            da.Fill(dt);
            GV.DataSource = dt;
            GV.DataBind();
        }