I am binding data to a gridview. Then I am adding a row at the top of the gridview. As soon as I click any column(hyperlink), the gridview gives me the id of the previous row.
The aspx file
pyramid_jinja2
CS File
<asp:GridView ID="gridview1" runat="server"
OnRowCommand="OnRowCommand" OnPreRender="OnPreRender" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="EditImage" src="image/clickme.png"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
--Adding the First Row of textboxes and then Binding the image to the value of id (FIRST COLUMN VALUE)
--Binding data to the gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
gridview1.Columns.Add(bfield);
}
}
}
--Clicking on image and getting the value of previous row
protected void OnPreRender(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < gridview1.Columns.Count; i++)
{
TableHeaderCell cell = new TableHeaderCell();
TextBox txtSearch = new TextBox();
txtSearch.Attributes["placeholder"] = gridview1.Columns[i].HeaderText;
txtSearch.CssClass = "search_textbox";
row.Controls.Add(cell);
}
gridview1.Controls[0].Controls.AddAt(0, row);
for (int i=0; i <gridview1.Rows.Count; i++)
{
GridViewRow row = gridview1.Rows[i];
var imgBtn = (ImageButton)row.FindControl("EditImage");
imgBtn.CommandArgument = row.Cells[1].Text; //GET THE ID(VALUE IN FIRST COLUMN)
}
}