Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I have a gridview with 3 column Sno, Name and age. Sno i am fetching from Database and in name and age column I just placed a textbox where user can enter them.
and i page load i am selecting first row and changing the background color too.
now when he moves to next row by mouse click i want the next row to be highlighted and insertable. and after entering all rows i want to save them at once.

i tried different codes for this but not working properly.

C#
protected void Page_Load(object sender, EventArgs e)
    {

        lQry = "Select SNo from sample";
        ds = clsGen.GetData(lQry);
        GridviewSample.DataSource = ds;
        GridviewSample.DataBind();
        if (!IsPostBack)
        {
            GridviewSample.SelectedIndex = 0;
            //GridviewSample.Rows[0].BackColor = System.Drawing.Color.LightGreen;
        }
    }


C#
protected void GridviewSample_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
      GridviewSample.Rows[GridviewSample.SelectedIndex].BackColor = System.Drawing.Color.LightGray;
  }


C#
protected override void Render(System.Web.UI.HtmlTextWriter writer)
   {
       foreach (GridViewRow row in GridviewSample.Rows)
       {
           if (row.RowType == DataControlRowType.DataRow)
           {
               row.Attributes["onmouseover"] =
                  "this.style.cursor='hand';this.style.textDecoration='underline';";
               row.Attributes["onmouseout"] =
                  "this.style.textDecoration='none';";
               // Set the last parameter to True
               // to register for event validation.
               row.Attributes["onclick"] =
                ClientScript.GetPostBackClientHyperlink(GridviewSample,
                   "Select$" + row.DataItemIndex, true);
           }
       }
       base.Render(writer);
   }


protected void GridviewSample_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState)5)
            {
                TextBox t1 = (TextBox)e.Row.Cells[1].Controls[0];
                TextBox t2 = (TextBox)e.Row.Cells[2].Controls[0];
                if (t1 != null & t2 != null)
                {
                    t1.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#DDDDDD'");
                    t1.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");


                    t2.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#DDDDDD'");
                    t2.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
                }
            }
        }
    }


This Is the design file.

XML
<asp:GridView ID="GridviewSample" runat="server" AutoGenerateColumns="false"
        onselectedindexchanged="GridviewSample_SelectedIndexChanged"
        onselectedindexchanging="GridviewSample_SelectedIndexChanging"
        SelectedRowStyle-BackColor="LightGray" ondatabound="GridviewSample_DataBound" >
    <Columns>
     <asp:BoundField DataField="SNo" HeaderText="SNo" />
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate >
     <asp:TextBox ID="txtname" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Age">
    <ItemTemplate >
     <asp:TextBox ID="txtage" runat="server" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
   </asp:GridView>


Please Help...
Posted

1 solution

You can use and change according to the requirement by adjusting SelectedRowStyle property of the Gridview.

Thanks
 
Share this answer
 
Comments
pinky1810 19-Dec-12 5:00am    
Thanks, I tried it but it is not working.in which event i can write this. because i am moving to next Row using mouse click. Can u please advice me.
Thanks in advance.
pinky1810 19-Dec-12 5:13am    
This code is working fine to change the selected row color but the problem is i can't enter anything in the textboxes of selected row, please help.

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
foreach (GridViewRow row in GridviewSample.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Attributes["onmouseover"] =
"this.style.cursor='hand';this.style.textDecoration='underline';";
row.Attributes["onmouseout"] =
"this.style.textDecoration='none';";
// Set the last parameter to True
// to register for event validation.
row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(GridviewSample,
"Select$" + row.DataItemIndex, true);
}
}
base.Render(writer);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900