Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add image button to Gridview (1st Column) dynamically from code behind file not from the .aspx file.. Please let me know if you need more details Image

Here is my code I created empty gridview without any columns:
HTML
<asp:GridView ID="gvTestData" runat="Server" AutoGenerateColumns="False" CellPadding="4"
           ForeColor="Black" GridLines="Horizontal"  ShowFooter="false"
           Height="110px" Style="margin-right: 0px" Width="990px" BackColor="White" OnRowDataBound="gvTestData_RowDataBound"
           AutoGenerateEditButton="True" OnRowEditing="gvTestData_RowEditing" OnRowCancelingEdit="gvTestData_RowCancelingEdit"
           OnRowUpdating="gvTestData_RowUpdating" OnRowDeleted="gvTestData_RowDeleted"
           BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
           OnRowCommand="gvTestData_RowCommand" onrowcreated="gvTestData_RowCreated">
           <Columns>


           </Columns>
           <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
           <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
           <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
           <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Justify"
               VerticalAlign="Middle" />
           <SortedAscendingCellStyle BackColor="#F7F7F7" />
           <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
           <SortedDescendingCellStyle BackColor="#E5E5E5" />
           <SortedDescendingHeaderStyle BackColor="#242121" />
           <EmptyDataTemplate>
               <table>
                   <tr style="background-color: #999999;">
                     <th>
                           Option
                       </th>
                       <th>
                           SlNo
                       </th>
                       <th>
                           IsRequired
                       </th>
                   </tr>
                   <tr>
                    <td headers="Option">
                           <asp:ImageButton ID="btnSave" runat="server" ImageUrl="images/save.ico" CausesValidation="false"
                               CommandName="FirstSaveRow" OnClick="btnSave_Click" />
                       </td>
                       <td headers="SlNo">
                       1
                       </td>
                       <td headers="IsRequired">
                           <asp:DropDownList ID="ddlFooterISRequired" runat="server" Visible="true">
                           <asp:ListItem Text="Yes" Value="Yes" />
                           <asp:ListItem Text="No" Value="No" />
                       </asp:DropDownList>
                       </td>
                   </tr>
               </table>
           </EmptyDataTemplate>
       </asp:GridView>



Code behind:
C#
    public void DynamicGridViewColumns(string[] strColumnNames)
    {

        if ((strColumnNames[0] != null))
        {
           
            if (!strColumnNames[0].Equals("SrNo", StringComparison.CurrentCultureIgnoreCase))
            {

                

//I have created a Gridview column from code behind file which is highlighted in bold characters with gridveiw column name options

                var cf = new CommandField();
                cf.HeaderText = "Options";
                //cf.ShowEditButton = true;
                gvTestData.Columns.Add(cf);

                BoundField columnTestDataID = new BoundField();
                columnTestDataID.DataField = "TestDataID";
                columnTestDataID.HeaderText = "TestDataID";

                gvTestData.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

                gvTestData.Columns.Add(columnTestDataID);

                BoundField columnSrNo = new BoundField();
                columnSrNo.DataField = "SrNo";
                columnSrNo.HeaderText = "Sr.No";

                gvTestData.HeaderStyle.HorizontalAlign = HorizontalAlign.Left ;

                gvTestData.Columns.Add(columnSrNo);
                BoundField columnIsReq = new BoundField();
                columnIsReq.DataField = "IsRequired";
                columnIsReq.HeaderText = "IsRequired";

                gvTestData.Columns.Add(columnIsReq);
            }
        }
   for (int i = 0; i <= strColumnNames.Length - 1; i++)
            {
                BoundField column = new BoundField(); //You can use the TemplateField object to add a template column
                column.HeaderText = strColumnNames[i];
                column.DataField = strColumnNames[i];


                gvTestData.Columns.Add(column);
               
            }
      
    }

 protected void gvTestData_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
// I am creating a image button which i need to add it in the 1st Column of gridveiw(Option column name)

            ImageButton imgBtn = new ImageButton();
            imgBtn.ID = "btnDelete";
            imgBtn.ImageUrl = "images/delete.ico";
            //TableCell tc = new TableCell();
            //tc.Controls.Add(imgBtn);
            //e.Row.Cells.AddAt(1, tc);
        }
    }

Do let me know if you need more information
Posted
Updated 25-Sep-13 11:58am
v7
Comments
Sergey Alexandrovich Kryukov 25-Sep-13 1:23am    
Please add a tag: ASP.NET. You are the one interested in that the most.
—SA
Shahul Hameed 25-Sep-13 1:32am    
thank you have done it
TryAndSucceed 1-Oct-13 12:56pm    
So, while debugging, were you able to come inside gvTestData_RowCreated(object sender, GridViewRowEventArgs e) Method?

1 solution

Add this to your code in gvTestData_RowCreated Method after assigning all attributes for your image button.

e.Row.Cells[YourImagePositionInGrid].Controls.Add(imgBtn);


It should work.

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900