Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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...
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...
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 8-Feb-13 7:22am
Comments
Minghang at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Set selected value of dropdown after your dropdownlist databind
 
drpDlistEditDept.DataBind();
drpDlistEditDept.SelectedValue=//The static string here.
 
-- UPDATED ANSWER HERE
 
Try these changes now,
 

Firstly, get your Gridview in RowCommand event like this
 
 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.
 
 Label lblValue1 = (Label)gvData.Rows[rowIndex].FindControl("lblValue1");
 Label lblValue2 = (Label)gvData.Rows[rowIndex].FindControl("lblValue2");
 strDeptSelectedValue = lblValue1.Text;
 strPositionSelectedValue = lblValue2.Text;
 
  Permalink  
v6
Comments
Minghang at 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
Sheikh Muhammad Haris at 9-Feb-13 20:28pm
   
I have updated the answer, check now
Sheikh Muhammad Haris at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 881
1 OriginalGriff 335
2 CPallini 225
3 Volynsky Alex 205
4 Richard Deeming 175
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100