Solved

How to change deleteText of command field in Grid view?

Posted on 2008-10-21
4
966 Views
Last Modified: 2012-05-05
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
Comment
Question by:viola123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22765956
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
 
LVL 6

Author Comment

by:viola123
ID: 22766045
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
 
LVL 13

Accepted Solution

by:
crazyman earned 500 total points
ID: 22766513
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
 
LVL 6

Author Comment

by:viola123
ID: 22772024
hi,
i tried it again, it works. don't know what's going on last time.

thanks a lot
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
transition to visual .net from vb6 5 63
Code enhancement 4 33
C# Linq - Join two objects into one 3 37
VB.net/VSTO Excel Add-in 2 19
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question