Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello experts, here is what i want to accomplish. I have a lable displaying Department name in the itemtemplate of gridview. When i click Edit linkbutton(inside the Itemtemplate) the edittemplate for the department which is a dropdownlist will be shown. But after i populate this dropdownlist i can't get it to select the value that was initially in that field. I have written the following code in the rowcommand event handler...
C#
else if (e.CommandName.ToLower().Equals("edit"))
            {
                GridViewRow grdRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                //int nIndex = grdRow.RowIndex;
                strDeptSelectedValue = grdRow.Cells[3].Text;
                strPositionSelectedValue = grdRow.Cells[4].Text;
            }


i want to get the Cell content of the row and store it in a static string...and i want to be able to utilize this string in the RowEditing Eventhandler as follows...
C#
DropDownList drpDlistEditDept = (DropDownList)grdQuestionGroup.Rows[e.NewEditIndex].FindControl("drpdlistEditDept");
            DropDownList drpDlistEditPosition = (DropDownList)grdQuestionGroup.Rows[e.NewEditIndex].FindControl("drpDlistEditPosition");

            ds = DepartmentDA.SelectAll();
            drpDlistEditDept.DataSource = ds.Tables[0];
            drpDlistEditDept.DataTextField = "Name";
            drpDlistEditDept.DataValueField = "Code";
            drpDlistEditDept.SelectedValue=//The static string here.
            drpDlistEditDept.DataBind();


however the static string always comes out to be a "" string. Why is the Cell[3].Text and Cell[4].Text not giving any value?? I have debugged and checked...the grdRow is getting the value as it should...
Posted
Comments
Minghang 9-Feb-13 23:37pm    
Sheikh Muhammad haris...i Cannot see your comment but my notification says that you have posted about 208 comments in this question...before this i received 305 comments from you but...those posts are not visible here....maybe some bug in the site??...i prbly should inform the site admin?

1 solution

Set selected value of dropdown after your dropdownlist databind

C#
drpDlistEditDept.DataBind();
drpDlistEditDept.SelectedValue=//The static string here.


-- UPDATED ANSWER HERE

Try these changes now,


Firstly, get your Gridview in RowCommand event like this

C#
protected void gvProduct_RowCommand(Object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "AddNewRow")
       {
           GridView gvProduct = (GridView)sender;

       }
   }


If you still don't get cell values, then try following changes too. You will definitely get your required values.

Use Labels by find Control for getting values in GridView RowCommand Event.

C#
Label lblValue1 = (Label)gvData.Rows[rowIndex].FindControl("lblValue1");
Label lblValue2 = (Label)gvData.Rows[rowIndex].FindControl("lblValue2");
strDeptSelectedValue = lblValue1.Text;
strPositionSelectedValue = lblValue2.Text;
 
Share this answer
 
v6
Comments
Minghang 8-Feb-13 23:16pm    
the static string turns out to be an empty string...binding it above the databind() works as well...i tried this by hardcoding the value...now i want to get the cell value as you've seen in the above code...grdRow.Cell[3].text
[no name] 9-Feb-13 20:28pm    
I have updated the answer, check now
[no name] 9-Feb-13 22:33pm    
+5
[no name] 9-Feb-13 22:54pm    
thanks man :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900