I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub
Related
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
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 want to get the ID of a textbox like below to add validator, the Client ID contains generated string, UniqueID too, but only the ID contains nothing, why?
Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'Manipulate only editing row.
If e.Row.RowType = DataControlRowType.DataRow Then
If sender.EditIndex = e.Row.RowIndex Then
'Search textbox and add validators.
For Each cell As TableCell In e.Row.Cells
If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'txt.ID is nothing...why?
SetValidators(cell.Controls, txt.ID)
End If
Next
End If
End If
End Sub
Well here is a workaround if applies to your application ,
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
//'txt.ID is nothing...why?
// Here you can assign new ID to your control as per your logic
txt.ID = "newID";
SetValidators(cell.Controls, txt.ID)
You can try following code to assign the validator control to the gridview control.
I am not sure what your doing with SetValidators()function.
Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'Manipulate only editing row.
If e.Row.RowType = DataControlRowType.DataRow Then
If sender.EditIndex = e.Row.RowIndex Then
'Search textbox and add validators.
For Each cell As TableCell In e.Row.Cells
If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'txt.ID is nothing...why?
SetValidators(cell.Controls, txt.ClientID)
End If
Next
End If
End If
End Sub
Finally, I'v solved that problem by to apply the workaround below.
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'The ID is generated by to refer ClientID.
Dim foo = txt.ClientID
'Therefore, already txt.ID is not nothing.
SetValidators(cell.Controls, txt.ID)
Thank you for your cooperation.
I am trying to update GridView with a lot of records through an ObjectDataSource. I am only updating the changed records, so I use ViewState to check if the original datatable has changed. It works fine on my localhost/IIS but when I try the same solution on the deployment server it gives me an error:
Index was outside the bounds of the array.
In this line of code:
Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("ID = '{0}'", currentID))(0)
Below is an example of my code. Is there a problem with ViewState on the server?
Private ProdNum As String
Private tableCopied As Boolean = False
Private originalDataTable As System.Data.DataTable
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
If Not tableCopied Then
originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
ViewState("originalValuesDataTable") = originalDataTable
tableCopied = True
End If
End If
End Sub
Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click, btnUpdate1.Click
originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)
For Each r As GridViewRow In GridView1.Rows
If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False)
Next
' Rebind the Grid to repopulate the original values table.
tableCopied = False
GridView1.DataBind()
End Sub
Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
Dim currentID As Guid
Dim currentNyhed as Boolean
currentID = Guid.Parse(GridView1.DataKeys(r.RowIndex).Value.ToString())
currentNyhed = CType(r.FindControl("CheckBoxNews"), CheckBox).Checked
Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("ID = '{0}'", currentID))(0)
If Not currentNyhed.Equals(row("News").ToString()) Then Return True
Return False
End Function
The GridView is bound to an ObjectDataSource with these basic settings:
<asp:ObjectDataSource ID="ObjectDataSource1" ViewStateMode="Enabled" EnableViewState="true" runat="server" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByOverruled" TypeName="DSoverskrivslisteTableAdapters.Shop_Products_Egenskaber_OverrulingTableAdapter" UpdateMethod="Update">
The whole thing is based on this tutorial below. Instead of SqlDataSource I am using the ObjectDataSource which - as mentioned - works fine when run on my localhost, but gives me the error when uploaded to the deployment server.
http://msdn.microsoft.com/en-us/library/aa992036(v=vs.90).aspx
Please help.
I am using following code in rowdatabound function.
Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Footer Then
Dim ddlItem As DropDownList = CType(e.Row.FindControl("ddlFProjectLevels"), DropDownList)
If ddlItem IsNot Nothing Then
ddlItem.DataSource = objMileStone.GetProjectLevels()
ddlItem.DataValueField = "MileStoneID"
ddlItem.DataTextField = "Name"
ddlItem.DataBind()
End If
End If
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Edit Then
Dim ddlEProjectLevels As DropDownList = CType(e.Row.FindControl("ddlEProjectLevels"), DropDownList)
ddlEProjectLevels.DataSource = objMileStone.GetProjectLevels()
ddlEProjectLevels.DataValueField = "MileStoneID"
ddlEProjectLevels.DataTextField = "Name"
ddlEProjectLevels.DataBind()
ddlEProjectLevels.SelectedValue = milestoneid
End If
End If
End Sub
ddlEProjectLevels is dropdownlist in edititemtemplate. When I click edit in 1st row ddlEProjectLevels gets loaded with data from database. But in 2nd row dropdownlist does not contain values. Again in 3rd it gets loaded from db. Means in alternate rows, when I click edit dropdownlist(ddlEProjectLevels) doesn't load values. Can anybody help?
This issue is somewhat confusing compared to other controls and applies to GridView and DetailsView controls. You'll need to check the RowState enumeration using bitwise logic since an item may have a state of Alternate or Edit.
So instead of:
If e.Row.RowState = DataControlRowState.Edit Then
Try:
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
For more info check out the DataControlRowState Enumeration page.