从GridView控件asp.net C#中的单元格的值 [英] Select a cell value from GridView asp.net c#
问题描述
我有一个用户GridView和我希望能够选择一个行的第一列 - 这是与用户名。我知道如何使用 autogenerateselectbutton
属性在做,但它看起来并不好对我的设计。所以,相反,我想让它不选择按钮。以下是我迄今为止:
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>
如果我删除 autogenerateselectbutton
,然后点击一个排什么都不做。
If I remove the autogenerateselectbutton
, then clicking on a row does nothing.
下面是我的C#code,这需要从该小区并将其存储在值转换成字符串,然后我称之为 SETUSER()
功能与细胞值作为参数。</ p>
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);
}
我认为这个问题是在 ASPX
文件,其中创建我的 GridView控件
,但不知道如何让选择该行没有 autogenerateselectbutton
设置为true。
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.
推荐答案
您希望使用您可以创建一个ASP的RowCommand方法:ButtonField字段列,设置命令名
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();
}
}
这篇关于从GridView控件asp.net C#中的单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!