如何改变在GridView控件一定条件的文本? [英] How to change the text on certain condition in GridView?

查看:127
本文介绍了如何改变在GridView控件一定条件的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变在GridView的某些条件的单元格的文本/值。我有一个返回'P'当我从数据库绑定GridView的细胞,'A','R'。我想在'P'情况'A',并在更新按钮的下拉'R'的情况下,拒绝的情况下,以显示待定,批准。我想换一个记录的状态。

我怎样才能更改文本和在数据绑定的时间做我所需要的任务。

 < ASP:TemplateColumn中>
  < HeaderStyle的CssClass =BGB白色P5 b TREB TTU W10/>
  <&HeaderTemplate中GT;
    < ASP:标签ID =lblstatus=服务器文本=状态>< / ASP:标签>
  < / HeaderTemplate中>
  <&ItemTemplate中GT;
    < ASP:标签ID =lblrmastatus=服务器文本='<%#绑定(RMA_STATUS)%>'>
    < / ASP:标签>
  < / ItemTemplate中>
< / ASP:TemplateColumn中>


解决方案

要解决这个问题的方法是处理<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx\"相对=nofollow>的RowDataBound 事件。这是一次提高每行(也页眉和页脚行,所以你需要筛选类型的行的DataRow )。可以用它来调整所显示的数据或它们的显示方式。您还可以设置在事件下拉的值。看到该链接的样本。

在为下拉列添加到GridView,使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatecolumn%28v=vs.110%29.aspx\"相对=nofollow> TemplateColumn中并把下拉到的ItemTemplate。在RowDataBound事件处理程序访问相应的小区。使用FindControl方法来检索下拉控件,并设置其值。


下面的示例显示了如何设置标签和DropDownList的编程方式:

GridView的

GridView控件显示原状态,并与长文本,一个DropDownList和命令列来更新数据的新状态。

 &LT; ASP:GridView控件ID =GDV=服务器
              OnRowDataBound =gdv_RowDataBound的AutoGenerateColumns =false的&GT;
    &LT;柱体和GT;
        &LT; ASP:BoundField的数据字段=RMAStatus的HeaderText =原始RMA状态/&GT;
        &LT; ASP:的TemplateField的HeaderText =调整RMA状态&GT;
            &LT;&ItemTemplate中GT;
                &LT; ASP:标签ID =lblStatus=服务器/&GT;
            &LT; / ItemTemplate中&GT;
        &LT; / ASP:的TemplateField&GT;
        &LT; ASP:的TemplateField的HeaderText =新状态&GT;
            &LT;&ItemTemplate中GT;
                &LT; ASP:DropDownList的ID =ddlStatus=服务器/&GT;
            &LT; / ItemTemplate中&GT;
        &LT; / ASP:的TemplateField&GT;
        &LT; ASP:CommandField中ShowEditButton =真/&GT;
    &LT; /专栏&GT;
&LT; / ASP:GridView的&GT;

code-背后

在code-后面,我已经定义了一个名为类 DATACLASS 包含数据(你也可以使用一个DataTable,但样品是更容易使用的一类,以产生一些数据)。此外,字典包含从短状态,长文本的映射。

的Page_Load ,词典设置,如果它是没有回传,数据绑定。

最后,在 gdv_RowDataBound ,标签设置为每一行和每一DropDownList的初始化。请注意,该行的标签和DropDownList中通过使用检索的FindControl

 公共类数据类
{
    公共字符串RMAStatus {搞定;组; }
}私人只读字典&LT;字符串,字符串&GT; rmaStatusMappings =新词典&LT;字符串,字符串&GT;();保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    rmaStatusMappings.Add(R,拒绝);
    rmaStatusMappings.Add(A,批准);
    rmaStatusMappings.Add(P,待定);
    如果(!的IsPostBack)
    {
        VAR数据=新的数据类[] {
            新数据类(){RMAStatus =P},
            新数据类(){RMAStatus =A},
            新数据类(){RMAStatus =R},
            新数据类(){RMAStatus =P},
        };
        gdv.DataSource =数据;
        gdv.DataBind();
    }
}保护无效gdv_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        VAR rowStatus =((数据类)e.Row.DataItem).RMAStatus;
        //设置标签长状态
        VAR lblStatus = e.Row.Cells [1] .FindControl(lblStatus)作为标签;
        如果(lblStatus!= NULL)
            lblStatus.Text = rmaStatusMappings [rowStatus]
        //设置下拉项
        变种ddlStatus = e.Row.Cells [2] .FindControl(ddlStatus)作为DropDownList的;
        如果(ddlStatus!= NULL)
        {
            ddlStatus.DataTextField =值;
            ddlStatus.DataValueField =键;
            ddlStatus.DataSource = rmaStatusMappings;
            ddlStatus.DataBind();
            ddlStatus.SelectedValue = rowStatus;
        }
    }
}

让我知道在评论,如果您有任何问题。

I want to change the text/value of a cell in certain condition in GridView. I have a cell that return 'P', 'A', 'R' when I bind the Gridview from database. I want to show 'Pending' in case of 'P', 'Approved' in case of 'A' and 'Rejected' in case of 'R' in the dropdown with the update button. I want to change the status of a record.

How can I change the text AND do my required task at the time of Binding of data.

<asp:TemplateColumn> 
  <HeaderStyle CssClass="bgB white p5 b treb ttu w10" />
  <HeaderTemplate> 
    <asp:Label ID="lblstatus" runat="server" Text="Status"></asp:Label>       
  </HeaderTemplate> 
  <ItemTemplate> 
    <asp:Label ID="lblrmastatus" runat="server" Text='<%# Bind("RMA_STATUS") %>'>
    </asp:Label> 
  </ItemTemplate> 
</asp:TemplateColumn>

解决方案

A way to solve this is to handle the RowDataBound event. It is raised once per row (also header and footer rows, so you need to filter for rows of type DataRow). You can use it to adjust the data that are displayed or the way they are displayed. Also you can set the values of a DropDown in the event. See the link for a sample.

In order to add the DropDown-column to the GridView, use a TemplateColumn and put the DropDown into the ItemTemplate. Access the corresponding cell in the RowDataBound event handler. Use the FindControl method to retrieve the DropDown control and set its values.


The following sample shows how to set the Label and the DropDownList programmatically:

GridView

The GridView shows both the original status and a new status with the long text, a DropDownList and a command column to update the data.

<asp:GridView ID="gdv" runat="server" 
              OnRowDataBound="gdv_RowDataBound" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="RMAStatus" HeaderText="Original RMA Status" />
        <asp:TemplateField HeaderText="Adjusted RMA Status">
            <ItemTemplate>
                <asp:Label ID="lblStatus" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="New Status">
            <ItemTemplate>
                <asp:DropDownList ID="ddlStatus" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="true" />
    </Columns>
</asp:GridView>

Code-behind

In Code-behind, I've defined a class called DataClass that contains the data (you can also use a DataTable, but for the sample it was easier to use a class to generate some data). Also, a dictionary contains the mappings from the short status to the long text.

In Page_Load, the dictionary is set up and if it is no PostBack, the data are bound.

Finally, in gdv_RowDataBound, the label is set for each row and the DropDownList is initialized. Please note that the Label and the DropDownList for the row are retrieved by using FindControl.

public class DataClass
{
    public string RMAStatus { get; set; }
}

private readonly Dictionary<string, string> rmaStatusMappings = new Dictionary<string, string>();

protected void Page_Load(object sender, EventArgs e)
{
    rmaStatusMappings.Add("R", "Rejected");
    rmaStatusMappings.Add("A", "Approved");
    rmaStatusMappings.Add("P", "Pending");
    if (!IsPostBack)
    {
        var data = new DataClass[] {
            new DataClass() { RMAStatus = "P" }, 
            new DataClass() { RMAStatus = "A" }, 
            new DataClass() { RMAStatus = "R" }, 
            new DataClass() { RMAStatus = "P" }, 
        };
        gdv.DataSource = data;
        gdv.DataBind();
    }
}

protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var rowStatus = ((DataClass)e.Row.DataItem).RMAStatus;
        // Set label to long status
        var lblStatus = e.Row.Cells[1].FindControl("lblStatus") as Label;
        if (lblStatus != null)
            lblStatus.Text = rmaStatusMappings[rowStatus];
        // Set drop down items
        var ddlStatus = e.Row.Cells[2].FindControl("ddlStatus") as DropDownList;
        if (ddlStatus != null)
        {
            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Key";
            ddlStatus.DataSource = rmaStatusMappings;
            ddlStatus.DataBind();
            ddlStatus.SelectedValue = rowStatus;
        }
    }
}

Let me know in the comments if you have any questions.

这篇关于如何改变在GridView控件一定条件的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆