For formatting the selected row (to highlight the selected row), add below code in the "gvStudents_SelectedIndexChanged" event of the GridView:
// Get the currently selected row using the SelectedRow property.
GridViewRow SelectedRow = gvStudents.SelectedRow;
// Highlight the selected row
foreach (GridViewRow GVR in gvStudents.Rows)
{
if (GVR == SelectedRow)
GVR.Style.Add("background-color", "#FCD85C;");
else
GVR.Style.Clear();
}
Sumit Pathak ------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback
or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Then when I set the EnableEventValidation to false in the page directive, nothing is happening on row click.
Yes, the gvStudents_SelectedIndexChanged event is getting called. But the count of cells in the selected row is displayed as 1 eventhough there are 6 columns and the value is displayed as null.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 20, 2012 10:55 AM|LINK
Hi,
I have modified your code to make it work.
@ ASPX:
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False" AllowPaging="True" Width="315%" OnPageIndexChanging="gvStudents_PageIndexChanging" OnRowDataBound="gvStudents_RowDataBound" OnSelectedIndexChanged="gvStudents_SelectedIndexChanged"> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" runat="server">Select</asp:LinkButton> </ItemTemplate> <ItemStyle Width="30%" /> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="lblName" Text='<%#DataBinder.Eval(Container.DataItem, "StudentName")%>' runat="server" ></asp:Label> </ItemTemplate> <ItemStyle Width="30%" /> </asp:TemplateField> <asp:TemplateField HeaderText="Place"> <ItemTemplate> <asp:Label ID="lblPlace" runat="server" Text='<%#Eval("Place")%>'></asp:Label> </ItemTemplate> <ItemStyle Width="30%" /> </asp:TemplateField> <asp:TemplateField HeaderText="Pincode"> <ItemTemplate> <asp:Label ID="lblPincode" runat="server" Text='<%#Eval("Pincode") %>'></asp:Label> </ItemTemplate> <ItemStyle Width="20%" /> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label ID="lblEmail" runat="server" Text='<%#Eval("EmailId") %>'></asp:Label> </ItemTemplate> <ItemStyle Width="20%" /> </asp:TemplateField> </Columns> </asp:GridView>@ CS:
protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnkEdit = (LinkButton)e.Row.FindControl("lnkEdit"); lnkEdit.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvStudents, "Select$" + e.Row.RowIndex.ToString()); } } catch (Exception ex) { Response.Write(ex.ToString()); } } protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e) { try { // Get the currently selected row using the SelectedRow property. GridViewRow SelectedRow = gvStudents.SelectedRow; Label lblName = (Label)SelectedRow.FindControl("lblName"); TextBox1.Text = lblName.Text; } catch (Exception ex) { Response.Write(ex.ToString()); } }Try this and let me know in case of any more issues...
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
285 Posts
Re: How to select a row from GridView
Feb 20, 2012 11:25 AM|LINK
Hi, Thanks for the reply.
Is it necessary to add a link button to select data? Is there no way to select data on click of any row in gridview, without adding any link button ?
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 20, 2012 11:42 AM|LINK
Yes, there are lots of other ways to select a Row w/o LinkButton and one of them includes by directly clicking anywhere on a Row to be selected.
From GridView, remove the TemplateField for Select LinkButton and replace the code in "gvStudents_RowDataBound" with below code:
protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.DataRow) e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvStudents, "Select$" + e.Row.RowIndex.ToString()); } catch (Exception ex) { Response.Write(ex.ToString()); } }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 20, 2012 11:45 AM|LINK
For formatting the selected row (to highlight the selected row), add below code in the "gvStudents_SelectedIndexChanged" event of the GridView:
// Get the currently selected row using the SelectedRow property. GridViewRow SelectedRow = gvStudents.SelectedRow; // Highlight the selected row foreach (GridViewRow GVR in gvStudents.Rows) { if (GVR == SelectedRow) GVR.Style.Add("background-color", "#FCD85C;"); else GVR.Style.Clear(); }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
285 Posts
Re: How to select a row from GridView
Feb 20, 2012 04:22 PM|LINK
Hi,
Thanks for the reply. I wrote the code as you specified.
protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvStudents, "Select$" + e.Row.RowIndex.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
But when clicked on any row, it is showing error
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Then when I set the EnableEventValidation to false in the page directive, nothing is happening on row click.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 21, 2012 04:35 AM|LINK
Debug the code and check whether the SelecedIndexChage event of the GridView is getting called or not.
Also, in the SelecedIndexChage event, check whether GridView is already populated or not.
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
285 Posts
Re: How to select a row from GridView
Feb 21, 2012 04:49 AM|LINK
Yes, the gvStudents_SelectedIndexChanged event is getting called. But the count of cells in the selected row is displayed as 1 eventhough there are 6 columns and the value is displayed as null.
This is my code in teh presentation layer
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindGrid();
}
private void BindGrid()
{
sBAL = new studentBAL();
gvStudents.DataSource = sBAL.BALStudentSelect();
gvStudents.DataBind();
}
protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = gvStudents.SelectedRow.Cells[0].Text;
}
==============
In Business Layer
public DataTable BALStudentSelect()
{
dt = new DataTable();
dr = sDAL.DALStudentSelect();
dt.Load(dr);
return dt;
}
In Data Layer
public SqlDataReader DALStudentSelect()
{
dbcnnString.Open();
SqlCommand cmdSelect = new SqlCommand("spRegistrationSelect", dbcnnString);
SqlDataReader dr = cmdSelect.ExecuteReader();
dr.Read();
return dr;
}
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 21, 2012 05:43 AM|LINK
Remove the condition of Page.IsPostBack and try..
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
285 Posts
Re: How to select a row from GridView
Feb 21, 2012 11:12 AM|LINK
Hi,
I removed the condition on pageload. But still the issue is there.
The text is null.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 21, 2012 11:54 AM|LINK
Instead of:
Use:
protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e) { try { // Get the currently selected row using the SelectedRow property. GridViewRow SelectedRow = gvStudents.SelectedRow; Label lblName = (Label)SelectedRow.FindControl("lblName"); txtName.Text = lblName.Text; } catch (Exception ex) { Response.Write(ex.ToString()); } }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"