Steve
Steve

Reputation: 101

How can we put data on particular row index on GridView

I have a Gridview which binds some data from database except Quantity Textbox. There is also a button on Grid on each click of this button one number is added to the quantity box (This I want to show on grid). Please see my code below & suggest me.

Code :

if (e.CommandName == "Next")
   {
       int Index_Next = Convert.ToInt32(e.CommandArgument);
       TextBox Quantity = (TextBox)grdSale.Rows[Index_Next].FindControl("txtDisplay");
       int Val_Next = Convert.ToInt32(Quantity.Text);
       if (Val_Next != 0 && Val_Next > 1)
       {
           Val_Next = Val_Next + 1;
       }
       (TextBox)grdSale.Rows[Index_Next].FindControl("txtDisplay") = Val_Next.ToString();    // Here I'm getting error. I also convert this string to TextBox but still show error.
   }

Upvotes: 2

Views: 262

Answers (2)

Samiey Mehdi
Samiey Mehdi

Reputation: 9424

if (e.CommandName == "Next")
    {
        int Index_Next = Convert.ToInt32(e.CommandArgument);
        TextBox Quantity = (TextBox)grdSale.Rows[Index_Next].FindControl("txtDisplay");
        int Val_Next = Convert.ToInt32(Quantity.Text);
        if (Val_Next != 0 && Val_Next < 1)
        {
            Val_Next = Val_Next + 1;
        }
        ((TextBox)grdSale.Rows[Index_Next].FindControl("txtDisplay")).Text = Val_Next.ToString();
    }

Upvotes: 3

Manish Mishra
Manish Mishra

Reputation: 12375

but you are assigning to TextBox

(TextBox)grdSale.Rows[Index_Next]
                .FindControl("txtDisplay") = Val_Next.ToString();

which is wrong in first place, as Left side is System.Web.UI.WebContols.TextBox and right side is String . You should try and set the Text of Found TextBox. You should be doing

((TextBox)grdSale.Rows[Index_Next]
                 .FindControl("txtDisplay")).Text = Val_Next.ToString();

Upvotes: 4

Related Questions