

protected void BindGridData1()
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("Select * from FormTypeHeader ORDER BY fh_order", connString);
DataSet ds = new DataSet();
da.Fill(ds);
GridView3.DataBind();
GridViewRow FirstRow = GridView3.Rows[0];
ImageButton ImageUp = (ImageButton)FirstRow.FindControl("ImageUP");
ImageUp.Enabled = false;
GridViewRow LastRow = GridView3.Rows[GridView3.Rows.Count - 1];
ImageButton ImageDown = (ImageButton)LastRow.FindControl("ImageDOWN");
ImageDown.Enabled = false;
}
protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = 0;
GridViewRow gvrow;
GridViewRow previousRow;
if (e.CommandName == "Up")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = GridView3.Rows[index];
previousRow = GridView3.Rows[index - 1];
int headerorder =
Convert.ToInt32(GridView3.DataKeys[gvrow.RowIndex].Values[0].ToString());
int fh_id = Convert.ToInt32(gvrow.Cells[1].Text);
int previousId = Convert.ToInt32(previousRow.Cells[1].Text);
connString.Open();
da.UpdateCommand = new SqlCommand("update form_headerA set fh_order='" +
(headerorder - 1) + "' where fh_id='" + fh_id + "'; update form_headerA set
fh_order='" + (headerorder) + "' where fh_id='" + previousId + "'",
connString);
da.UpdateCommand.ExecuteNonQuery();
connString.Close();
}
if (e.CommandName == "Down")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = GridView3.Rows[index];
previousRow = GridView3.Rows[index + 1];
int headerorder = Convert.ToInt32(GridView3.DataKeys[gvrow.RowIndex].Values[0].ToString());
int fh_id = Convert.ToInt32(gvrow.Cells[1].Text);
int previousId = Convert.ToInt32(previousRow.Cells[1].Text);
connString.Open();
da.UpdateCommand = new SqlCommand("update form_headerA set fh_order='" + (headerorder + 1) + "' where fh_id='" + fh_id + "'; update form_headerA set
fh_order='" + (headerorder) + "' where fh_id='" + previousId + "'",
connString);
da.UpdateCommand.ExecuteNonQuery();
connString.Close();
}
if (e.CommandName == "Exclude")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int fh_id =
Convert.ToInt32(GridView3.DataKeys[row.RowIndex].Values["fh_id"].ToString());
connString.Open();
da.DeleteCommand = new SqlCommand("DELETE FROM form_headerA WHERE fh_id ='" +
fh_id + "'", connString);
da.DeleteCommand.ExecuteNonQuery();
connString.Close();
}
BindGridData1();
BindGridData();
}
Jignesh TrivediPosted Apr 15, 2013, 11:32 PM
hi,
here problem is, your are accessing data row of grid view but grid view is not bind with data source so grid view has no row and you trying to access row(0).
instead of doing write your code in row RowDataBound event or Load event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.control.load.aspx
hope this will help you.