troubleshooting Question

Select Gridview row without select button

Avatar of Prysson
Prysson asked on
C#ASP.NET
5 Comments1 Solution10259 ViewsLast Modified:
I am looking for a solution to allow me to select a gridview row without having a select button.

I looked at this solution

https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21734715.html

but when I insert the code
e.Row.Attributes.Add("onclick", e.Row.FindControl("btnSelectRow").ClientID & ".click();");


Operator & cannot be applied to operands of type string and string.


Someone on a different message board suggested
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
            Label1.Text = String.Format("You selected row '{0}'.", GridView1.SelectedDataKey["ID"]);
      }

BUt I dont know how to get that event handler to fire without a "Select" button.



This is my current code for my Gridview

            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="ContactID" Width="200px"
                    onrowdatabound="GridView2_RowDataBound" AllowSorting="true" onselectedindexchanged="GridView2_SelectedIndexChanged"
                    >
               
            <Columns>
                <asp:CommandField SelectText ="Select"  ShowSelectButton="true"  
                       ItemStyle-CssClass = "HiddenColumn" HeaderStyle-CssClass ="HiddenColumn"  />

                <asp:TemplateField HeaderText="ContactID" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="lblContactID" runat="server" Text='<%# Bind("ContactID")%>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="LastName">
                    <ItemTemplate>
                        <asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName") %>' ></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Font-Underline="True" HorizontalAlign="Left" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="FirstName">
                    <ItemTemplate>
                        <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName")%>' ></asp:Label>
                        &nbsp;&nbsp;
                    </ItemTemplate>
                    <HeaderStyle Font-Underline="True" HorizontalAlign="Left" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MI">
                    <ItemTemplate>
                       <asp:Label ID="lblMiddleInit" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>
                    </ItemTemplate>                    
                    <ControlStyle Width="100px" />
                    <HeaderStyle Font-Underline="True" HorizontalAlign="Left" />
                </asp:TemplateField>
                </Columns>
                        </asp:GridView>


And here is the code in teh code behind

 protected void Page_Load(object sender, EventArgs e)
        {
            DataTable contdt = contbll.GetContactsData_();
            DataView contdv = new DataView(contdt);

            this.GridView2.DataSource = contdv;
            this.GridView2.DataBind();
           
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("onclick", e.Row.FindControl("btnSelectRow").ClientID & ".click();");
                e.Row.Attributes.Add("onmouseover", "Highlight_On(this);");
                e.Row.Attributes.Add("onmouseout", "Highlight_Off(this);");
                e.Row.Attributes["OnClick"] = ClientScript.GetPostBackEventReference(this, "Select$" + e.Row.RowIndex);
                e.Row.Style["cursor"] = "hand";
            }

        }


        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int colid = int.Parse(GridView2.Columns[1].ToString());
            int contid = int.Parse(GridView2.SelectedIndex.ToString());
            if (colid == contid)
            {
            }
 
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros