2

net 4 and c#.

I have a GridView, I would like take a Row when in Edit Mode in my code and find a control.

Here my code, but does not work, it takes only the first row for the GridView.

Any ideas?

protected void uxManageSlotsDisplayer_RowDataBound(object sender, GridViewRowEventArgs e)
{

    switch (e.Row.RowType)
    {
        case DataControlRowType.DataRow:

        // Take Row in Edit Mode DOES NOT WORK PROEPRLY
        if (e.RowState == DataControlRowState.Edit)
        {
            Label myTest = (Label)e.Row.FindControl("uxTest");
        }
        break;

    }

MY CODE EXAMPLES: GridView row in edit mode

SOLUTIONS: After reading this: Gridview row editing - dynamic binding to a DropDownList

        protected void uxList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
                (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            // Here you will get the Control you need like:
            Label dl = (Label)e.Row.FindControl("uxLblTest");
            dl.Text = "xxxxxxxxxxxxx";
        }
    }
Community
  • 1
  • 1
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • We need more information: what row do you need, the one that's the current selected row, or the row that's being edited, or multiple rows in edit mode ? – Steven Ryssaert Mar 02 '11 at 15:41
  • Hi i need the Row being edited (the row is in edit mode), so should be a unique ROW in edit mode (after the user click edit). any ideas? – GibboK Mar 02 '11 at 15:50

2 Answers2

1

you shoud set EditItemIndex in the grid before datatabind. you can do it in RowEditing event, as in this example:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx

Regards, Stefano

stefano m
  • 4,094
  • 5
  • 28
  • 27
  • Hi Stefano, I try your suggestion, but I am really a beginner and I am not able to work it out. I posted my code here: http://stackoverflow.com/questions/5170261/gridview-row-in-edit-mode could you kind me show an implementation? thanks once again – GibboK Mar 02 '11 at 16:16
0

Edit: Added check for DataRow

 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)

instead of

 if (e.RowState == DataControlRowState.Edit)
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • thanks but did not solve the problem... any other ideas? no one in SO seems know the answer :-( – GibboK Mar 02 '11 at 15:30
  • Edited! I have added the check for RowType. I have similar code in a few for my apps and they work. – Bala R Mar 02 '11 at 15:40
  • Hi, now is working but ONLY for the FIRST Row in the GridView... very strange? What do you think? I need the same for every row in the GridView. Please let me now many thanks – GibboK Mar 02 '11 at 15:49
  • Yes in fact, if i put in edit mode the first role your code is working. If i select the second or others rows does not work. quite strange – GibboK Mar 02 '11 at 16:07
  • Have you tried the RowEditing event? you'll have to have `var currentRow = GridView1.Rows[e.NewEditIndex];` to get the current row. – Bala R Mar 02 '11 at 16:10
  • im trying but im now able to make it. – GibboK Mar 02 '11 at 16:11
  • Here the code im using, i need to get the label in edit mode and change the text. May I ask you to write a simple line of code for my understanding??? I really appreciate it im a really beginner http://stackoverflow.com/questions/5170261/gridview-row-in-edit-mode – GibboK Mar 02 '11 at 16:16
  • hi here the solution if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) hope can help! – GibboK Mar 02 '11 at 17:33