I have usedgridView for update and delete data. But I'm getting
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" error while updating.
There is no problem in your jquery datetimepicker
Problem seems to be in :
GridViewRow row = GridView1.Rows[e.RowIndex];
Simply take separate link buttons for edit and delete as:
<asp:TemplateField HeaderText="Edit" ItemStyle-Width="150">
<ItemTemplate>
<asp:linkbutton id="lnkEdit" runat="server" CommandName="Edit" Text="Edit"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-Width="150">
<ItemTemplate>
<asp:linkbutton id="lnkDelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
I can see you are using dropdownlist for copy_id so it is better to use the below code to find the dropdownlist and fetch value from it.
DropDownList lblCopyDdl= (DropDownList )GridView1.Rows[e.RowIndex].FindControl("lblCopyId");
int Copy_Id = Convert.ToInt32(lblCopyDdl.selectedvalue);
Recommendation::
Better if you use the same code as mentioned above for other fields for getting value..
for eg
int Borrower_Id = Convert.ToInt32((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBorrowerId").Text);
Update::
If it not works then do check your e.RowIndex as mentioned in Ángel Di María's answer.
Update 2
try to fetch the date field like this
DateTime Date_Borrowed = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateBorrowed").Text);
and
DateTime Date_Returned= Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateReturned").Text);
It seems that you are trying to read a cell value using index but there is no data element at that position. Suggest you to do debug and increment index value in your collection in watch window and observe data
Related
My GridView is as following has one column Training Details with HyperLink
<asp:GridView ID="gvTrainingItems" runat="server"....
......
......
<asp:TemplateField HeaderText="Training Details" HeaderStyle-Wrap="true">
<ItemTemplate>
<asp:HyperLink ID="lnkTrainingItemDetails" runat="server" NavigateUrl='<%#"~/TrainingItemDetails.aspx?ItemId=" + DataBinder.Eval(Container.DataItem,"itemId").ToString().Trim() + "&EmpId=" + Request.QueryString["EmpId"].ToString() %>'
Name='link' CssClass="btn btn-info" Target="_blank">View</asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False"></ItemStyle>
</asp:TemplateField>
....
Right now I am confused on how to get the NagivateUrl value from this GridViewRow?
I have tried something like
GridViewRow row = gvTrainingItemsPending.Rows[e.RowIndex];
.....
((HyperLink) row.Cells[10].Controls[0]).NavigateUrl
but it returns the following error. Can I get some help? Thanks!
Unable to cast object of type 'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.HyperLink'.
You may consider using the FindControl method to pull this out. This will pull out the control regardless of the cell in the row.
GridViewRow row = gvTrainingItems.Rows[e.RowIndex];
.....
string url = ((HyperLink) row.FindControl("lnkTrainingItemDetails")).NavigateUrl;
I was looking at the incorrect GridView thus the cell number is incorrect, also it is controls[1] in my case. The syntax is correct tho.
Use findcontrol method for getting control id and its value.
I am supporting a web application. In that, there are two tables - TaxCode and TaxRate. TaxCode has 1 to many relationship with TaxRate.
The UI has a ListView with LayoutTemplate, ItemTemplate and EditTemplate to show TaxCode. When the users selects a tax code in EditTemplate it shows a CutomGridView that allows the user to create or edit tax rates for that particular tax code. This CustomGridView has 3 rows each has 4 template fields as shown below.
<asp:TemplateField HeaderStyle-CssClass="highlightTitlebar" HeaderStyle-Width="5%" HeaderStyle-Height="30px">
<HeaderTemplate>
<custom:CustomImageButton ID="imgAdd" runat="server" ImageUrl="image/add_round.gif" OnClick="imgAddTaxRateDetail_Click" CausesValidation="False"/>
</HeaderTemplate>
<ItemTemplate>
<custom:CustomImageButton ID="imgEdit" runat="server" ImageUrl="image/edit.gif" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="image/update.gif" CommandName="Update" />
<asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgInsert" runat="server" ImageUrl="image/insert.gif" CommandName="Insert" OnClick="imgInsert_Click" />
</FooterTemplate>
Each row in the CustomGridView is a template field. In, below image EffectiveOn section is CustomGridView,
When I try to save TaxRate with proper EffectiveOn and Rate, it throws "Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert." error as the ListView doesn't have InsertTemplate. But the record gets inserted into the DB.
Please let me know if there is any way to resolve this issue?
The error is self explanatory.
Take a look at this: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx
So you can do either of these things.
Create an InsertItemplate and insert using the ItemInserted event of the listview
Change the CommandName to CommandName="InsertData" and catch that event on the ItemCommand
I have been trying to write this simple logic of deleting a row from the gridview and from the SQL database of the selected row, but keep getting a null reference to cell, which has the primary key [ID] in a field.
Here's my html:
<asp:GridView ID="grvInventoryEdit" runat="server" BackColor="White" onrowdeleting="grvInventoryEdit_RowDeleting"
onrowediting="grvInventoryEdit_RowEditing"
onrowupdating="grvInventoryEdit_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" /><asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%#Eval("No")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>........ </Columns> </asp:GridView>
And my back-end code for rowdeleting event is :
protected void grvInventoryEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
TextBox id = (TextBox)grvInventoryEdit.Rows[e.RowIndex].FindControl("txtEditNo");
Asset asset = db.Assets.Single(a => a.No == Convert.ToInt32(id));
db.Assets.DeleteOnSubmit(asset);
db.SubmitChanges();
binddata();
}
and when the event fires, this is what i am seeing while debugging:
I am not sure why i am getting a null value ,though, there is a value for that cell.
Could you tell me what i am doing wrong ?
Regards,
Might be it is due to the readonly property of textbox, not suer.
If you want to use the image button for edit and delete then use
protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
{
GridViewRow gvRow = (GridViewRow)((ImageButton)sender).NamingContainer;
Int32 UserId = Convert.ToInt32(gvUsers.DataKeys[gvRow.RowIndex].Value);
// delete and hide the row from grid view
if (DeleteUserByID(UserId))
gvRow.Visible = false;
}
For complete code see
You are passing TextBox object instead instead of Text property of TextBox
Asset asset = db.Assets.Single(x=>x.No == Convert.ToInt32(id.Text));
and your TextBox is also coming null means it's unable to find it in GridView, try like this:
TextBox id = e.Row.FindControl("txtEditNo");
Also see this CodeProject article to understand how to use ItemTemplate and EditItemTemplate
why are you adding a second commandfield instead of just enabling the delete button on the existing one.
if you are using a command field you should be supplying an compatible datasource that provides Delete functionality
if you're "rolling your own" delete functionality then just use a regular Button control and supply a CommandName and CommandArgument, such as CommandName="MyDelete" CommandArgument=<row number> where <row number> is supplied via GridView RowDataBound() event.
Regardless of how you choose to implement Delete you should be placing the key field in the GridView DataKeys Property and not as a field within each row. This will make obtaining the PK far easier than what you are trying to do
I guess, in my HTML, i have only the value that's bound to the item, is being displayed, and there are no textboxes in my <ItemTemplate>, hence, it pulls null value. Whereas, in my <EditItemTemplate> there is a textbox, which can pull the value from the cell.
So I changed my HTML to this:
<asp:TemplateField HeaderText="Id">
<ItemTemplate>`<asp:label runat="server" ID="lblEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
and no change in my codebehind, and this resolved the issue.
I am doing a project in .net with c#. in that there is a registration form of new users into the site. admin should approve the registration. for that I'm using a GridView. How to find out the selected row in the grid view?
Use the GridView.SelectedRow property.
Alternatively, you can get the index of the selected row with the GridView.SelectedIndex property.
are you perhaps referring to a DATAGRIDVIEW?
if so, to find the content of a certain column of the selected row
datagridviewname.item(0, datagridviewname.currentrow.index).value
where 0 = first column of your datagrid
Use following gridview property>
gvTradeFile.SelectedRows[0].Cells[10].Value.ToString();
Hope this will help you.
Normally I use RowCommand for this type of thing:
<asp:GridView runat="server" id="gvUsers" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="username" HeaderText="Username" />
<asp:BoundField DataField="dateregistered" HeaderText="Date" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" id="lnkApprove" CommandName="Approve" CommandArgument='<%# Eval("userid") %>' Text="Approve"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" id="lnkDeny" CommandName="Deny" CommandArgument='<%# Eval("userid") %>' Text="Deny"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now you need to pass instructions through on RowCommand...
private void gvUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "Approve":
// Executes code to update the users table,
// setting the approved flag to true
// Usage: ApproveUser(integer userid);
ApproveUser(e.CommandArgument);
break;
case else:
// do whatever if the user registration wasn't approved
break;
}
}
The switch is a good idea in case you need to do a few things with the data here. It evaluates the primary key value for the row (in this case, that's the value of the userid column in the database) for the selected row (automatically determined when you click the link) along with the CommandName to tell it what to do with that information.
Hope this helps.
I have a GridView which is editable and is bound to a database. So the datatype of each column in the GridView is Int in the database. Therefore my GridView accepts only integer.
But I want to give a notification to the user if the user enters any other character in the GridView cells other than numbers.
Basically I need to validate and give a error message.
Can someone help me how to go about it?
I have used Boundfields for my GridView.
Therefore suppose I enter any character other than a number it gives me an exception message saying : System.Data.SqlClient.SqlException: Conversion failed when converting the nvarchar value 'a' to data type int.
You best bet is to convert the BoundField into a TemplateField and add the validation control to the EditItemTemplate.
<asp:TemplateField HeaderText="Application" SortExpression="APPName">
<EditItemTemplate>
<asp:TextBox ID="txtApp" runat="server" Text='<%# Bind("APPName") %>'/>
<asp:RequiredFieldValidator runat='server' ID='requiredApp'
ErrorMessage='Application Name Cannot Be Empty' ControlToValidate='txtApp' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelApp" runat="server" Text='<%# Bind("APPName") %>'/>
</ItemTemplate>
</asp:TemplateField>
SOURCE