首页 > 解决方案 > 如果行没有数据 Asp.net c#,如何在 Gridview 中显示按钮链接

问题描述

如果行没有数据,我对 gridview 中的不可见链接按钮有问题。

这是我的代码的结果

我的 HTML 代码:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        Width="700px" AllowPaging="True" 
        onpageindexchanging="GridView1_PageIndexChanging" DataKeyNames="ProductId" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
     <Columns>
            <asp:ButtonField Text="view" CommandName="Select">
                <ItemStyle Width="100px" />
            </asp:ButtonField>
            <asp:BoundField HeaderText="ProductId" DataField="ProductId" >
                 <ItemStyle Width="100px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="ProductName" DataField="ProductName">
                <ItemStyle Width="300px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField HeaderText="QuantityPerUnit" DataField="QuantityPerUnit">
                <ItemStyle Width="200px"></ItemStyle>
            </asp:BoundField>
   </Columns>
   </asp:GridView>

我的 Aspx 代码:

    public void BindData()
    {
        string strConnection = @"Data Source=.\sa;Initial Catalog=Northwind;Integrated Security=SSPI;";

        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select ProductId, ProductName, SupplierId from Products", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        //Script row 10
        int rowcount = ds.Tables[0].Rows.Count;
        int remainingCount = 10 - (rowcount % 10);
        for (int i = 0; i < remainingCount; i++)
        {
            DataRow row = ds.Tables[0].NewRow();
            ds.Tables[0].Rows.Add(row);
        }

        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();  
    }

任何人都可以改进我的代码,拜托..

标签: c#asp.netgridview

解决方案


如果我正确理解了问题,您可以通过 rowdatabound 解决它。 https://docs.microsoft.com/ru-ru/dotnet/api/system.web.ui.webcontrols.gridview.rowdatabound?view=netframework-4.8


推荐阅读