[英]Select a cell value from GridView asp.net c#
I have a GridView with users and I want to be able to select the first column of a row - which is with usernames.我有一个带有用户的 GridView,我希望能够选择一行的第一列 - 这是用户名。 I know how to do it with the
autogenerateselectbutton
property, but it doesn't look well on my design.我知道如何使用
autogenerateselectbutton
属性来完成它,但它在我的设计中看起来不太好。 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.如果我删除
autogenerateselectbutton
,则单击一行什么也不做。
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.这是我的 c# 代码,它从该单元格中获取值并将其存储到一个字符串中,然后我以单元格值作为参数调用
setUser()
函数。
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.我认为问题出在
aspx
文件中,我在其中创建了GridView
,但不知道如何在没有将autogenerateselectbutton
设置为 true 的情况下使行可选。
You want to use the RowCommand method you can create a asp:ButtonField column and set the command name要使用 RowCommand 方法可以创建一个 asp:ButtonField 列并设置命令名称
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();
}
}
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"如果要获取用户在其中执行任何操作的特定行的选定索引值,则必须获取 rowindex 值如果要从标签控件获取值,则“
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如果你想从 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);
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.
您是否使用 if(!ispostback) 检查回发控制,因为当页面回发时 gridview 也会,这意味着 grv 将为空。 How can u get data from empty grv you have use postback control where your datasource and databind lines...
你如何从你使用回发控制的空 grv 中获取数据,你的数据源和数据绑定行......
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.现在触发 RowCommand 事件:- 从参数中获取用户名,无需从网格中获取。
<%@ 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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.