I'm binding GridView like -
namespace grid_edit
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data source=HP_OWNER\\SQLEXPRESS;Database=demo;Integrated security=true";
con.Open();
SqlCommand cmd = new SqlCommand("select * from demo", con);
cmd.ExecuteNonQuery();
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
DataTable myDataTable = myDataSet.Tables[0];
GridView1.DataSource = myDataTable;
GridView1.DataBind();
Session["mytable"] = myDataTable;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String updatedItem = e.NewValues[0].ToString();
String anotherUpdatedItem = e.NewValues[1].ToString();
}
}
}
and having Mark up like following -
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
CellPadding="4" CellSpacing="2" ForeColor="Black"
style="top: 63px; left: 309px; position: absolute; height: 183px; width: 312px; margin-top: 25px;"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating" >
<RowStyle BackColor="White" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderSt开发者_JS百科yle BackColor="Black" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Now I would like to update the rows from the grid view but in the update event GridViewUpdateEventArgs
I'm getting NewValues and OldValues empty.
Try to change the code as follows:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data source=(local);Database=northwind;Integrated security=true";
con.Open();
SqlCommand cmd = new SqlCommand("select * from Categories", con);
cmd.ExecuteNonQuery();
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
DataTable myDataTable = myDataSet.Tables[0];
GridView1.DataSource = myDataTable;
if(!IsPostBack) /// <<<<<<<<<<<<<<<<<<<<<<
GridView1.DataBind();
Session["mytable"] = myDataTable;
}
It works fine here for .NET Framework 4.
Since you are using .NET Framework 3.0, this code does not work. I have looked at the GridView's HandleUpdate method and found out that these collections (e.NewValues and e.OldValues) are only populated when the Gridview is bound to a DataSourceControl through DataSourceID property... A possible solution is to use the ExtractValuesFromCell method:
cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
GridView1.Columns[1].ExtractValuesFromCell(
e.NewValues,
cell,
DataControlRowState.Edit,
true);
NOTE: the Page_Load method should have the code as I suggested, i.e. the DataBind should called only once.
精彩评论