Gridview没有显示Run上的最顶行

时间:2014-04-09 14:09:56

标签: c# asp.net visual-studio-2012

第一次海报,长时间的搜索者。我确定这是一个简单的解决方案....但我在这里敲我的头。使用visual studio 2012

我有一个gridview,在后面的代码中使用sql的rowdatabound。问题是,当我运行它时,第一行不会显示在gridview上。这些数据非常动态且不断变化。有什么建议?

代码:

sql2 = "select cf.cfs_no, us.unit_id, ua.unit_activity_description, ct.call_description, cf.premise_name, us.location " +
                           "from dba.units_service us with(nolock) " +
                            "left outer join dba.cfs cf with(nolock) on " +
                            "us.cfs_key = cf.cfs_key " +
                            "left outer join dba.call_type ct with(nolock) on " +
                            "cf.call_type = ct.call_type_cd " +
                            "left outer join dba.unit_activity ua with(nolock) on " +
                            "us.unit_activity_cd = ua.unit_activity_cd " +
                            "where " +
                            "(agency_id in (@agency_id) and us.unit_activity_cd != @unit_activity) " +
                            "order by 1 desc, 2 desc ";

                    //agency_id = @varrible ; us.unit_activity_cd = @varrible

                    using (SqlCommand cmd = new SqlCommand(sql2, con))
                    {
                        //con.Open();
                        SqlDataReader reader1 = cmd.ExecuteReader();
                        //SqlDataAdapter da = new SqlDataAdapter(cmd);

                        if (reader1.HasRows)
                        {
                            reader1.Read();
                            GridView1.DataSource = reader1;
                            GridView1.DataBind();
                        }

gridview中是否有可能导致这种情况的属性? gridview是RowDatabound,并有代码来处理它。我只是认为这些属性很愚蠢。

谢谢!

1 个答案:

答案 0 :(得分:1)

通过使用reader1.Read()命令,您跳过第一行,而不是尝试

using (SqlDataReader reader = cmd.ExecuteReader())
{
    DataTable table = new DataTable();
    table.Load(reader);
    GridView1.DataSource = table;
}