• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Making items in gridview selectable

I have a gridview that I am pulling information with in ASP.net.  It is pulling a username.  The problem that I have is, I want to have the row selectable without having the "Select" word right next to the item I want to select.  So, how do I do that without having "Select" right there and make the Username a link instead?
0
VBBRett
Asked:
VBBRett
  • 2
1 Solution
 
Bane83Commented:
See below
Page:
 
<asp:GridView ID="userGrid" runat="server" AutoGenerateColumns="false" 
			RowStyle-BackColor="LightGray" AlternatingRowStyle-BackColor="White"
			SelectedRowStyle-BackColor="Yellow">
			<Columns>
				<asp:BoundField DataField="ID" HeaderText="ID" />
				<asp:TemplateField HeaderText="Username">
					<ItemTemplate>
						<asp:LinkButton ID="lbUsername" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Username") %>' 
							CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>' OnClick="Username_Click"></asp:LinkButton>
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>	
		</asp:GridView>
 
codebehind:
protected void Page_Load(object sender, EventArgs e)
    {
		DataTable users = new DataTable();
		//DataColumn rowID = new DataColumn("rowID", typeof(uint));
		DataColumn[] columns = { new DataColumn("ID", typeof(uint)), new DataColumn("Username", typeof(string)) };
 
		users.Columns.AddRange(columns);
		users.PrimaryKey = new DataColumn[] { users.Columns[0] };
 
		DataRow dr = users.NewRow();
		dr["ID"] = 1;
		dr["Username"] = "Jimmy";
 
		users.Rows.Add(dr);
 
		dr = users.NewRow();
		dr["ID"] = 2;
		dr["Username"] = "Kate";
 
		users.Rows.Add(dr);
 
		dr = users.NewRow();
		dr["ID"] = 3;
		dr["Username"] = "Bill";
 
		users.Rows.Add(dr);
 
		dr = users.NewRow();
		dr["ID"] = 4;
		dr["Username"] = "Jane";
 
		users.Rows.Add(dr);
	
		userGrid.DataSource = users;
		userGrid.DataBind();
    }
 
	protected void Username_Click(object sender, EventArgs e)
	{
		int ID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
 
		int selectedIndex = ((DataTable)userGrid.DataSource).Rows.IndexOf(((DataTable)userGrid.DataSource).Rows.Find(ID));
 
		userGrid.SelectedIndex = selectedIndex;
	}

Open in new window

0
 
VBBRettAuthor Commented:
Thanks!  It works!
0
 
VBBRettAuthor Commented:
Making items in gridview selectable.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now