Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Control to Gridview Cell

I want to add a button on GridView cell on certain condition. I did the following in RowDatabound event

if( i==0)
{
   Button btn= new Button();
   btn.Text = "view more";        
   e.Row.Cells[7].Controls.Add(btn);
}

When this executes, the text in the cell which is bound is lost and only the button appears. I need to have the button along with the cell text present already.

Can anyone please help me doing this?

like image 369
Shanna Avatar asked Dec 19 '22 22:12

Shanna


2 Answers

When you add a control into the cell it trumps the text in the cell and only wants to display the control(s).

You can however have both the text and the button while keeping them separate. To do this you need to add another control in the form of a label:

Label myLabel = new Label();
myLabel.Text = e.Row.Cells[7].Text; //might want to add a space on the end of the Text
e.Row.Cells[7].Controls.Add(myLabel);

LinkButton myLinkButton = new LinkButton();
myLinkButton.Text = "view more";
e.Row.Cells[7].Controls.Add(myLinkButton);
like image 129
Hugo Yates Avatar answered Jan 05 '23 23:01

Hugo Yates


It's a workaround, check if it helps you:

You can convert your existing BoundColumn to Linkbuton if it is feasible with your requirement.

if( i==0)
{
    LinkButton lnkbtn = new LinkButton();
    lnkbtn.Text = e.Row.Cells[7].Text;
   // Create a command button and link it to your id    
   // lnkbtn.CommandArgument = e.Row.Cells[0].Text; --Your Id 
   // lnkbtn.CommandName = "NumClick";
   // btn.Text = "view more";        
    e.Row.Cells[7].Controls.Add(lnkbtn);
}
like image 31
Suraj Singh Avatar answered Jan 06 '23 00:01

Suraj Singh