C# gridview的恒定长度

C# gridview的恒定长度,c#,asp.net,gridview,C#,Asp.net,Gridview,最近我遇到了一个问题。这个问题虽然没有影响我的流程,但如果解决了,会使GUI看起来很好 问题是…我有一个搜索屏幕,根据搜索条件过滤一些记录,这些记录显示在定义了一些ItemTemplate的gridview中。我的问题是网格的长度根据网格中记录的数量进行调整。我需要有一个恒定的网格高度,以便我的页面长度在所有搜索中保持不变。只有当用户希望每页显示超过10条记录时,才应增加此高度 请帮我解决这个问题。因为您的要求是:您有一个按钮,上面写着“显示更多”,单击后会显示更多的10行 一种方法是使用对象的

最近我遇到了一个问题。这个问题虽然没有影响我的流程,但如果解决了,会使GUI看起来很好

问题是…我有一个搜索屏幕,根据搜索条件过滤一些记录,这些记录显示在定义了一些ItemTemplate的gridview中。我的问题是网格的长度根据网格中记录的数量进行调整。我需要有一个恒定的网格高度,以便我的页面长度在所有搜索中保持不变。只有当用户希望每页显示超过10条记录时,才应增加此高度


请帮我解决这个问题。

因为您的要求是:您有一个按钮,上面写着“显示更多”,单击后会显示更多的10行

一种方法是使用
对象的
列表
,然后调用方法。如果您已经使用了
LINQ
中的
Take(n)
扩展名,也可以只返回所需数量的记录。我将
CountToDisplay
作为变量保存当前要显示的记录数,初始设置为0(零)

GetEmployees
方法

protected List<Employee> GetEmployees(int CountToDisplay)
{
List<Employee> employees= new List<employee>();

// Sample code to fill the List. Use the `Take` Extension
    dbDataContext db = new dbDataContext();
    var e= ( from c in db.Employees
            select c ).Take(CountToDisplay);

     //iterate through results and add to List<Employee>
     foreach(var c in e)
     {
         employee emp = new employee { name = c.name, address = c.address };
         employees.Add(emp);
     } 
     return employees;

}
现在是有趣的部分。假设您有一个“显示更多”按钮,当单击该按钮时,将显示接下来的10多行。这会一直持续到你到达终点。因此,在我的例子中,我使用了一个链接按钮,并在单击时使用了一个服务器方法来加载和刷新网格

<asp:LinkButton ID="btnShowMore" class="ShowMoreLink" runat="server"
 OnClick="ShowMoreResults"></asp:LinkButton>
网格刷新方法:

private void RefreshGrid()
        {
              List<Employee> employees = GetEmployees(CountToDisplay)
              if (employees != null)
                {                    
                  empGrid.DataSource = employees;
                }
// **Hide the ShowMore link in case Count to display exceeds the total record.**
               btnShowMore.Visible = employees.Count > CountToDisplay;
               // Finally bind the GridView
               empGrid.DataBind();
      }
private void RefreshGrid()
{
List employees=GetEmployees(CountToDisplay)
如果(员工!=null)
{                    
empGrid.DataSource=员工;
}
//**如果要显示的案例数超过总记录,请隐藏ShowMore链接**
btnShowMore.Visible=employees.Count>CountToDisplay;
//最后绑定GridView
empGrid.DataBind();
}

因为您的要求是:您有一个按钮,上面写着“显示更多”,当单击该按钮时,将显示接下来的10多行

一种方法是使用
对象的
列表
,然后调用方法。如果您已经使用了
LINQ
中的
Take(n)
扩展名,也可以只返回所需数量的记录。我将
CountToDisplay
作为变量保存当前要显示的记录数,初始设置为0(零)

GetEmployees
方法

protected List<Employee> GetEmployees(int CountToDisplay)
{
List<Employee> employees= new List<employee>();

// Sample code to fill the List. Use the `Take` Extension
    dbDataContext db = new dbDataContext();
    var e= ( from c in db.Employees
            select c ).Take(CountToDisplay);

     //iterate through results and add to List<Employee>
     foreach(var c in e)
     {
         employee emp = new employee { name = c.name, address = c.address };
         employees.Add(emp);
     } 
     return employees;

}
现在是有趣的部分。假设您有一个“显示更多”按钮,当单击该按钮时,将显示接下来的10多行。这会一直持续到你到达终点。因此,在我的例子中,我使用了一个链接按钮,并在单击时使用了一个服务器方法来加载和刷新网格

<asp:LinkButton ID="btnShowMore" class="ShowMoreLink" runat="server"
 OnClick="ShowMoreResults"></asp:LinkButton>
网格刷新方法:

private void RefreshGrid()
        {
              List<Employee> employees = GetEmployees(CountToDisplay)
              if (employees != null)
                {                    
                  empGrid.DataSource = employees;
                }
// **Hide the ShowMore link in case Count to display exceeds the total record.**
               btnShowMore.Visible = employees.Count > CountToDisplay;
               // Finally bind the GridView
               empGrid.DataBind();
      }
private void RefreshGrid()
{
List employees=GetEmployees(CountToDisplay)
如果(员工!=null)
{                    
empGrid.DataSource=员工;
}
//**如果要显示的案例数超过总记录,请隐藏ShowMore链接**
btnShowMore.Visible=employees.Count>CountToDisplay;
//最后绑定GridView
empGrid.DataBind();
}

您可以像这样使用自定义页面网格:您可以像这样使用自定义页面网格: