Problem with moving the rows up and down in a GridView
Hi,
I have a GridView and want to enable the user to move the rows up and down. Here is the code I have which should do that, but the rows don't move up or down:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string Qry = string.Empty;
if (e.CommandName == "Up")
{
int index = Convert.ToInt32(e.CommandArgument);
if (index == 0)
{
Label3.Text = "You Cant move record' Up";
Label3.Visible = true;
return;
}
GridViewRow row = GridView1.Rows[index];
Label LblinID = (Label)row.FindControl("lblid");
conn.Open();
Qry = "Select RedBr from dbTable where RedBr = ''" + LblinID.Text + "''";
SqlCommand Command1=new SqlCommand(Qry,conn);
string SeqNo = Command1.ExecuteScalar().ToString();
conn.Close();
if (Convert.ToInt16(SeqNo) > 1)
{
Qry = "Select RedBr from dbTable where RedBr = ''" + (Convert.ToInt16(SeqNo) - 1) + "''";
conn.Open();
string PreDisplayID = Command1.ExecuteScalar().ToString();
conn.Close();
Qry = "Update dbTable Set RedBr = ''" + (Convert.ToInt16(SeqNo) - 1) + "'' Where RedBr = ''" + LblinID.Text + "'';";
Qry += "Update dbTable Set RedBr = ''" + Convert.ToInt16(SeqNo) + "''Where RedBr = ''" + Convert.ToInt16(PreDisplayID) + "'';";
conn.Open();
Command1.ExecuteNonQuery();
conn.Close();
bind();
}
}
if (e.CommandName == "Down")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label LblinID = (Label)row.FindControl("lblid");
conn.Open();
Qry = "Select Max(RedBr) from dbTable";
SqlCommand Command1 = new SqlCommand(Qry, conn);
string MaxSeqNo = Command1.ExecuteScalar().ToString();
conn.Close();
if (Convert.ToInt16(index + 1) == Convert.ToInt16(MaxSeqNo))
{
Label3.Text = "You Cant move record down";
Label3.Visible = true;
return;
}
Qry = "Select RedBr from dbTable where RedBr = ''" + LblinID.Text + "''";
conn.Open();
string SeqNo = Command1.ExecuteScalar().ToString();
conn.Close();
if (Convert.ToInt16(MaxSeqNo) > Convert.ToInt16(SeqNo))
{
Qry = "Select RedBr from dbTable where RedBr = ''" + (Convert.ToInt16(SeqNo) + 1) + "''";
conn.Open();
string PostDisplayID = Command1.ExecuteScalar().ToString();
conn.Close();
Qry = "Update dbTable Set RedBr = ''" + (Convert.ToInt16(SeqNo) + 1) + "'' Where RedBr = ''" + LblinID.Text + "'';";
Qry += "Update dbTable Set RedBr = ''" + Convert.ToInt16(SeqNo) + "'' Where RedBr = ''" + Convert.ToInt16(PostDisplayID) + "'';";
conn.Open();
Command1.ExecuteNonQuery();
conn.Close();
bind();
}
}
}
Could anybody help me please, and tell me where I go wrong? I have a feeling that it even doesn't enter the GridView1_RowCommand block, since when I put Response.Write(index) at the beginning it doesn't shows when I view the site in browser.???
Thank you very much in advance.