Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi - I'm populating a gridview on the page load, in this grid there is an Edit button on each row which copies the data contents from the gridview into various controls (DropdownLists, textboxes) below the GV once clicked. Now any changes that are made to the data contents I want to save back to the DB and refresh the GV. The DataTextField value on the DDL is displaying the value that was in the GV but the DataValueField doesn't not seem to be updated because when I got to save any changes I've made to the textboxes my code is crashing because Convert.ToInt32(ddlHPType.SelectedValue) = "".

What do i need to do to set the DataValueField for the DDL when the GV Edit button is clicked?

my DDL is populated here:
C#
protected void gvHeatPumpData_SelectedIndexChanged(object sender, EventArgs e)
        {
            //populate textboxes, ddl with values from select row in gvHeatPumpData
            ddlHPRange.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[2].Text;
            txtHPModelNumber.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[3].Text;
            ddlHPType.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[4].Text;
            txtHPDimensions.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[5].Text;
            ddlElectricalSupply.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[6].Text;
            txtHPVoltage.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[7].Text;
            txtHPStartingCurrent.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[8].Text;
            txtHPWeight.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[9].Text;           
        }
Posted
Updated 22-Aug-12 3:50am
v2

Are you populating the control in your postback ? if you populate it in your page load, that will reset the selected index. Don't use Convert.ToXXX. Using int.TryParse so you can deal with invalid values.
 
Share this answer
 
Comments
pmcm 22-Aug-12 10:57am    
No I am initially populating my DDL control on the page load, should I be repopulating it upon postback?
Christian Graus 22-Aug-12 10:58am    
You should popuate it in the prerender event, and not do it on postback. The page load occurs before events, and the prerender, afterwards. Are you sure it's not happening on postback, b/c if it is, that would explain it. Move it to the prerender event, that's where it belongs
whenever I was populating the DDL with the GV row data I was doing:
C#
ddlHPRange.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[2].Text;


I've amended this to be now
C#
ddlHPRange.SelectedValue = gvHeatPumpData.DataKeys[gvHeatPumpData.SelectedIndex].Values["HeatPumpRangeID"].ToString();

and now the ID is not blank when I'm saving any updates
 
Share this answer
 

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