Click here to Skip to main content
15,919,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,Is there any to get a boundField into a label...
I am using this code to get a boundFied row into a textbox and I want also to get the same into a Label but I am getting an error.
This is my code:

C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:BoundField DataField="CompanyName" HeaderText="Company Name" />
                    <asp:BoundField DataField="Address" HeaderText="Sddress" />
                    <asp:BoundField DataField="City" HeaderText="City" />
                    <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
                    <asp:BoundField DataField="Country" HeaderText="Country" />
                    <asp:CommandField ShowEditButton="true" />
                    <asp:CommandField ShowDeleteButton="true" />
                </Columns>
            </asp:GridView>


and this the c# code:

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Customerid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            TextBox textCompanyName = (TextBox)row.Cells[0].Controls[0];
            TextBox textAddress = (TextBox)row.Cells[1].Controls[0];
            TextBox textCity = (TextBox)row.Cells[2].Controls[0];
            TextBox textPostalCode = (TextBox)row.Cells[2].Controls[0];
            //string country = row.Cells[4].Text;
            Label lblCountry = (Label)row.Cells[4].Controls[0];
...
}

I am getting an error ont this line:
Label lblCountry = (Label)row.Cells[4].Controls[0];
Any help
Posted
Updated 5-Jun-18 2:37am
v2
Comments
Thanks7872 23-May-13 6:45am    
where is label in your gridview? and whers textbox?
El Dev 23-May-13 6:56am    
I dont need them in GridView I can create the in code behind Like this
TextBox textCompanyName = (TextBox)row.Cells[0].Controls[0];
and this work fine but when I am using this Label lblCountry = (Label)row.Cells[4].Controls[0]; it is not working...

The error is clearly telling you that you cannot convert an object of type A to an object of type B.
TextBox and Label are two different controls. You are type casting TextBox as a Label, in which case the error is obvious.
If you want to get the value of textbox and apply it to a label, then the following code would help.
var textBoxObj = (TextBox)row.Cells[4].Controls[0];
Label lblCountry = new Label(){ Text = textBoxObj.Text };
 
Share this answer
 
Because on updating boundfields are converted into textboxes. First you will have to cast it as a textbox after that you can assign its value to any label.

Try replacing
C#
Label lblCountry = (Label)row.Cells[4].Controls[0];

with
C#
TextBox lblCountry = (TextBox )row.Cells[4].Controls[0];

Then you can have something like:
C#
Label myLabel = new Label();
myLabel.Text = lblCountry.Text.Trim(); 
 
Share this answer
 
Comments
El Dev 23-May-13 9:01am    
Thanks!
Zafar Sultan 23-May-13 9:06am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900