Reputation: 2180
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.
Upvotes: 1
Views: 37343
Reputation: 739
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewDemo.aspx.cs" Inherits="DemoWebApp.GridviewDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmployee" runat="server" EmptyDataText="No Records Found" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField HeaderText="SNo" DataField="EmpID" />
<asp:BoundField HeaderText="Employee Name" DataField="EmpName" />
<asp:BoundField HeaderText="Age" DataField="EmpAge" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int age = Convert.ToInt32(e.Row.Cells[2].Text.ToString());
if (age >= 25 && age < 30)
{
e.Row.BackColor = Color.GreenYellow;
}
if (age == 30)
{
e.Row.BackColor = Color.LightBlue;
}
}
}
Upvotes: 0
Reputation: 4370
if you want to get the selected index value of a particular row in which the user do any operation.then you have to get the rowindex value If you want to Get a Value From a Label Control then"
Label l=(Label)Gridviewname.rows[e.rowindex].Findcontrol("label name of which you want to get the data")
String s=l.Text;
setselecteduser(l);
if you want to get the value from TextFiled
TextField t=(TextField)GridviewName.rows[e.rowindex].FindControl("Textbox Id of which you want to get the data")
String s=t.Text;
SetSelecteduser(t);
Upvotes: 1
Reputation: 475
Put a linkbutton with Select text, command name as "Select" and pass "User name" as command argument.
<asp:LinkButton runat="server" ID="lnkStatus" CommandName="Select"
CommandArgument='<%# Eval("username") %>' Text='Select' />
Now fire RowCommand event:- Get username from argument, no need to get it from grid.
Upvotes: 0
Reputation: 11
Seems ok but does your page postbacking after select ? do you check postback control with if(!ispostback) because when the page is postbacking the gridview will also, it means the grv will be empty. How can u get data from empty grv you have use postback control where your datasource and databind lines...
Upvotes: 0
Reputation: 1698
You want to use the RowCommand method you can create a asp:ButtonField column and set the command name
protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName=="MyCommand")
{
int row = int.Parse(e.CommandArgument.ToString());
var cellText= gvOwner.Rows[row].Cells[1].Text.Trim();
}
}
Upvotes: 2