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.
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;
}
}
protected void GridviewSample_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridviewSample.Rows[GridviewSample.SelectedIndex].BackColor = System.Drawing.Color.LightGray;
}
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';";
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.
<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...