2

I have a boundfield in a gridview. How can I change the boundfield to a textbox only for a specific column?

I tried

BoundField colname= new BoundField();
grid.Columns.Add(colname as TextBox);

But it goves a cast expression

1 Answer 1

2

I'm not sure if this will work for your situation, but you could try using a template field, like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("SomeValue")%>' ... />
    </ItemTemplate>
</asp:TemplateField>

EDIT: Adding TextBox to item template from code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) 
    {
        TemplateField txtColumn = new TemplateField();
        txtColumn.ItemTemplate = new TextColumn();
        GridView1.Columns.Add(txtColumn);
    }
}

public class TextColumn : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        TextBox txt = new TextBox();
        txt.ID = "MyTextBox";
        container.Controls.Add(txt);
    }
}

EDIT: Setting text of dynamically added TextBox

//get the cell and clear any existing controls
TableCell cell = e.Row.Cells[0];
cell.Controls.Clear();

//create a textbox and add it to the cell
TextBox txt = new TextBox(); 
txt.Text = cell.Text;    
cell.Controls.Add(txt);
Sign up to request clarification or add additional context in comments.

12 Comments

nopes i dont do this in the mark up...I need the code behind way of doing it...I want to cast the BoundField to a text Box dynamically form the code behind
I don't know if you can do it with a bound field, but you might be able to add a textbox to a template field from the code behind. Let me check it out and I'll edit my answer.
I dont have template field...I have a bound field inside a grid view which is added to the grid view from code behind.Can you please modfiy my code snippet...im a bit confused...thanks
Can you not use a template field instead? I don't think you can add a TextBox to a bound field.
Hey thanks i found a way to do it The link is forums.asp.net/t/1660176.aspx/1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.