Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
During editing in a grid view I want the old values of the particular field in the drop down.

Currently when I enter the edit mode in the gridview i am getting the dropdown as i specified in the list item.

I tried the Selected Value attribute directly but its not working in the grid view. Actually i didn't find it in the Intellisense also.

I found a solution but its working for only odd rows, even rows are not getting showing the same thing.

e.g If i set someones designation as Web Designer when entering the edit mode the dropdown shows Sales which is first in the Data Members.

After adding some code I thought i solved the problem but on closer testing i found the even rows are showing the first value of data member i.e Sales. But for Odd rows its working properly.

The Code:

C#
protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkgrdDelete");
            l.Attributes.Add("onclick", "javascript:return confirm('Are You Sure');");

        }
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddlgrdDesg");
            DropDownList ddl2 = (DropDownList)e.Row.FindControl("ddlgrdUType");

            UserBAL user = new UserBAL();

            int key = Convert.ToInt32(grdUsers.DataKeys[e.Row.RowIndex].Value.ToString());
            string[] data = user.GetUserByID(key);

            ddl1.SelectedItem.Text = data[4];
            ddl2.SelectedItem.Text = data[5];
        }
    }


This code works for odd rows but not for even rows. I dont understand something very funny. Its right from my point of view. Also during debugging the even rows never enter the if condition as e.Row.RowState is normal then.
Posted
Comments
__TR__ 13-Dec-12 8:39am    
You can store the selected value in a hidden field and then use the value in the hidden field to select the dropdown list item.
Siddhesh Patankar 14-Dec-12 1:31am    
No not happening.... the problem is when we enter the edit mode in even rows its rowstate is normal so it doesnot enter the condition block so the values remain as it is.

1 solution

C#
public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {
string TempValue="";
TempValue =dataGridView1.Rows[e.RowIndex].Cells["ColumnName"].Value.ToString();
UrDrowDownThing.datasource=TempValue;
}
 
Share this answer
 
Comments
StackQ 13-Dec-12 6:48am    
Here each time when u will click on cell,each time new value will be store into ur dropdown.
Siddhesh Patankar 13-Dec-12 6:52am    
Sorry... but theres no cellclick event in the grid view event. I am using VS2010 and asp.net 4.0 framework
Siddhesh Patankar 13-Dec-12 6:55am    
I dont want to add a new value... I want to set a selected value from the original list of list items present in the dropdown. Means the value which is stored in the DB for that user must be shown as default selected value in the dropdown. Means if a person is a programmer it must show programmer not sales.
StackQ 13-Dec-12 6:55am    
ok,so in case of vs2010 i don't know,bcoz i m not using vs2010.Well see other answer.Or search another event which is used like cellClick in 2010version.
StackQ 13-Dec-12 7:03am    
ok,If VS2008 Then On dataGridCellClick event make a query like-

string Query,PersonName,JobType;
PersonName=dataGridView1.Rows[e.RowIndex].Cells["PersonName"].Value.ToString();//here u will get person name to query for this person
Query=" Select Job from UserList where PersonName='" & PersonName & "'";
SqlDataAdapter DA=new SqlDataAdapter(Query,UrConnstringName);
DataSet ds=new dataset();
DA.fill(ds);
JobType=ds; //here u will get Programmer for the person (or for which row u clicked)
now
set
ur dropdownThing.defaultVALUE=JobType

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