Make bound field in grid like HyperLinkField - html

I have a nested gridview as shown below. I populate the gridview from a stored procedure and method in my DAL/BLL layers. This works fine and data is returned to my nested gridview as I expect.
What I'd like to do though, is have the boundfield (VisitTypeId) in my nested gridview as a hyperlink, so the user can click on it and be redirected to the related 'Visit' for the Patient (in other words, 'where ID={0} and VisitTypeId={0}').
I'm certain this must be possible, but can't find a way to do it. Can someone please help?
Markup:
<asp:GridView ID="gvPatients" runat="server" AutoGenerateColumns="False" DataSourceID="odsPatients" OnRowDataBound="gvVisits_RowDataBound"
DataKeyNames="ID" AllowPaging="True" CssClass="interactTable" CellPadding="4"
AllowSorting="True" meta:resourcekey="gvPersonsResource1">
<HeaderStyle CssClass="header" />
<RowStyle CssClass="row"/>
<Columns>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:Boundfield DataField="ID" HeaderText="ID" />
<asp:Boundfield DataField="Forename" HeaderText="Forename" />
<asp:Boundfield DataField="Surname" HeaderText="Surname" />
<asp:Boundfield DataField="DoB" HeaderText="DoB" DataFormatString="{0:dd/MM/yyyy}" />
<asp:TemplateField HeaderText="Visits">
<ItemTemplate>
<asp:GridView ID="gvVisits" runat="server" AutoGenerateColumns="false" BorderWidth="0" ShowHeader="false" >
<Columns>
<%-- <asp:BoundField DataField="PatientID" Visible="false" /> --%>
<asp:BoundField DataField="VisitTypeID" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/AddEditPatient.aspx?ID={0}" HeaderText="Edit" Text="Edit" />
</Columns>
</asp:GridView>
DAL:
Public Shared Function GetVisitType(PatientId As Integer) As DataSet
Using myConnection As New SqlConnection(AppConfiguration.ConnectionString)
Using myCommand As New SqlCommand("SPGetVisitType", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("PatientID", PatientId)
myConnection.Open()
Dim ds As New DataSet()
Using myAdapter As New SqlDataAdapter(myCommand)
myAdapter.Fill(ds)
Return ds
End Using
myConnection.Close()
End Using
End Using
End Function
BLL:
Public Shared Function GetVisitType(ByVal PatientId As Integer) As DataSet
Dim ds As DataSet = ScreeningTestDAL.Visit.GetVisitType(PatientId)
Return ds
End Function
Codebehind:
Protected Sub gvVisits_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gv As GridView = DirectCast(e.Row.FindControl("gvVisits"), GridView)
Dim PatientId As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
Dim ds1 As DataSet = ScreeningTestBLL.VisitManager.GetVisitType(PatientId)
gv.DataSource = ds1
gv.DataBind()
End If
End Sub

Related

Grid view is not displaying in asp.net web page

I am building my website every thing is going good ,but when I run my website the grid view doesn't appear ,, please would any one help me how to fix the problem and let the grid view appear on the page
here is the aspx code :
<%# Page Title="" Language="VB" MasterPageFile="~/Member.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script type="text/javascript">
var oldgridcolor;
function SetMouseOver(element) {
oldgridcolor = element.style.backgroundColor;
element.style.backgroundColor = '#ffeb95';
element.style.cursor = 'pointer';
element.style.textDecoration = 'underline';
}
function SetMouseOut(element) {
element.style.backgroundColor = oldgridcolor;
element.style.textDecoration = 'none';
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style="margin-right: 0cm; margin-left: 4.1cm; height: 679px; background-color: #CC0000; width: 1038px; margin-bottom: 0px; margin-top: 109px;">
<fieldset style="width:230px;">
<legend style="font-family: 'Times New Roman', Times, serif; font-size: xx-large; color: #FFFF00; font-weight: normal; font-style: normal">Menu<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:conStr %>"
SelectCommand="SELECT * FROM [Menu]">
</asp:SqlDataSource>
<br />
</legend>
<asp:GridView ID="MenuGrid" runat="server" AutoGenerateColumns="false"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="MealNo" DataSourceID="SqlDataSource1"
Width="990px" GridLines="None" OnRowDataBound="EmpGridView_RowDataBound">
<Columns>
<asp:BoundField DataField="MealNo" HeaderText="MealNo" />
<asp:BoundField DataField="MealName" HeaderText="MealName" />
<asp:BoundField DataField="Desp" HeaderText="Desp" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="MealCateID" HeaderText="MealCateID"/>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</fieldset>
</div>
</asp:Content>
vb code :
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class Order
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindEmpGrid()
End If
End Sub
Private Sub BindEmpGrid()
Dim dt As New DataTable()
Try
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
Dim adp As New SqlDataAdapter("Select * from Menu", con)
adp.Fill(dt)
If dt.Rows.Count > 0 Then
MenuGrid.DataSource = dt
MenuGrid.DataBind()
Else
MenuGrid.DataSource = Nothing
MenuGrid.DataBind()
End If
Catch ex As Exception
Response.Write("Error Occured: " & ex.ToString())
Finally
dt.Clear()
dt.Dispose()
End Try
End Sub
Protected Sub EmpGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles MenuGrid.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "javascript:SetMouseOver(this)"
e.Row.Attributes("onmouseout") = "javascript:SetMouseOut(this)"
End If
End Sub
End Class
You are not opening the connection anywhere. Open it or better wrap in Using ... End Using, Try like this:
Using con As New sqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
Dim adp As New SqlDataAdapter("Select * from Menu", con)
adp.Fill(dt)
End Using

GridView is not allowing to find any control

I want to use nested gridview. But my Parent Grid is showing Nothing in ChildGrid's variable.
Any help? I've tried following ways:
1)
Dim gvChild As GridView =
CType(Me.gvwSubjectForProject.FindControl("gvChildGrid"), GridView)
2)
Dim gvChild As GridView =
Me.gvwSubjectForProject.FindControl("gvChildGrid")
gvChild.DataSource = dt_Subject
gvChild.DataBind()
I want to find Child grid's id and then want to assign it data source.
VB
Private Function FillGrid(ByVal mode As String, ByVal dt_Subject As
DataTable) As Boolean
'Dim dt_Subject As DataTable = Nothing
'Dim dt_sortsubject As DataTable = Nothing
Try
If (mode = "ParentGrid") Then
Me.gvwSubjectForProject.DataSource = Nothing
Me.gvwSubjectForProject.DataBind()
'Me.ViewState(VS_CurrentSubject) = Nothing
Me.btnExport.Visible = False
'dt_Subject.Columns.Add("Status", GetType(String))
If Not dt_Subject Is Nothing Then
If dt_Subject.Rows.Count > 0 Then
If dt_Subject.Rows.Count > 0 Then
For Each dr_Row In dt_Subject.Rows
If dr_Row("cStatus") = "AC" Then
dr_Row("Status") = "Active"
ElseIf dr_Row("cStatus") = "IA" Then
dr_Row("Status") = "In Active"
ElseIf dr_Row("cStatus") = "HO" Then
dr_Row("Status") = "Hold"
ElseIf dr_Row("cStatus") = "SC" Then
dr_Row("Status") = "Screened"
ElseIf dr_Row("cStatus") = "BO" Then
dr_Row("Status") = "Booked"
ElseIf dr_Row("cStatus") = "OS" Then
dr_Row("Status") = "On Study"
ElseIf dr_Row("cStatus") = "FI" Then
dr_Row("Status") = "Forever Ineligible"
Else
dr_Row("cStatus") = "Not Found"
End If
Next
Me.btnExport.Visible = True
'Me.ViewState(VS_CurrentSubject) = dt_Subject
Me.gvwSubjectForProject.DataSource = dt_Subject
Me.gvwSubjectForProject.DataBind()
End If
End If
End If
ElseIf (mode = "ChildGrid") Then
If Not dt_Subject Is Nothing Then
If dt_Subject.Rows.Count > 0 Then
'Me.gvwSubjectForProject.DataSource = dt_Subject
'Me.gvwSubjectForProject.DataBind()
'Me.gvwSubjectForProject.FindControl("gvChildGrid").DataSource =
dt_Subject
'CType(Me.gvwSubjectForProject.FindControl("gvChildGrid"),
GridView).DataBind()
'Me.gvwSubjectForProject.FindControl("gvChildGrid").
'Me.gvwSubjectForProject.FindControl("gvChildGrid").DataBind()
Dim gvChild As GridView = Me.gvwSubjectForProject.FindControl("gvChildGrid")
gvChild.DataSource = dt_Subject
gvChild.DataBind()
End If
End If
End If
Return True
Catch ex As Exception
Me.ShowErrorMessage("Error While Binding Grid", "....FillGrid")
Return False
End Try
End Function
HTML NESTED GRIDVIEW
<asp:Panel ID="pnlgvwSubjectStatus" runat="server" Style="max-height: 500px; overflow: auto;
width: 60%;">
<asp:GridView ID="gvwSubjectForProject" runat="server" AutoGenerateColumns="false"
SkinID="grdViewSmlAutoSize" Width="70%" ShowFooter="true" AllowPaging="true"
PageSize="1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtnExpand" runat="server" ImageUrl="../images/Plus.gif" alt="Expand"
OnClientClick="imgbtnExpand_Click" />
<asp:HiddenField ID="hcStatus" runat="server" Value='<%# Eval("cStatus") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Status" HeaderText="STATUS">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="40%" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="40%" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<div style="max-height: 300px; width: 100%; overflow: auto;
text-align: right;">
<%--<asp:Panel ID="pnlOrders" runat="server" Width="100%">--%>
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" SkinID="grdViewSmlAutoSize">
<Columns>
<asp:BoundField ItemStyle-Width="40%" DataField="vSubjectID" HeaderText="SUBJECT ID">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="40%" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="40%" />
</asp:BoundField>
<asp:BoundField ItemStyle-Width="60%" DataField="FullName" HeaderText="FULL NAME">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="60%" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="60%" />
</asp:BoundField>
</Columns>
</asp:GridView>
<%--</asp:Panel>--%>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TotalSubject" HeaderText="TOTAL SUBJECTS">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="50%" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="50%" />
</asp:BoundField>
</Columns>
</asp:GridView>
</asp:Panel>
And one more thing, in HTML when child gridview is not displayed, its still filling while creating its DOM. I want to delete that blank space too.
Your posted code is not working, because you are using the FindControl() method on the entire parent grid view control instead of on an individual row in the grid view control, like this:
Find child grid view in OnRowDataBound event:
Sub ParentGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' Only look for child grid view in data rows, ignore header and footer rows
If e.Row.RowType = DataControlRowType.DataRow Then
Dim theChildGridView As GridView = DirectCast(e.Row.FindControl("gvChildGrid"), GridView)
' Do something with theChildGridView here
End If
End Sub
Find child grid view by looping through all rows in parent grid view control:
For Each row As GridViewRow In ParentGridView.Rows
' Only look for child grid view in data rows, ignore header and footer rows
If row.RowType = DataControlRowType.DataRow Then
Dim theChildGridView As GridView = DirectCast(row.FindControl("gvChildGrid"), GridView)
' Do something with theChildGridView here
End If
Next

GridView Column modification

I have bound my gridView with code behind which Contains 4 column but I want first column to be a linkbutton.
So I have added itemtemplate but now my grid contain 5 columns with two same columns, but I want only one column which is of linkbutton column.
This is my itemtemplate code
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'
OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Code behind grid binding :
Bussiness_logic.GridView_Bind(GridView1, "GET_DATA");
public static void GridView_Bind(GridView Grid_Name,String Procdure_name)
{
SqlDataAdapter dap = new SqlDataAdapter(Procdure_name, GetConnection());
DataSet ds = new DataSet();
dap.Fill(ds);
Grid_Name.DataSource = ds;
Grid_Name.DataBind();
}
Your Getting five columns because you Set GridView AutoGenerateColumn to trueAND add an extra template column in grid view.So GridView generate columns for all columns that are present in your data set and add you extra column i.e your template column.
Now to solve this issue Set AutoGenerateColoumn Property of GridView To false. Define Your Column Like below..
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>' OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameofSecondColumnInYourDateSet" HeaderText="Column2"/>
<asp:BoundField DataField="NameofThirdColumnInYourDateSet" HeaderText="Column3"/>
<asp:BoundField DataField="NameofFourthColumnInYourDateSet" HeaderText="Column4"/>
</Columns>

select and update multiple checkbox in gridview

I want to select multible record in one time and update them by a buttom
I searched the internet by it's always cs and I want vb.net
I did my grid view as following
<asp:GridView ID="GridView1" runat="server" AutoGenerateCheckboxColumn="True"
CheckboxColumnIndex="0" AllowSorting="true"
AutoGenerateColumns="False" DataKeyNames="USER_ID"
DataSourceID="SqlDataSource1StudentActivList" CellPadding="2">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName"
SortExpression="MiddleName" />
<asp:BoundField DataField="ProgramCODE" HeaderText="ProgramCODE"
SortExpression="ProgramCODE" />
<asp:BoundField DataField="USER_NAME" HeaderText="USER_NAME"
SortExpression="USER_NAME" />
<asp:BoundField DataField="USER_Email" HeaderText="USER_Email"
SortExpression="USER_Email" />
<asp:BoundField DataField="MajorNameInEnglish" HeaderText="MajorNameInEnglish"
SortExpression="MajorNameInEnglish" />
<asp:BoundField DataField="GivenStudentID" HeaderText="GivenStudentID"
SortExpression="GivenStudentID" />
<asp:BoundField DataField="accepted" HeaderText="accepted"
SortExpression="accepted" />
<asp:BoundField DataField="Gender" HeaderText="Gender"
SortExpression="Gender" />
<asp:BoundField DataField="USER_ID" HeaderText="USER_ID" ReadOnly="True"
SortExpression="USER_ID" />
</Columns>
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1StudentActivList" runat="server"
ConnectionString="<%$ ConnectionStrings:mydbConnectionString %>"
SelectCommand="SELECT DISTINCT [FirstName], [LastName], [MiddleName], [ProgramCODE], [USER_NAME], [USER_Email], [MajorNameInEnglish], [GivenStudentID], [accepted], [Gender], [USER_ID] FROM [DIP_StudentsActivationList]">
</asp:SqlDataSource>
<p><asp:Label ID="lblSelection" runat="server" /></p>
<asp:Button ID="btupdate" runat="server" Text="Update" />
this is the VB code(converted) it's not what i want but i think i can deal with it
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Public Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
If MultiSelectGridView1.SelectedDataKeys.Count > 0 Then
lblSelection.Text = "You selected employees: "
For Each k As DataKey In MultiSelectGridView1.SelectedDataKeys
lblSelection.Text += k.Value.ToString() & ", "
Next
lblSelection.Text = lblSelection.Text.TrimEnd(","C, " "C)
Else
lblSelection.Text = "No employees selected"
End If
End Sub
End Class
and I want to change the accepted status in my database by clicking the buttom
any help please

Stored procedure to format data from temp object and put in grid view

I am new to stored procedure.
I would like to change the format of data that I get from stored procedure. For example, a column returns a string value of 20110918190154, I would like to change it to 2011/09/18 19:01:54.
This is how I execute the stored procedure and return a DataTable:
storedProcCommand = new SqlCommand("GetPurchaseHistory", conn);
storedProcCommand.CommandType = CommandType.StoredProcedure;
storedProcCommand.Parameters.Add("#customerID", cCustomerID);
adapter = new SqlDataAdapter(storedProcCommand);
adapter.Fill(allData);
Show the data in GridView as below:
<asp:GridView ID="GridView_PurchaseHistory" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="cTranDateTime" HeaderText="Latest Purchase Date" />
<asp:BoundField DataField="cItemNo" HeaderText="Item Number" />
<asp:BoundField DataField="cDescDef" HeaderText="Description" />
<asp:BoundField DataField="fQty" HeaderText="Total Quantity" />
</Columns>
</asp:GridView>
The output would be like:
Latest Purchase Date
20121007193111
20120130171010
20110918190154
I have written the code to substring and change the format which takes in 1 string as a time. I would like to know if there is anyway I can store the Date in a temp object and change its format accordingly.. Or if I could change before displaying it.
Thank you so much.. Any advice is appreciated.
Lets say I have a list of string like below
List<string> dates= new List<string>();
dates.Add("20121007193111");
dates.Add("20120130171010");
dates.Add("20110918190154");
and a gridview like
<asp:gridview runat="server" ID="gvDates" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Last Purchase Date">
<ItemTemplate>
<%#ModifyDate(Container.DataItem.ToString())%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Define the ModifyDate function in code behind as below
public string ModifiedDate(string date)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyyMMddHHmmss";
return DateTime.ParseExact(date, format, provider).ToString("yyyy/MM/dd HH:mm:ss");
}
Finally bind the list with the gridview
gvDates.DataSource = dates;
gvDates.DataBind();
I hope it solves your problem.