0

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>
Azhar Shahid
  • 151
  • 1
  • 4
  • 23
  • Yes it is possible, please show UI code, are you adding columns from code behind or from UI itself? – Incredible May 28 '14 at 10:57
  • @Iti- adding from code behind, not from UI – Azhar Shahid May 28 '14 at 11:00
  • http://stackoverflow.com/questions/2418379/how-do-i-programmatically-add-a-button-to-a-gridview-and-assign-it-to-a-specific And there is one more link : http://stackoverflow.com/questions/10397856/add-aspbutton-from-codebehind – Incredible May 28 '14 at 11:01
  • Or maybe this is also helpfull: http://stackoverflow.com/questions/16398555/how-to-add-controlls-linkbuttons-on-runtime-into-a-gridview-templatefield – DatRid May 28 '14 at 11:02
  • Please try below link http://stackoverflow.com/questions/11971035/add-boundfield-to-gridview-in-codebehind-file-c-sharp and http://www.codeproject.com/Articles/13461/how-to-create-columns-dynamically-in-a-grid-view – Deepak Joshi May 28 '14 at 11:13

3 Answers3

1

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>

0

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

<asp:ButtonField ButtonType="button" CommandName="MoreDetail" 
         HeaderText="More Details" Text="More Details" />
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
0

Please try below link

add boundField to gridview in codebehind file C#

Code project link

http://www.codeproject.com/Articles/13461/how-to-create-columns-dynamically-in-a-grid-view

Community
  • 1
  • 1
Deepak Joshi
  • 1,036
  • 7
  • 17