Page view counter

Changing a value in a GridView cell

Last post 11-26-2007 4:23 PM by BigBlueEye. 5 replies.

Sort Posts:

  • Changing a value in a GridView cell

    07-02-2007, 6:53 PM
    • Loading...
    • wsyeager
    • Joined on 06-26-2002, 7:42 PM
    • Weston, Florida, USA
    • Posts 456
    • Points 1,733

    I need to be able to change the value (set it to an empty string) of a Comment column on a GridView when a user clicks on an Edit button (which is a button column within the gridview). I usually perform these types of operations in the RowDatabound event (which was not dependent on another button click), but after pressing the Edit button, no instance of the column is available.

    For instance, when the RowDataBound event fires after getting rows from the database (not pressing Edit), I have the following code run depending upon a certain situation:

    <code>

    Dim txtComments As TextBox = DirectCast(e.Row.FindControl("txtComments"), TextBox)

    txtComments.Enabled = False

    </code>

    I am able to get an instance of the comments field. However, after pressing the Edit button and executing the same code above, (note that the RowEditing event fires first, then the RowDataBound event) I am not able to get an instance of the column.

     

    I have tried to set the column to an empty string in the RowEditing event, but it doesn't work. Note the followng code:

    <code>

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing

    Try

    Dim row As GridViewRow

    row = GridView1.Rows(e.NewEditIndex)

    Dim lblOrderID As Label = CType(row.FindControl("lblOrderID"), Label)

    Dim lblEvePhone As Label = CType(row.FindControl("lblEvePhone"), Label)

    Dim txtComments As TextBox = CType(row.FindControl("txtComments"), TextBox)

    GridView1.EditIndex = e.NewEditIndex

    txtPrevComments.Text = String.Empty

    txtPrevComments.Text = txtComments.Text

    txtComments.Text = String.Empty

    </code>

    So, my question is....

    How can I set the Comments field to an empty string after the user presses Edit (which is inside the GridView row for the record)?

    Thanks,

    Bill Yeager MCP.Net, BCIP
  • Re: Changing a value in a GridView cell

    07-02-2007, 7:08 PM
    • Loading...
    • rmaiya
    • Joined on 06-25-2007, 11:08 PM
    • Olympia, WA
    • Posts 1,559
    • Points 10,050

    you can use RowCommand event for this instead of RowEditing event. Look for e.CommandName in GridViewCommandEventArgs. This eliminates your worry abot which event fires first [like RowEditing or RowDataBound] , And also answer your another question you can

    txtComments.Text = String.Empty in RowCommand event

    Raghu
    (MCSD.NET, MCAD.NET, MCDBA)
    [Don't forget to click on Mark as answer on the post that helped you ]
  • Re: Changing a value in a GridView cell

    07-03-2007, 9:32 AM
    • Loading...
    • wsyeager
    • Joined on 06-26-2002, 7:42 PM
    • Weston, Florida, USA
    • Posts 456
    • Points 1,733

    You cannot use this event to do a "FindControl" method since it doesn't have any. There are no "Item" or "Row" properties of the "e" parameter in this event.

    There has to be a way to successfully get an instance of the Comment textbox after the user presses the Edit button within the row. I just don't know how to implement it.

    Can anybody help?

    Thanks,

    Bill Yeager MCP.Net, BCIP
  • Re: Changing a value in a GridView cell

    07-03-2007, 9:41 AM
    Answer
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 2,117
    • Points 12,418

    You can achieve your goal in RowDataBound like this (code is in C#, but you'll get the point):

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
         if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    TextBox comment = (TextBox)e.Row.FindControl("TextBox1");
                    comment.Text = string.Empty;
                }
        }

    Words are just like a crowd of people - it's not necessary to know them all... Choose only the right ones for yourself...
  • Re: Changing a value in a GridView cell

    07-03-2007, 1:05 PM
    Answer
    • Loading...
    • rmaiya
    • Joined on 06-25-2007, 11:08 PM
    • Olympia, WA
    • Posts 1,559
    • Points 10,050

    You dont have to look for e.Item in RowCommand , you already have the gridView

    grUsersList.Rows[grUsersList.EditIndex].FindControl("txtuserID")).Value;  

    Raghu
    (MCSD.NET, MCAD.NET, MCDBA)
    [Don't forget to click on Mark as answer on the post that helped you ]
  • Re: Changing a value in a GridView cell

    11-26-2007, 4:23 PM
    • Loading...
    • BigBlueEye
    • Joined on 05-17-2007, 10:14 AM
    • Posts 73
    • Points 15

    Raghu,

    I'm having similar issues as this thread, specifically with trying to place an image in a gridview column in the RowdatBound event. Here is my code and the error is in-line (at icon.ImageUrl):

    Your help is greatly appreciated! 

    // I setup the controls
    ImageField FileIcon = new ImageField();
    gvDocuments.Columns.Add(FileIcon);

    //then I set the datasource and register the event-handler
    gvDocuments.DataSource = response;
    gvDocuments.RowDataBound += new GridViewRowEventHandler(gvDocuments_RowDataBound);

            protected void gvDocuments_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if ((e.Row.RowType == DataControlRowType.DataRow))
                {
                    System.Web.UI.WebControls.Image icon = ((System.Web.UI.WebControls.Image) e.Row.FindControl("FileIcon"));
                    icon.ImageUrl = "_layout/images/filetype_txt.gif"; //ERROR - Object reference not set to an instance of an object.
                }
            }

Page 1 of 1 (6 items)