How do you get access to the values you have entered under edit mode in a gridview? I only seem to be able to access the values prior to them being edited.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 1)Convert the row index stored in the CommandArgument property to an Integer
int index = e.RowIndex;
// Retrieve the row that contains the button clicked by the user from the Rows collection
GridViewRow row = GridView1.Rows[index];
//get datakeys (forecastkey)
int intID = (int)GridView1.DataKeys[row.DataItemIndex].Value;
//get all other fields to be updated
DropDownList ddl1 = new DropDownList();
ddl1 = (DropDownList)GridView1.Rows[index].FindControl("ddlCompanyName");
long lngCompanyKey = Convert.ToInt64(ddl1.SelectedValue);
DropDownList ddl2 = new DropDownList();
ddl2 = (DropDownList)GridView1.Rows[index].FindControl("ddlForecastType");
char chrForecastType = Convert.ToChar(ddl2.SelectedValue);
TextBox txt1 = new TextBox();
txt1 = (TextBox)GridView1.Rows[index].FindControl("txtMoneyValue");
double dblMoneyValue = Convert.ToDouble(txt1.Text);
DropDownList ddl3 = new DropDownList();
ddl3 = (DropDownList)GridView1.Rows[index].FindControl("ddlForecastPercentage");
double dblForecastPercentage = Convert.ToDouble(ddl3.SelectedValue);
TextBox txt2 = new TextBox();
txt2 = (TextBox)GridView1.Rows[index].FindControl("txtDueDate");
DateTime dtmDueDate = Convert.ToDateTime(txt2.Text);
//create update parameters
SqlDataSource1.UpdateParameters.Clear();
SqlDataSource1.UpdateParameters.Add("ForecastKey", TypeCode.Int32, intID.ToString());
SqlDataSource1.UpdateParameters.Add("UserKey", TypeCode.Int32, UserKey.ToString());
SqlDataSource1.UpdateParameters.Add("CompanyKey", TypeCode.Int64, lngCompanyKey.ToString());
SqlDataSource1.UpdateParameters.Add("ForecastType", TypeCode.Char, chrForecastType.ToString());
SqlDataSource1.UpdateParameters.Add("MoneyValue", TypeCode.Double, dblMoneyValue.ToString());
SqlDataSource1.UpdateParameters.Add("ForecastPercentage", TypeCode.Double, dblForecastPercentage.ToString());
SqlDataSource1.UpdateParameters.Add("DueDate", TypeCode.DateTime, dtmDueDate.ToString());
//update forecast table
DataAccess da = new DataAccess();
da.UpdateForecast(intID, UserKey,
lngCompanyKey, chrForecastType,
dblMoneyValue, dblForecastPercentage,
dtmDueDate);
GridView1.EditIndex = -1;
//refresh gridview
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 1)Convert the row index stored in the CommandArgument property to an Integer
int index = e.RowIndex;
// Retrieve the row that contains the button clicked by the user from the Rows collection
GridViewRow row = GridView1.Rows[index];
//get datakeys (forecastkey)
int intID = (int)GridView1.DataKeys[row.DataItemIndex].Value;
//get all other fields to be updated
DropDownList ddl1 = new DropDownList();
ddl1 = (DropDownList)GridView1.Rows[index].FindControl("ddlCompanyName");
long lngCompanyKey = Convert.ToInt64(ddl1.SelectedValue);
DropDownList ddl2 = new DropDownList();
ddl2 = (DropDownList)GridView1.Rows[index].FindControl("ddlForecastType");
char chrForecastType = Convert.ToChar(ddl2.SelectedValue);
TextBox txt1 = new TextBox();
txt1 = (TextBox)GridView1.Rows[index].FindControl("txtMoneyValue");
double dblMoneyValue = Convert.ToDouble(txt1.Text);
DropDownList ddl3 = new DropDownList();
ddl3 = (DropDownList)GridView1.Rows[index].FindControl("ddlForecastPercentage");
double dblForecastPercentage = Convert.ToDouble(ddl3.SelectedValue);
TextBox txt2 = new TextBox();
txt2 = (TextBox)GridView1.Rows[index].FindControl("txtDueDate");
DateTime dtmDueDate = Convert.ToDateTime(txt2.Text);
//create update parameters
SqlDataSource1.UpdateParameters.Clear();
SqlDataSource1.UpdateParameters.Add("ForecastKey", TypeCode.Int32, intID.ToString());
SqlDataSource1.UpdateParameters.Add("UserKey", TypeCode.Int32, UserKey.ToString());
SqlDataSource1.UpdateParameters.Add("CompanyKey", TypeCode.Int64, lngCompanyKey.ToString());
SqlDataSource1.UpdateParameters.Add("ForecastType", TypeCode.Char, chrForecastType.ToString());
SqlDataSource1.UpdateParameters.Add("MoneyValue", TypeCode.Double, dblMoneyValue.ToString());
SqlDataSource1.UpdateParameters.Add("ForecastPercentage", TypeCode.Double, dblForecastPercentage.ToString());
SqlDataSource1.UpdateParameters.Add("DueDate", TypeCode.DateTime, dtmDueDate.ToString());
//update forecast table
DataAccess da = new DataAccess();
da.UpdateForecast(intID, UserKey,
lngCompanyKey, chrForecastType,
dblMoneyValue, dblForecastPercentage,
dtmDueDate);
GridView1.EditIndex = -1;
//refresh gridview
GridView1.DataBind();
}