How to implement full row selecting in GridView without select button?
You must add this on every postback and not only on databinding. Therefore you should use the RowCreated-Event of the GridView.
For example
(C#):
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
(VB.Net):
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Row.ToolTip = "Click to select row"
e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
How to select and unselect the row in the GridView without having select button?
Ali, this is a common thing to want to do and a simple Google search will return several excellent tutorials on how to achieve it.
Basically, you can add the following code to your RowDataBound event:
if (e.row.RowType == DataControlRowType.DataRow) {
e.row.Attributes["onmouseover"] =
"this.style.cursor='hand';";
e.row.Attributes["onmouseout"] =
"this.style.textDecoration='none';";
// Set the last parameter to True
// to register for event validation.
e.row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(CustomerGridView,
"Select$" + row.RowIndex, true);
}
Complete guide here: http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx
Selecting a Gridview row without a select column in ASP.NET
I think you should use jQuery for this.
Here is a good example
Asp net grid view select row using jquery
Here too is a good example
http://www.codedigest.com/Articles/ASPNET/324_Make_GridView_Row_Selectable_or_Clickable_using_jQuery_in_ASPNet.aspx
Select a row without set AutoGenerateSelectButton to true
This is an average gridview with no select button. The RowDataBound method makes your grid highlightable. You can check if a certain column was selected possibly with if conditions in your SelectedIndexChanged method as that will tell you what row was selected and then you can get a value from a specific column based on the order your columns were made as shown below. This wont show exactly where they clicked x and y but should head you in the right direction I think. This will however get column data based on what row was highlighted and clicked on with a post back of course.
<asp:GridView ID="PropertyGridView" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="PropertyGridView_SelectedIndexChanged" OnRowDataBound="PropertyGridView_RowDataBound">
<RowStyle HorizontalAlign="Left" />
<EmptyDataTemplate>
<div>
<asp:Label runat="server" ID="EtLbl" Text="No properties were found."</asp:Label>
</div>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="Name" HeaderText="User Name" Width="20%" /> </asp:BoundField>
<asp:BoundField DataField="Age" HeaderText="User Age" Width="20%" /> </asp:BoundField>
<asp:BoundField DataField="Height" HeaderText="User Height" Width="20%" /> </asp:BoundField>
</Columns>
</asp:GridView>
public void PropertyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex == -1)
return;
e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';");
e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(PropertyGridView,"Select$" + e.Row.RowIndex.ToString()));
}
public void PropertyGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string Name = PropertyGridView.SelectedRow.Cells[0].Text;
string Age= PropertyGridView.SelectedRow.Cells[1].Text;
string Height PropertyGridView.SelectedRow.Cells[2].Text;
if( Height != null)
{
// The user selected a row and this Column ( Height has data )
}
}
How to select a row without using OnSelectedIndexchanged in asp.net
You could simply add a custom button to the GridView
whose CommandName
property is Select
. Then use the SelectedIndexChanged
event as usual.
Why do you say you don't want the SelectedIndexChanged
event to fire? Note that this event will fire even if you change the selected index in code by calling the GridView.SelectRow
method.
hiding the Select button in gridview
Have you considered hiding the column with CSS? This can be done in the RowDataBound
event:
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Style["display"] = "none";
// or e.Row.Cells[0].CssClass = "hidden-cell";
}
Button pressed on grid view without selection
You need to check if there is any row selected in your DataGridView
control. If there is you can delete the first selected or all selected. The following snippet is demonstrating deleting of the only one selected row. Also you should pay attention not to try to delete the new row that is uncommitted. Therefore, you could use the lines:
DataGridViewRow dgrvr = numberSettingTable.SelectedRows[0];
if(!dgrvr.IsNewRow)
The full code snippet is like this:
if(numberSettingTable.SelectedRows.Count>0)
{
DataGridViewRow dgrvr = numberSettingTable.SelectedRows[0];
if(!dgrvr.IsNewRow)
numberSettingTable.Rows.Remove(dgrvr);
}
in order to delete multiple selected rows, just iterate trough SelectedRows collection.
Related Topics
How to Clear Browser Cache on Browser Back Button Click in MVC4
Inheritance on a Constrained Generic Type Parameter
Breakpoint Failed to Bind - Visual Studio 2015
Interaction Between Webbrowser Control and Windows Forms
How to Add the Same Column to All Entities in Ef Core
Does Dispose Still Get Called When Exception Is Thrown Inside of a Using Statement
System.Valuetype Understanding
Client-Side Validation in Custom Validation Attribute - ASP.NET MVC 4
Gracefully Handling Corrupted State Exceptions
Convert Time Span Value to Format "Hh:Mm Am/Pm" Using C#
Patch Async Requests with Windows.Web.Http.Httpclient Class
Stack Overflow Exception in C# Setter
Project Builds But Can't Publish
Generating a Random Decimal in C#