Reputation: 467
I want to do an update operation through my gridview but I couldn't figure it out.
My gridview is this:
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" AutoGenerateColumns="False" DataKeyNames="ID"
OnDataBinding="GridView1_DataBinding"
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="UserID" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="UserPW" HeaderText="UserPW" SortExpression="UserPW" />
<asp:BoundField DataField="UserType" HeaderText="UserType" SortExpression="UserType" />
</Columns>
</asp:GridView>
My add/delete works fine but there is a problem with update such as getting null values from the grid. I tried 3 different methods to get the value. The commented lines gave me NullReferenceException while the uncommented lines did not,yet the values they held were empty strings.
This is my aspx.cs for update.(I'm using GridView's Edit/Update)
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userID = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex]["ID"]);
var UserOperations = new ModelOperations.UserList.UserListOperations();
//TextBox uname = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("UserName");
string uname = this.GridView1.Rows[e.RowIndex].Cells[2].Text;
string upw = this.GridView1.Rows[e.RowIndex].Cells[3].Text;
//string uname = this.GridView1.SelectedRow.Cells[2].Text;
//string upw = this.GridView1.SelectedRow.Cells[3].Text;
//TextBox upw = (TextBox)this.GridView1.Rows[e.RowIndex].FindControl("UserPW");
var result = UserOperations.updateUser(userID, uname, upw);
GridView1.EditIndex = -1;
GridView1.DataBind();
}
Upvotes: 2
Views: 118
Reputation: 12639
Try:
string uname = e.NewValues["UserName"];
string upw = e.NewValues["UserPW"];
If you want, you can even compare it with e.OldValues
.
Upvotes: 3