gridview up down control

Posted by Rishi7588 under ASP.NET on 8/17/2009 | Views : 11648 | Status : [Member] | Replies : 1
hi friends i need following req, in my gridview it shows following details like name ,address,image....there i used up and down button...
if i click the up button the corresponding row details move to previous row and the same case for down button also.....i need this urgent...




Responses

Posted by: Lakhangarg on: 8/18/2009 [Member] [Moderator] Silver

Up
0
Down
HTML Code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnMoveUp" Text="UP" runat="server" CommandName="Edit" CommandArgument="up" />
<asp:Button ID="btnMoveDown" Text="Down" runat="server" CommandName="Edit" CommandArgument="down"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = CreateDataTbale();
GridView1.DataBind();
}
}
private DataTable CreateDataTbale()
{
DataTable dtNew = new DataTable("Test Data");
DataColumn dcName = new DataColumn("Name");
dtNew.Columns.Add(dcName);
DataColumn dcCountry = new DataColumn("Country");
dtNew.Columns.Add(dcCountry);
DataColumn dcCity = new DataColumn("City");
dtNew.Columns.Add(dcCity);


DataRow drNew;
drNew = dtNew.NewRow();
drNew["Name"] = "Lakhan Pal Garg";
drNew["Country"] = "India";
drNew["City"] = "Chandigarh";
dtNew.Rows.Add(drNew);

drNew = dtNew.NewRow();
drNew["Name"] = "Naveen Kumar Garg";
drNew["Country"] = "India";
drNew["City"] = "Dharamkot";
dtNew.Rows.Add(drNew);

drNew = dtNew.NewRow();
drNew["Name"] = "Mohit Bajaj";
drNew["Country"] = "USA";
drNew["City"] = "New York";
dtNew.Rows.Add(drNew);
return dtNew;
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
ViewState["type"] = e.CommandArgument.ToString();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Write(GridView1.Rows.Count.ToString());
if (ViewState["type"].ToString() == "up")
{
if (e.NewEditIndex > 0)
{
string Name = ((Label)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblName")).Text;
string Country = ((Label)GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lblCountry")).Text;
string City = ((Label)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("lblCity")).Text;

((Label)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblName")).Text=((Label)GridView1.Rows[e.NewEditIndex-1].Cells[0].FindControl("lblName")).Text;
((Label)GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lblCountry")).Text = ((Label)GridView1.Rows[e.NewEditIndex - 1].Cells[1].FindControl("lblCountry")).Text;
((Label)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("lblCity")).Text = ((Label)GridView1.Rows[e.NewEditIndex - 1].Cells[2].FindControl("lblCity")).Text;

((Label)GridView1.Rows[e.NewEditIndex-1].Cells[0].FindControl("lblName")).Text=Name;
((Label)GridView1.Rows[e.NewEditIndex - 1].Cells[1].FindControl("lblCountry")).Text = Country;
((Label)GridView1.Rows[e.NewEditIndex - 1].Cells[2].FindControl("lblCity")).Text = City;
}
}
else if (ViewState["type"].ToString() == "down")
{

if (e.NewEditIndex < GridView1.Rows.Count)
{
string Name = ((Label)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblName")).Text;
string Country = ((Label)GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lblCountry")).Text;
string City = ((Label)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("lblCity")).Text;

((Label)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblName")).Text = ((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[0].FindControl("lblName")).Text;
((Label)GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lblCountry")).Text = ((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[1].FindControl("lblCountry")).Text;
((Label)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("lblCity")).Text = ((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[2].FindControl("lblCity")).Text;

((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[0].FindControl("lblName")).Text = Name;
((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[1].FindControl("lblCountry")).Text = Country;
((Label)GridView1.Rows[e.NewEditIndex + 1].Cells[2].FindControl("lblCity")).Text = City;
}
}

}

Rishi7588, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response