0

这就是我在 ASP.NET C# 项目中创建网格视图的方式。该网格有 4 列。我想添加第 5 列并在每一行上添加一个按钮。我怎样才能做到这一点 ?

<asp:GridView ID="gv" runat="server"  CellPadding="1" Width="900px"/>
4

2 回答 2

0
  1. AutoGenerateColumns="False"在 gridview 标记中设置
  2. 将列定义为BoundField(或TemplateField如果您愿意)
  3. TemplateField为最后一列的按钮添加一个

总结:

<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px">
    <Columns>
        <%-- <asp:BoundField /> Definitions here --%>
        ...
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button Text="Click ME" runat="server" ID="btn" OnClick="Clicked" />        
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
于 2013-05-21T08:43:31.303 回答
-1

suppose you are binding data table having 4 column say col1,col2,col3 and col4

then your gird view in .aspx will be like;

<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1"                 Width="900px" OnRowCommand="gv_RowCommand">
<Columns>
    <asp:TemplateField HeaderText="col1">
        <ItemTemplate>
            <%#Eval("col1")%>// "col1" is field of your database
        </ItemTemplate>
    </asp:TemplateField>  

    <asp:TemplateField HeaderText="col2">
        <ItemTemplate>
            <%#Eval("col2")%>
        </ItemTemplate>
    </asp:TemplateField> 

    <asp:TemplateField HeaderText="col3">
        <ItemTemplate>
            <%#Eval("col3")%>
        </ItemTemplate>
    </asp:TemplateField>                     

    <asp:TemplateField HeaderText="col4">
        <ItemTemplate>
            <%#Eval("col4")%>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="btnOK" runat="server" Text="OK"  CommandName="show"  />   
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

*.cs page will be *

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName.ToLower() == "show")
  {
    //your code on click event 
  }
}
于 2013-05-21T09:28:32.680 回答