<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>
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); } }
e.Row.Cells[YourImagePositionInGrid].Controls.Add(imgBtn);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)