c# - 如果行没有数据 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();
}
任何人都可以改进我的代码,拜托..
解决方案
如果我正确理解了问题,您可以通过 rowdatabound 解决它。 https://docs.microsoft.com/ru-ru/dotnet/api/system.web.ui.webcontrols.gridview.rowdatabound?view=netframework-4.8
推荐阅读
- ios - 如何在 SwiftUI 中位于 Apple Files App 的文件夹中写入文件?
- flutter - Flutter:多个小部件使用相同的 GlobalKey
- r - 如何在 R 中的 x 上绘制日期时间?
- java - Eclipse jdk 11 问题:java.lang.String 类型无法解析。它是从所需的 .class 文件中间接引用的
- python - Discord.py 在随机命令中发送奇怪的消息格式
- mysql - MySQL 可以允许缺少参考目标吗?
- docker - 如何访问 docker 上下文之外的目录才能访问 nuget/项目?
- javascript - 如何使用纯 JavaScript(无 JQuery)在两个 div 之间放置一个 div
- java - Java 优雅地按数组拆分字符串
- django - Django 频道 - 在连接时发送数据