Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Update Command will not fire.

Status
Not open for further replies.

asmith555

Programmer
Oct 7, 2002
97
US
I am really frustrated on this one.
I cannot get the update command of this datagrid to fire. All other commands work.

I have tried using both the ItemCOmmand and the Update Command. PLease Help


Datagrid:

<asp:DataGrid id="dgUser" runat="server" AutoGenerateColumns="False" CellPadding="2" OnItemCommand="dgUser_ItemCommand" OnUpdateCommand="dgUser_UpdateCommand" >
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="User ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.userid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="email" HeaderText="Email"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Is Admin">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IsAdmin") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:checkbox runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.IsAdmin") %>'></asp:checkbox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

---------------------------------------------------------

Here is the code behind:


public void dgUser_ItemCommand(object src, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch (e.CommandName.ToString())
{
case "Delete":
int rowToDelete = e.Item.ItemIndex;
this.dsUser.Tables[0].Rows[rowToDelete].Delete();
dgUser.DataBind();
break;

case "Edit":
int rowToEdit = e.Item.ItemIndex;
dgUser.EditItemIndex = rowToEdit;
dgUser.DataBind();
break;

case "Update":
// Get Cells.
TableCell EmailCell = e.Item.Cells[3];
TableCell IsAdminCell = e.Item.Cells[4];

// Get the controls.
TextBox EmailBox = (TextBox)EmailCell.Controls[0];
CheckBox IsAdminChk = (CheckBox)IsAdminCell.Controls[0];

// Extract the values.
string email = EmailBox.Text;
string isadmin = IsAdminChk.Checked.ToString();

// update the data source.
this.dsUser.Tables[0].Rows[e.Item.ItemIndex]["email"] = email;
this.dsUser.Tables[0].Rows[e.Item.ItemIndex]["IsAdmin"] = isadmin;

// Accept Changes and Switch out of edit mode.
dgUser.EditItemIndex = -1;
dgUser.DataBind();
break;

case "Cancel":
dgUser.EditItemIndex = -1;
dgUser.DataBind();
break;

default:
break;

}
}

public void dgUser_UpdateCommand(object src, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write("cmd is " + e.CommandName.ToString());
dgUser.EditItemIndex = -1;
dgUser.DataBind();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top