I have a GridView with users and I want to be able to select the first column of a row – which is with usernames. I know how to do it with the autogenerateselectbutton property, but it doesn’t look well on my design. So instead, I want to make it without the Select button. Here is what I have so far:
<asp:GridView ID="GridView5" AllowPaging="true" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle="alt" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
Width="750px"
CausesValidation="False" OnPageIndexChanging="gridView5_PageIndexChanging" OnSelectedIndexChanged="gridView5_SelectedIndexChanged" autogenerateselectbutton="True" >
<Columns>
<asp:BoundField DataField="username" HeaderText="Username" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="lastname" HeaderText="Last name" />
</Columns>
<selectedrowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-bold="true"/> </asp:GridView>
If I remove the autogenerateselectbutton, then clicking on a row does nothing.
Here is my c# code, which takes the value from this cell and stores it into a string and then i call the setUser() function with the cell value as a parameter.
protected void gridView5_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = GridView5.SelectedRow;
string user = row.Cells[1].Text.ToString();
setSelectedUser(user);
}
I think the problem is in the aspx file, where I create my GridView, but don’t know how to make the row selectable without the autogenerateselectbutton set to true.
You want to use the RowCommand method you can create a asp:ButtonField column and set the command name