Dynamic Single click , Double Click for Editing the rows in Gridview

Majith
Posted by in ASP.NET category on for Intermediate level | Views : 23220 red flag

I am going to expalin how to perform the editing options through sigle click or double click on Gridview or DataGrid.
Dynamically Clicking (Single click or Double Click) for Editing the rows in Gridview
Introduction

I am going to expalin how to perform the editing options through sigle click or double click on Gridview or DataGrid.Normally if we want to edit the row we will create a modify button or using checkbox , however using javascript we can perform easily by single or double clicking the rows.



Functionalities

use the following two Gridview events

CodeBehind file

1.Gridview1_RowCommand

2.GridView1_RowDataBound


CodeBehind file

1.Gridview1_RowCommand

Protected void protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        selectedIndex = int.Parse(e.CommandArgument.ToString());
        string _commandName = e.CommandName;
        switch (_commandName)
        {
            case ("SingleClick"):
                {
                    Gridview1.SelectedIndex = selectedIndex;
                    object send = new object();
                    GridViewEditEventArgs ex = new GridViewEditEventArgs(selectedIndex);
                    Gridview1_RowEditing(send, ex);
                    break;
                }
        }
    }

2.Gridview1_RowDataBound

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
                string _jsDouble = ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
                e.Row.Attributes["ondblclick"] = _jsDouble;
                e.Row.Attributes["onmouseover"] = "javascript:return setMouseOverColor(this);";
                e.Row.Attributes["onmouseout"] = "javascript:return setMouseOutColor(this);";    }  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                string jsCommand = string.Format("if ({2} != {1}) __doPostBack('{0}','Edit${1}')", Gridview1.ID, e.Row.RowIndex, Gridview1.EditIndex);
                foreach (TableCell c in e.Row.Cells)
                {
                    if (c == e.Row.Cells[1])
                    {
                    }
                    else
                    {
                        c.Attributes["onclick"] += jsCommand;
                    }
                    }
                    }
                   }
        catch(Exception ex)
        {
            throw ex;
        }   }
Html Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
          OnRowEditing="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand"
          OnRowDataBound="GridView1_RowDataBound" AllowSorting="True" PageSize="5">
   <columns>
    add the appropriate columns
    </columns>
   </asp:GridView>
 Conclusion
 

Hope you all understand the above method for editing the rows in GridView.


 
Page copy protected against web site content infringement by Copyscape

About the Author

Majith
Full Name: Majith Basha
Member Level:
Member Status: Member
Member Since: 7/18/2008 11:49:59 PM
Country:



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)