C#Webform,可以通过GridView Select commandField在每一行显示不同的文本

时间:2017-02-24 15:40:05

标签: c# asp.net gridview

所以我正在网站上工作并使用GridView。有一列称为“状态”(CommandField Select),是否可以针对每一行更改该列内的消息?因此,第一行显示“可用”,第二行显示“已售出”,两者都可以选择以触发selectedindexchange。

谢谢!

    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
    </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您可以在GridView的OnRowDataBound事件中执行此操作。

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

背后的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //set the column which contains the edit buttons
    int columnWithEditButton = 0;

    //check if the row is a normal datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //get the linkbutton in the cell with findcontrol
        LinkButton lb = e.Row.Cells[columnWithEditButton].Controls[0] as LinkButton;

        //modify the text
        lb.Text = "Select " + e.Row.RowIndex;
    }
}