By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,079 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,079 IT Pros & Developers. It's quick & easy.

editing gridview when paging

P: n/a
I am trying to edit a gridview while using paging, but whenever I try to
edit a row on a page other than page 1, I get an error. Here is my
gridview and my code :

<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1"
DataKeyNames="UserKey" AllowSorting="True"
HeaderStyle-Height="24px"
AutoGenerateColumns="false" SkinID="Grey3"
EditRowStyle-CssClass="dgedit"
AllowPaging="true"
PagerSettings-Mode="NumericFirstLast" PagerStyle-CssClass="pager"
PageSize="10" PagerStyle-ForeColor="Blue"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Key"
SortExpression="UserKey">
<ItemTemplate>
<asp:Label ID="lblUserKey" Text='<%#
Eval("UserKey") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Team
Type" SortExpression="TeamType">
<ItemTemplate>
<asp:Label ID="lblTeamType"
Text='<%# Eval("TeamType") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="120px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="Business
Unit" SortExpression="BusinessUnit">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit"
Text='<%# Eval("BusinessUnit") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id="ddlBusinessUnit" DataSourceID="SqlDataSource6" Runat="Server"
DataTextField="BusinessUnit"
DataValueField="BusinessUnit" SelectedValue='<%# Bind("BusinessUnit")
%>'/>
</EditItemTemplate>
<ItemStyle Height="24px" Width="170px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="Team
Manager" SortExpression="TeamManager">
<ItemTemplate>
<asp:Label ID="lblTeamManager"
Text='<%# Eval("TeamManager") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlTeamManager"
DataSourceID="SqlDataSource3" Runat="Server"
DataTextField="TeamManager"
DataValueField="TeamKey" SelectedValue='<%# Bind("TeamKey") %>'/>
</EditItemTemplate>
<ItemStyle Height="24px" Width="160px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="Territory
Code" SortExpression="TerritoryCode">
<ItemTemplate>
<asp:Label ID="lblTerritoryCode"
Text='<%# Eval("TerritoryCode") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id="ddlTerritoryCode" DataSourceID="SqlDataSource4" Runat="Server"
DataTextField="TerritoryCode"
DataValueField="TerritoryCode" SelectedValue='<%# Bind("TerritoryCode")
%>'/>
</EditItemTemplate>
<ItemStyle Height="24px" Width="150px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="User
Name" SortExpression="UserName">
<ItemTemplate>
<asp:Label ID="lblUserName"
Text='<%# Eval("UserName") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName"
Text='<%# Bind("UserName") %>' runat="server"
MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName"
Display="None" ErrorMessage="Please enter a User Name" />
</EditItemTemplate>
<ItemStyle Height="24px" Width="140px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="User
Login" SortExpression="UserLogin">
<ItemTemplate>
<asp:Label ID="lblUserLogin"
Text='<%# Eval("UserLogin") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserLogin"
Text='<%# Bind("UserLogin") %>' runat="server"
MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtUserLogin"
Display="None" ErrorMessage="Please enter a User Login" />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtUserLogin"
Display="None" ErrorMessage="Please enter a valid User Login"
ValidationExpression="[^ ]*" />
</EditItemTemplate>
<ItemStyle Height="24px" Width="140px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="Password"
SortExpression="Password">
<ItemTemplate>
<asp:Label ID="lblPassword"
Text='<%# Eval("Password") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPassword"
Text='<%# Bind("Password") %>' runat="server"
MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtPassword"
Display="None" ErrorMessage="Please enter a Password" />
<asp:CustomValidator
ID="CustomValidator2" runat="server" ControlToValidate="txtPassword"
Display="None"
ErrorMessage="Password must be 8-15 characters long, including at least
1 upper case character, 1 lower case character and 1 numeric digit"
OnServerValidate="ValidatePassword"
/>
</EditItemTemplate>
<ItemStyle Height="24px" Width="80px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Email"
SortExpression="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" Text='<%#
Eval("Email") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%#
Bind("Email") %>' runat="server"></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator1" runat="server" ControlToValidate="txtEmail"
Display="None" ErrorMessage="Please
enter a valid Email Address"
OnServerValidate="ValidateEmail" />
</EditItemTemplate>
<ItemStyle Height="24px" Width="70px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Region"
SortExpression="Region">
<ItemTemplate>
<asp:Label ID="lblRegion" Text='<%#
Eval("Region") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlRegion"
DataSourceID="SqlDataSource5" Runat="Server"
DataTextField="Region"
DataValueField="Region" SelectedValue='<%# Bind("Region") %>'/>
</EditItemTemplate>
<ItemStyle Height="24px" Width="70px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Last
Login" SortExpression="LastLogin">
<ItemTemplate>
<asp:Label ID="lblLastLogin"
Text='<%# Eval("LastLogin") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="140px"
/>
</asp:TemplateField>

<asp:TemplateField HeaderText="Active"
SortExpression="Active">
<ItemTemplate>
<asp:CheckBox ID="chkActive"
Checked='<%# Eval("Active") %>' runat="server" Enabled="false" />
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:CommandField ShowEditButton="True"
ButtonType="Link" ShowCancelButton="True"
UpdateText="Update" EditText="Edit"
CancelText="Cancel" />

<asp:ButtonField ButtonType="Link"
CommandName="ActivateDeactivate" Text="ActivateDeactivate" />

</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton EditButton =
(LinkButton)e.Row.Cells[12].Controls[0];
EditButton.ForeColor = System.Drawing.Color.Blue;

LinkButton ActivateDeactivateButton =
(LinkButton)e.Row.Cells[13].Controls[0];
ActivateDeactivateButton.ForeColor =
System.Drawing.Color.Blue;

string strActive =
Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Active"));

CheckBox chkActive =
(CheckBox)e.Row.FindControl("chkActive");

if (strActive == "True")
{
chkActive.Checked = true;
ActivateDeactivateButton.Text = "Deactivate";
}
else
{
chkActive.Checked = false;
ActivateDeactivateButton.Text = "Activate";
}
}
}

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "ActivateDeactivate")
{
// 1)Convert the row index stored in the CommandArgument
property to an Integer
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked by the
user from the Rows collection
GridViewRow row = GridView1.Rows[index];

//get datakeys
int id = (int)GridView1.DataKeys[row.DataItemIndex].Value;

//2)get current status of checkbox and switch
bool blnActivate = false;

CheckBox cb = new CheckBox();

cb =
(CheckBox)GridView1.Rows[index].FindControl("chkActive");

if (cb.Checked.ToString() == "True")
{
blnActivate = true;
}
else
{
blnActivate = false;
}

//3)update status
DataAccess da = new DataAccess();

//not doing anything with DBResult here
DBResult dbrUpdateStatus =
(Xerox.DBResult)da.ActivateDeactivateUser(id, blnActivate);

GridView1.EditIndex = -1;

//refresh gridview
GridView1.DataBind();
}
}

public void GridView1_RowUpdated(Object source,
SqlDataSourceStatusEventArgs e)
{
//refresh gridview
GridView1.DataBind();
}

Any help would be really appreciated.

*** Sent via Developersdex http://www.developersdex.com ***
Sep 18 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.