c# - 获取gridview的行高

标签 c# asp.net

我在 Web 窗体中使用 c# asp.net,我想获取 GridView 的行高但不能,当我尝试获取它时总是显示 0。我使用了 Stack Overflow 中其他帖子的代码。

c#

foreach (GridViewRow row in gv.Rows)
{
   height = Convert.ToDecimal(row.Height.Value) + height;
}

html

<wc:ReportGridView ID="gv" runat="server" AutoGenerateColumns="false" AllowPrintPaging="true"
 CellPadding="4" ForeColor="Black" OnRowDataBound="gv_RowDataBound" ShowFooter="True"
tyle="font-family: 'Century Gothic'; font-size: small; z-index: -1; color: #000000;
 margin-top: 0px;" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px">

我看到所有评论都说他们可以工作,但为什么我不能?或者得到其他解决方案来获取行高,无论是 C# 还是 JavaScript。

最佳答案

gridview 中的行高大小可以变化,因此您可以获得特定行的高度。

int x = grdvw1.Rows[0].Height;

它也可以从行模板中获得:

int x = grdvw1.RowTemplate.Height

已更新-对于 WebForms,我认为您无法通过服务器端编程获得此高度,因为它取决于浏览器的渲染方式。您可以通过使用客户端编程来尝试。您仍然可以等待专家评论。如果客户端满足您的需求,您可以尝试以下代码。

<script>
    $(document).ready(function () {
        $("#Button1").click(function () {
            debugger;
            var a = $("#gvd").height();
            alert(a);
        });
    });
</script>

Gridview 应该在 gvd div 下。

<div id="gvd">

        <asp:gridview runat="server" ID="gv" runat="server" AutoGenerateColumns="False" AllowPrintPaging="true"
 CellPadding="4" ForeColor="Black"  ShowFooter="True" rowstyle Height="20px"
tyle="font-family: 'Century Gothic'; font-size: small; z-index: -1; color: #000000;
 margin-top: 0px;" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" 
            DataKeyNames="CorporateEmployeeId" DataSourceID="SqlDataSource1">
            <Columns>
               //Bounded columns
            </Columns>
        </asp:gridview>
        </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
            SelectCommand="SELECT * FROM [mytable]"></asp:SqlDataSource>
    </div>

关于c# - 获取gridview的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045148/

相关文章:

c# - 生成用户友好的 URL

c# - 为什么我的根证书不受信任?

c# - 即使文件很大,内存也较少的文件下载

.net - 跨多个选项卡窗口共享 ASP.NET session 的方法

c# - ForEach 扩展方法中的多重选择

asp.net - 如何在 KeyUp 上进行文本框回发?

c# - C# 构建的 .exe 是否可以 self 删除?

c# - Ninject C# 中的工厂方法

c# - 当前上下文中不存在名称

c# - 我可以从枚举中取出前 n 个元素,然后仍然使用枚举的其余部分吗?