Click here to Skip to main content
12,898,811 members (54,727 online)
Rate this:
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.

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
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

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

Solution 1

Set selected value of dropdown after your dropdownlist databind

drpDlistEditDept.SelectedValue=//The static string 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;
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 i want to get the cell value as you've seen in the above code...grdRow.Cell[3].text
Sheikh Muhammad Haris 9-Feb-13 20:28pm
I have updated the answer, check now
Sheikh Muhammad Haris 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2017
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