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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing gridview when using paging

Status
Not open for further replies.

fegdvbna22

Programmer
Aug 9, 2006
52
GB
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.
 
I get an error
You'll have to be a bit more descriptive than that...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
As you can see from the GridView above, I have a standard Edit column and also a Activate/Deactivate column. When I try to do the edit I get the error 'Input string was not in a correct format', and when I try to do the Activate/Deactivate I get the error 'Index was out of range. Must be non-negative and less than the size of the collection.'
 
I get the error 'Input string was not in a correct format'
And which line does this error occur on?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I get the error here :

<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>
 
That's not where the error occurs (that is simply the formatting for the GridView). Debug your code and step through it to see which line it actually occurs on.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I have stepped through the code in VS 2005 and it also steps through the .aspx page, and this is where it crashes. If I remove the breakpoint that leads to this error, then it doesn't step through the part of the code where the error occurs.
 
If it's crashing binding data to the GridView itself, then there must be an error raised in the RowDataBound or RowCommand event. Try checking the value of each database column (e.g. Region) and see what it equals.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Do you know why the second error I mentioned might be occuring?
 
Which line does that error occur on?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
This line :

int id = (int)GridView1.DataKeys[row.DataItemIndex].Value;
 
In that case there isn't an item in the DataKeys collection with the index of whatever row.DataItemIndex equals. Debug the project, have a look at the DataKeys collection (specifically the length) and see why there isn't anything in the collection at the index that you specified.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've solved the first error (it was a datatype problem). The second error is still a mystery.
There are 10 records in the DataKeys collection, DataItemIndex = 16 andRowIndex = 6.
 
There are 10 records in the DataKeys collection, DataItemIndex = 16
So, there are 10 items in the collection yet you are trying to get item 16. That obviously won't work hence the error you are getting.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
The page I am trying to do this on is page 2. I have 10 records per page, so the DataItemIndex represents the index of the 16th record in the complete recordset (all pages), but the DataKeys collection seems to only be holding the records for page 2. This would seem to be a major flaw in gridview paging rather than an error in my code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top