trying to delete selected gridview row. I read few sites and used code however I am getting error. I think because most of the codes I've seen they using DataGridView, This code is within delete button event
For i = 0 To myGridView.Rows.Count - 1 Step i + 1
Dim delrow As GridViewRow = myGridView.Rows(i)
If delrow.Selected = True Then
myGridView.Rows.RemoveAt(i)
End If
Next
the two errors I am getting are:
1) RemoveAt is not a member of GridViewRowCollection
2) Selected is not a member of GridViewRow
Your help will be highly appreciated!
I usually delete rows on GridViews putting an ImageButton (with a trash bin) on every row with a Template Field, then on RowDataBound event I associate "DeleteRow" event and rowIndex to the CommandName and CommandArgument
Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ib As ImageButton = e.Row.FindControl("ibDeleteRow")
ib.CommandName = "DeleteRow"
ib.CommandArgument = e.Row.RowIndex
End if
end sub
Private Sub myGridView_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "DeleteRow" Then
Dim rowToDeleteIndex as integer = e.CommandArgument
myGridView.DeleteRow(rowToDeleteIndex)
end if
end sub
Private Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
End Sub
Related
I have added Textbox like
Protected Sub gvReconciliation_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvReconciliation.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtRemarks As New TextBox()
txtRemarks.ID = "txtRemarks"
txtRemarks.Text = TryCast(e.Row.DataItem, DataRowView).Row("Remarks").ToString()
e.Row.Cells(10).Controls.Add(txtRemarks)
End If
End Sub
I have n number of dynamically generated GridViews in my asp.net page. In all the GridView all the cells are linkbuttons which also dynamically generated. And I want to get selected cells/ linkbuttons index.
How do i do it?
Note- I dont have GridView name as it is dynamically generated in loop.
code as below-
Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click
Call GetFinal()
End Sub
Public Sub GetFinal()
For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()
'DataTable filling Code goes here
GrdView1.DataSource = dt1
AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' All cell to LinkButton code goes here
AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub
You create linkButtons
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
LinkButton lnkShow= new LinkButton();
lnkShow.Text = "Show";
lnkShow.CommandArgument = "Show";
lnkShow.CommandArgument = e.Row.Cell(0); ' For instance
End IF
End Sub
And then you handle the GridView1.RowCommand event:
Public Sub GrdView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "show" Then
LinkButton lnkShow= (LinkButton)sender;
argument = e.CommandArgument;
(do something)
End IF
End Sub
I thoink you dont need Link1_Click
I currently have a Gridview in ASP.NET VB where you can select a row and the data will then be transported to the next page for entry purposes. My question is there an easy way where I can highlight the row after someone has entered information on this row and for the same day. So if someone enters data for a person today, then tomorrow the highlighting will go away the next calendar day. I appreciate any help.
This is what i currently have.
Public Sub Get_GV()
' Fills Gridview with data selected from dropdown-area
DSADT.SelectParameters("Area").DefaultValue = DDArea.SelectedValue
Dim dv As DataView = DSconnect.Select(DataSourceSelectArguments.Empty)
GridView1.DataSource = dv
GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
' Allows you to "select"/Highlight a row without a select button
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.backgroundColor = '#87CEFF';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';this.style.backgroundColor = '#FFFFFF';"
e.Row.Attributes("OnClick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim sb As New System.Text.StringBuilder()
'Submits data to next page
Response.Redirect("RoundingEntry.aspx?Room=" & GridView1.SelectedRow.Cells(1).Text & "&Name=" & GridView1.SelectedRow.Cells(2).Text & "&Rounder=" & DDRounder.SelectedValue & "&Area=" & DDArea.SelectedValue)
End Sub
You need to include the last modified date in the data returned from your query to bind the grid view and then you can handle the RowDataBound event to check the last modified date for each row against today's date, like this:
Code-behind:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' Only check the last modified date for data rows, ignore header and footer rows
If e.Row.RowType = DataControlRowType.DataRow Then
' Get the last modified date for each row, possibly in a hidden field control and compare it to today's date
End If
End Sub
Markup:
<asp:gridview id="GridView1"
onrowdatabound="GridView1_RowDataBound"
runat="server">
</asp:gridview>
onrowdatabound="CustomersGridView_RowDataBound"
You can use something like this:
Private Sub FormatMyGridView _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
Handles GridView1.RowDataBound
Dim drItems As DataRow
If e.Row.RowType = DataControlRowType.DataRow Then
drRequest =DirectCast(e.Row.DataItem, System.Data.DataRowView).Row
' get your field from DataItem
' format the row based on field value:
if drItems.YourFieldDateTime.Date = DateTime.Date
' hilight the row
e.Row.BackColor = System.Drawing.Color.Red;
End If
End Sub
I've got a grid view which uses a "If e.Row.RowType = DataControlRowType.DataRow" to calculate the total of a column and hold this in the footer.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Totalnumbers += Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "RequestTotalnumbers"))
ElseIf
e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(3).Text = String.Format("{0}", Totalnumbers)
End If
However i now wish to also add....
If e.Row.RowType = DataControlRowType.DataRow Then
Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
End If
so that it can transfer on click to another page....
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(e.CommandArgument).Value.ToString)
Response.Redirect(" ")
End If
End Sub
I've tried combining to two if's together but have had no success...how can i do this?
Not quite sure, this may be what you want. Make sure you also added "datakeynames" property in your GridView.
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(row.RowIndex).Value.ToString)
Response.Redirect(" ")
End If
End Sub
I'm dynamically adding a link button in the footer of a grid view. The grid view is wrapped in an update panel. I can get an async post back (I can tell by seeing an update progress flash), but I can't get the debug point in my click function to fire.
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
End If
End If
Private Sub clearButton_click(ByVal sender As Object, ByVal e As System.EventArgs)
ClearCart()
End Sub
try this
<dl>
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
Sorry,It's my mistake I have posted wrong code.Place the above code on OnRowCreated event of gridview
try this
Private Sub gvParts_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
The controls have to be added to your Controls collection prior to the page_load event. The default databinding (which fires the OnRowCreated, OnRowDataBound events) happens during the OnLoad event. Try moving your databinding code to the Page_Init function. Depending on what your databinding code looks like, this might mean you will have to implement the databinding "manually" (ie. Set the datasource and call .DataBind() in code)