从 GridView asp.net c# 中选择一个单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13802054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select a cell value from GridView asp.net c#
提问by Apostrofix
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 autogenerateselectbuttonproperty, 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:
我有一个带有用户的 GridView,我希望能够选择一行的第一列 - 这是用户名。我知道如何处理这个autogenerateselectbutton属性,但它在我的设计中看起来不太好。所以相反,我想在没有选择按钮的情况下制作它。这是我到目前为止所拥有的:
<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 aspxfile, where I create my GridView, but don't know how to make the row selectable without the autogenerateselectbuttonset to true.
我认为问题出在aspx文件中,我在其中创建了我的GridView,但不知道如何在没有autogenerateselectbutton设置为 true 的情况下使该行可选。
采纳答案by mosesfetters
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();
}
}
回答by Derman Deniz ?zkaya
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...
看起来不错,但是您的页面在选择后回发吗?您是否使用 if(!ispostback) 检查回发控制,因为当页面回发时 gridview 也会,这意味着 grv 将为空。你如何从你使用回发控制的空 grv 中获取数据,你的数据源和数据绑定行......
回答by Hiral
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 事件:- 从参数中获取用户名,无需从网格中获取。
回答by ??q?? zo???
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);

