• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 970
  • Last Modified:

How to change deleteText of command field in Grid view?

hi all,
i have a delete command field in my grid view, when bind the data source, i try to change the delete text according to another bound field.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="personID" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting" OnRowDeleting="GridView1_RowDeleting">
        <Columns>
            <asp:BoundField DataField="personID" HeaderText="personID" ReadOnly="True" SortExpression="personID" />
<asp:BoundField DataField="flag" HeaderText="flag" SortExpression="flag" />
<asp:CommandField DeleteText="Exclude" ShowDeleteButton="True" ButtonType="button"></asp:CommandField>//when boundfield "flag" is true, the deleteText of commandfield should be 'Exclude', otherwise, it is 'Include'
 </Columns>
    </asp:GridView>
 
-------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 DataRowView drv = (DataRowView)e.Row.DataItem;
 
//how to get the command field in this event??? the following code won't work cos i cannot get the command field, pls help!!!!
 
Commandfield btn = (Commandfield)e.Row.cell[2].control(0);
 
if(drv["flag"].ToString() == "1")
{
    btn.DeleteText = "Exclude";
}
else
{
   btn.DeleteText = "Include";
 
}
                        
    }

Open in new window

0
viola123
Asked:
viola123
  • 2
  • 2
1 Solution
 
crazymanCommented:
Button btn = e.Row.Cells[2].Controls[0] as Button; //May be index 1,2,3,4,5,6,7 who knows...
if(drv["flag"].ToString() == "1")
{
    btn.Text = "Exclude";
}
else
{
   btn.Text = "Include";
 
}
0
 
viola123Author Commented:
hi,
i tried to cast the command field to button, but it gives me exception said the datacontrol field cannot be cast to button.
0
 
crazymanCommented:
This worked for me
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
 
                Button btn = (Button)e.Row.Cells[2].Controls[0];
 
                if (drv["flag"].ToString() == "1")
                {
                    btn.Text = "Exclude";
                }
                else
                {
                    btn.Text = "Include";
 
                }
            }
 
        }

Open in new window

0
 
viola123Author Commented:
hi,
i tried it again, it works. don't know what's going on last time.

thanks a lot
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now