简体   繁体   中英

How to make gridview last column Button

I am trying to make gridview last column fields Button.

Trying but could not do this.

It is possible while adding column in gridview using sql data source. But to do this using this binding method.

Here is my code-

 private void BindUserRoles()
    {
        gvUserRoles.DataSource = UserRoles.GetAllRoles();
        gvUserRoles.DataBind();
    }
public List<UserRoles> GetAllRoles()
    {
        try
        {
            List<UserRoles> userRoles = new List<UserRoles>();

            using (IDataAccess dataAccess = Mspl.MobileTracking.DataAccess.DataAccess.GetDataAccess("TrackingConnectionString"))
            {
                var dataReader = dataAccess.RetrieveData("GetAllRoles", null);

                while (dataReader.Read())
                {
                    UserRoles roles = new UserRoles();
                    roles.RoleId = dataReader["RoleId"].ToString();
                    roles.RoleName = dataReader["RoleName"].ToString();

                    userRoles.Add(roles);
                }
            }

            return userRoles;
        }
        catch (Exception ex)
        {

            return null;
        }

    }



<asp:GridView ID="gvUserRoles" runat="server" EnableModelValidation="True" 
</asp:GridView>

Set AutoGeneratedColumn=false and add templatefields like shown below.

Change your grid code like this-

<Columns>
                <asp:BoundField DataField="RoleId" HeaderText="RoleId" ItemStyle-CssClass="HideColumn" HeaderStyle-HorizontalAlign="Left"/>
                <asp:TemplateField HeaderText="Role Name" HeaderStyle-CssClass="normalText">
                    <ItemTemplate>
                        <asp:Label ID="lblRoleName" CssClass="normalText" runat="server" Text='<%# Bind("RoleName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnEditRole" runat="server" Text="Edit" OnClick="EditRoles_Click"></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

Just replace your current "last" column with a ButtonField , for example:

<asp:ButtonField ButtonType="button" CommandName="MoreDetail" 
         HeaderText="More Details" Text="More Details" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM