Hello there.
With this code the value of DataKeyNames is dynamically assigned in the gridview
string sentence = ddl.SelectedValue;
string[] words = sentence.Split('-');
int id = 0;
if (words[1].ToString().Contains("36"))
{
gvProducts.DataKeyNames = new string[] { "ID" };
}
else
{
gvProducts.DataKeyNames = new string[] { "sID" };
GridViewRow gvrow = (sender as ImageButton).NamingContainer as GridViewRow;
id = Convert.ToInt32(gvProducts.DataKeys[gvrow.RowIndex].Values[0]);
}
Now i need to edit the gridview using an ImageButton and open in window popup new aspx page for editing row
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string sentence = ddl.SelectedValue;
string[] words = sentence.Split('-');
int id = 0;
if (words[1].ToString().Contains("36"))
{
gvProducts.DataKeyNames = new string[] { "ID" };
}
else
{
gvProducts.DataKeyNames = new string[] { "sID" };
GridViewRow gvrow = (sender as ImageButton).NamingContainer as GridViewRow;
id = Convert.ToInt32(gvProducts.DataKeys[gvrow.RowIndex].Values[0]);
}
Thread.Sleep(3000);
string queryString = "edit.aspx?sID=" + id.ToString();
string newWin = "var Mleft = (screen.width/2)-(1200/2);" +
"var Mtop = (screen.height/2)-(400/2);" +
"window.open('" + queryString + "','_blank','height=400,width=1200," +
"status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no," +
"top=\'+Mtop+\', left=\'+Mleft+\';');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OpenWindow", newWin, true);
}
But i have error on this line
id = Convert.ToInt32(gvProducts.DataKeys[gvrow.RowIndex].Values[0]);
error
System.FormatException: 'The format of the input string is incorrect.'
help to do it, please.