DropDown binding in gridview - asp.net

I have this code:
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server" AppendDataBoundItems="true"
DataSourceID="SqlDataSource3" DataTextField="GestisciIP"
DataValueField="GestisciIP" SelectedValue='<%# Bind("GestisciIP") %>'>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The datasource contain the value of a field selected on the current user (user id) and it can be True or False.
My problem is that I want the dropdown to always shows the 2 options True and False, but it should also have the selected value got from the datasource.
If I remove the 2 lines
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
it only shows the value got from the db, if I leave these lines it shows 3 values: the one got from the db and True, False added by this code.
So, how can I do this? I hope I was clear enough, thanks
IT'S DONE!
Thank you again
ASPX
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
CODE BEHIND
Private Sub GridView2_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
Dim LblddlGestisciIP As Label = DirectCast(e.Row.FindControl("LblddlGestisciIP"), Label)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
Private Sub GridView2_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
Dim ddlGestisciIP As DropDownList = DirectCast(GridView2.Rows(GridView2.EditIndex).FindControl("ddlGestisciIP"), DropDownList)
SqlDataSourceUtenti.UpdateCommand = "UPDATE [Utenti] SET [RfProfilo] = #RfProfilo, [Nome] = #Nome, [Cognome] = #Cognome, [Username] = #Username, [Password] = #Password, [Badge] = #Badge, [IpCorretto] = #IpCorretto, [GestisciIp] = " & ddlGestisciIP.SelectedValue & " WHERE [IdUtente] = #Original_IdUtente"
End Sub

try this,add Onrowdatabound event to your gridview and bind your dropdownlist in that event.
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" CellPadding="4" DataSourceID="SqlDataSourceUtenti"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
DataKeyNames="IdUtente" BorderColor="#E1E1E1" BorderStyle="Solid"
BorderWidth="3px" OnRowDataBound="GridView2_RowDataBound" OnRowCommand="GridView2_RowCommand" OnRowUpdating="GridView2_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RfProfilo" HeaderText="RfProfilo"
SortExpression="RfProfilo" Visible="False" />
<asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
<asp:BoundField DataField="Cognome" HeaderText="Cognome"
SortExpression="Cognome" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:TemplateField HeaderText="Profilo" SortExpression="Profilo">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Nome" DataValueField="idProfilo"
SelectedValue='<%# Bind("RfProfilo") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeProfilo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="Password">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# mascheraPassword(Eval("Password").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Badge" HeaderText="Badge" SortExpression="Badge" />
<asp:TemplateField HeaderText="IP di origine" SortExpression="IpCorretto">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IpCorretto") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblIpCorretto" maxlength="16" runat="server" Text='<%# limitaCaratteri(Eval("IpCorretto").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Aggiorna" ForeColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Annulla" ForeColor="White"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Modifica"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Elimina"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</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>
in code behind
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
First ,With the use of RowCommand Event of the Grid View , Get the value of the Edited Label into the ViewState,then Use viewstate and Handle your RowDataBound
and to upadate your data you can use roeupdate event,like follows
protected void Gridview2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
//add code for updating values in database
}

If i understood correct, you only want the values TRUE and FALSE, so, you don't need a datasource to do this...
Remove this properties DataSourceID, DataTextField, DataValueField and SelectedValue...
Try it please.

Related

Grid View Footer doesn't show even though the footer template exists

I have a Grid View. The Data displays correctly. However the footer doesn't show. Can anyone tell me what is wrong ?
Below is the code:-
<asp:GridView ID="grdJLLAlloc" runat="server" AutoGenerateColumns="false" Width="400px"
ShowFooter="True" CellPadding="4" SkinID="OrigBkrAlloc">
<Columns>
<asp:BoundField DataField="Professional" HeaderText="Professional" />
<asp:BoundField DataField="Market" HeaderText="Market" />
<asp:BoundField DataField="BusinessLine" HeaderText="Business Line" />
<asp:BoundField DataField="PSBusinessUnit" HeaderText="Business Unit" />
<asp:TemplateField HeaderText="%">
<HeaderStyle HorizontalAlign="Center" Wrap="True" Width="170px" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblJLLAllocPercentage" runat="server" Text='<%# Eval("Percentage") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblJLLAllocTotalPer" runat="server" Text="" Visible="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<HeaderStyle HorizontalAlign="Center" Wrap="True" Width="170px" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblJLLAllocAmt" runat="server" Text='<%# Eval("Amount") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblJLLAllocTotalAmt" runat="server" Text="" Visible="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Private Sub LoadJllAllocGrid()
Dim dsJLLCommAlloc As New DataSet("JLLCommissionAlloc")
Try
If m_DealId > 0 Then
dsJLLCommAlloc = oComm.GetCapForceJLLCommissionAlloc(m_DealId)
grdJLLAlloc.DataSource = dsJLLCommAlloc
grdJLLAlloc.DataBind()
End If
Catch ex As Exception
ExceptionPolicy.HandleException(ex, Common.EXCEPTION_POLICY)
End Try
End Sub
Private Sub grdJLLAlloc_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdJLLAlloc.RowDataBound
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalAmt As Label = DirectCast(e.Row.FindControl("lblJLLAllocTotalAmt"), Label)
lblTotalAmt.Text = "100,000$"
End If
End Sub
It looks like you forgot to bind the grdJLLAlloc_RowDataBound method to the GridView
<asp:GridView ID="grdJLLAlloc" OnRowDataBound="grdJLLAlloc_RowDataBound"

How to get the ID of the record in the GridViewRow in loop?

Aim: Gridview with checkbox on each line, user then clicks a button and all ticked lines are actioned in the button code. I need the ID's of all the records where the CheckBox is checked.
Error:
Object reference not set to an instance of an object.
on row: ID_Current = row.FindControl("ID").ToString
if I change the to ID_Current = DirectCast(row.FindControl("cbSelect"), CheckBox).Checked I get 'True' as a result but I already know that and want to get the ID.
Aspx-Code with my Gridview:
<asp:Panel ID="ActionGrid" runat="Server">
<h2>Actions To Edit</h2>
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="UPRN" DataSourceID="SqlDataSource3" EmptyDataText="There are no data records to display." Font-Size="Medium" PagerStyle-CssClass="pgr" Width="1000px">
<%-- AutoGenerateEditButton="True"--%>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ControlStyle-Width="50px" DataField="UPRN" HeaderText="UPRN" ReadOnly="True" SortExpression="UPRN" />
<asp:BoundField DataField="LocationItemPosition" HeaderText="Location Item Position" ReadOnly="True" SortExpression="LocationItemPosition" />
<asp:TemplateField HeaderText="Surveye" SortExpression="SurveyDate">
<ItemTemplate>
<asp:Label ID="lblSurveyDate" runat="server" Text='<%# apFunctionCharacters.fncDateTidy(Eval("SurveyDate"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemRef" HeaderText="Item Ref" ReadOnly="True" SortExpression="ItemRef" />
<asp:BoundField DataField="OverallRiskCategory" HeaderText="Overall Risk Category" ReadOnly="True" SortExpression="OverallRiskCategory" />
<asp:BoundField DataField="Comments" HeaderText="Comments" ReadOnly="True" SortExpression="Comments" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Panel>
Submit button:
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
.aspx.vb stored procedure/action code
Protected Sub btnChangeToNA_Click(sender As Object, e As EventArgs) Handles btnChangeToNA.Click
For Each row As GridViewRow In GridView3.Rows
Dim ID_Current As String = ""
'read the label
If DirectCast(row.FindControl("cbSelect"), CheckBox).Checked Then
ID_Current = row.FindControl("ID").ToString
' change value
'############stored procedure here
Dim connection As SqlConnection
Dim command As New SqlCommand
Dim ds As New DataSet
Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("MY_SQL").ToString()
connection = New SqlConnection(ConnectionString1)
connection.Open()
With command
.Connection = connection
.CommandText = "spChangeValue "
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
.Parameters.AddWithValue("#ID_Current", ID_Current)
.ExecuteNonQuery()
End With
'#############################
lblTest.Text += ID_Current
End If
'Loop
Next
End Sub
With FindControl you find controls(what suprise) not strings. And you have to pass the ID of the control not a property like Id.
So this doesnt make much sense:
Dim ID_Current As String = row.FindControl("ID").ToString()
Instead you could store the ID in a HiddenField, so on aspx:
<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID_Current") %>' />
Now you can get the reference to this HiddenField with FindControl:
Dim hiddenID As HiddenField = DirectCast(row.FindControl("HiddenID"), HiddenField)
Dim ID_Current As String = hiddenID.Value
You need a control with the id "ID"
<ItemTemplate>
<asp:HiddenField ID="ID" runat="server" Value='<%# FillMeSomehow%>' />
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>

Error in exporting selected rows from Gridview to Excel

I am trying to export selected columns from GridView to Excel, and every time I run the code an error pops up with some new .dll file name. What could be the issue? Please find the code below for your reference.
<asp:Panel ID="general" runat="server" BorderColor="#333333" BorderStyle="Double" CssClass="emailpnl" Width="800px">
<asp:GridView ID="bestpractgrv" runat="server" AutoGenerateColumns="False" OnLoad="bestpractgrv_Load" CellPadding="3" GridLines="Vertical" Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AllowPaging="True" OnPageIndexChanging = "OnPaging">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID ="boolcb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="idlbl" runat="server" Text='<%# Bind("iID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discipline ID">
<ItemTemplate>
<asp:Label ID="discplnlbl" runat="server" Text='<%# Bind("iDisciplineID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Created">
<ItemTemplate>
<asp:Label ID="dateclbl" runat="server" Text='<%# Bind("dDateCreated")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="bptitlelbl" runat="server" Text='<%# Bind("cBPTitle")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</asp:Panel>
VB Code
Private Sub PopulateCheckBoxArray()
Dim CheckBoxArray As ArrayList
If ViewState("CheckBoxArray") IsNot Nothing Then
CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
Else
CheckBoxArray = New ArrayList()
End If
Dim CheckBoxIndex As Integer
Dim CheckAllWasChecked As Boolean = False
Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)
Dim checkAllIndex As String = "chkboxSelectAll-" & bestpractgrv.PageIndex
If chkAll.Checked Then
If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
CheckBoxArray.Add(checkAllIndex)
End If
Else
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
CheckBoxArray.Remove(checkAllIndex)
CheckAllWasChecked = True
End If
End If
For i As Integer = 0 To bestpractgrv.Rows.Count - 1
If bestpractgrv.Rows(i).RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = DirectCast(bestpractgrv.Rows(i).Cells(0).FindControl("boolcb"), CheckBox)
CheckBoxIndex = bestpractgrv.PageSize * bestpractgrv.PageIndex + (i + 1)
If chk.Checked Then
If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 AndAlso Not CheckAllWasChecked Then
CheckBoxArray.Add(CheckBoxIndex)
End If
Else
If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 OrElse CheckAllWasChecked Then
CheckBoxArray.Remove(CheckBoxIndex)
End If
End If
End If
Next
ViewState("CheckBoxArray") = CheckBoxArray
End Sub
And I get the error on following line
Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)
This is the error message
An exception of type 'System.NullReferenceException' occurred in App_Web_pqx0e3wy.dll but was not handled in user code
After every compilation the dll name changes, while App_Web_XXXX.dll remains constant

Bold Gridview Rows Based on Cell Contents

I know this question has been asked several times before and I have tried many of the suggested solutions. I am attempting to bold each row of my GridView that contains the text "Earned" or "Total" (haven't added "total" code yet). I have successfully gotten the DataBound event to fire, and I've successfully gotten it to recognize the 2 For looping statements that I use to loop through each row and column (I use labels to let me know which code blocks I've accessed and the counts are 24 rows and 9 columns), but for some reason, the code that I use to specifically determine if the cell contains the text "Earned" won't fire. I'm not sure what I'm doing wrong. Could someone take a look and help me understand what I'm doing wrong? Thank you for your help!
Here's my ASP code for the GridView (sorry, not sure why it's in multiple blocks):
<asp:GridView ID="gv_VacationDetails" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="VacationDetails_DataSource" GridLines="Vertical" style="z-index: 1; left: 24px; top: 275px; position: absolute; height: 220px; width: 435px" ShowFooter="True" EnableViewState="True" AutoPostback="True" OnRowDataBound="gv_VacationDetails_DataBound">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="Action" SortExpression="Employee_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Employee_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl_InsertVacAction" runat="server" OnSelectedIndexChanged="gv_VacationDetails_SelectedIndexChanged" EnableViewState="True" AutoPostback="True" TabIndex="1">
<asp:ListItem>Adjustments</asp:ListItem>
<asp:ListItem>Used</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Employee_Name") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Unit_Area_ID" HeaderText="Unit_Area_ID" SortExpression="Unit_Area_ID" Visible="False" />
<asp:TemplateField HeaderText="Vacation (Days)" SortExpression="Vacation" FooterStyle-Width="133px" FooterStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Vacation")%'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txb_InsertVacHours" runat="server" Width="115px" TabIndex="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="val_VacationTimeReq" runat="server" ControlToValidate="txb_InsertVacHours" ErrorMessage="Vacation Time is a required field. Please enter in DAYS." ForeColor="#FF3300" ValidationGroup="Vacation">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Vacation Time - You can only enter a positive or negative number." ForeColor="Red" Operator="DataTypeCheck" Type="Double" ControlToValidate="txb_InsertVacHours" ValidationGroup="Vacation">*</asp:CompareValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Vacation") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Used" SortExpression="Used_Date" FooterStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Used_Date") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbx_InsertVacUsed" runat="server" Width="115px" TabIndex="3"></asp:TextBox>
<asp:RegularExpressionValidator ID="Val_VacDateCheck" runat="server" ControlToValidate="tbx_InsertVacUsed" ErrorMessage="Vacation Used - Please enter the dat in format mm/dd/yyyy" ForeColor="#FF3300" ValidationExpression="^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d$" ValidationGroup="Vacation">*</asp:RegularExpressionValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Used_Date", "{0:d}") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Updated" SortExpression="Update_Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Update_Date") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btn_InsertVacation" runat="server" CommandName="InsertVacation" Text="Add Action" TabIndex="4" />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Update_Date", "{0:d}") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="First_Name" HeaderText="First_Name" SortExpression="First_Name" Visible="False" />
<asp:BoundField DataField="Last_Name" HeaderText="Last_Name" SortExpression="Last_Name" Visible="False" />
<asp:BoundField DataField="Unit_ID" HeaderText="Unit_ID" SortExpression="Unit_ID" Visible="False" />
<asp:BoundField DataField="HireRehire" HeaderText="HireRehire" SortExpression="HireRehire" Visible="False" />
<asp:BoundField DataField="Display_Year" HeaderText="Display_Year" SortExpression="Display_Year" Visible="False" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Here is my DataBound code:
Public Sub gv_VacationDetails_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Label6.Text = "Inside Bold"
'Label6.Text = gv_VacationDetails.Rows(1).Cells("Action").Text.ToString
Dim r As Integer
Dim c As Integer
For r = 0 To gv_VacationDetails.Rows.Count - 1
Label6.Text = "Inside ROW"
For c = 0 To gv_VacationDetails.Columns.Count - 1
Label6.Text = "Inside COLUMN"
Label7.Text = "Rows = " & CType(gv_VacationDetails.Rows.Count - 1, String) & "; Columns = " & CType(gv_VacationDetails.Columns.Count - 1, String)
If gv_VacationDetails.Rows(r).Cells(c).Text.Trim = "Earned" Then
Label6.Text = "Inside BOLD IF"
gv_VacationDetails.Rows(r).Cells(c).Font.Bold = True
'gv_VacationDetails.Rows(r).Cells(c).ForeColor = Drawing.Color.Red
End If
Next
Next
'For i = 1 To gv_VacationDetails.Rows.Count - 1
' 'Label6.Text = gv_VacationDetails.Rows(i).Cells(1).Text.ToString
' If gv_VacationDetails.Rows(i).Cells(0).Text = "Earned" Then
' Label6.Text = "Inside Bold IF"
' gv_VacationDetails.Rows(i).Font.Bold = True
' End If
'Next
'If e.Row.RowType = DataControlRowType.DataRow Then
' Label6.Text = "Inside Bold IF"
' Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
' If CType(drv(0), String) = "Earned" Or CType(drv(0), String) = "Total Adjustments" Or _
' CType(drv(0), String) = "Total Used" Or CType(drv(0), String) = "Total Remaining" Then
' e.Row.Font.Bold = True
' End If
'End If
End Sub
The method you used to access the GridView rows cells will only work with bound fields not template fields. In order to access a template field you can use Row.FindControl method.
Please try this instead in your gv_VacationDetails_DataBound method:
Public Sub gv_VacationDetails_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'First you need to get the template fields, which are labels as in GridView your design
Dim Label1 As Label = DirectCast(e.Row.FindControl("Label1"), Label)
Dim Label2 As Label = DirectCast(e.Row.FindControl("Label2"), Label)
Dim Label3 As Label = DirectCast(e.Row.FindControl("Label3"), Label)
Dim Label4 As Label = DirectCast(e.Row.FindControl("Label4"), Label)
'Next you can check each field value
If Label1.Text = "Earned" Then
Label1.Font.Bold = True
End If
If Label2.Text = "Earned" Then
Label2.Font.Bold = True
End If
If Label3.Text = "Earned" Then
Label3.Font.Bold = True
End If
If Label4.Text = "Earned" Then
'Label6.Text = "Inside BOLD IF"
Label4.Font.Bold = True
End If
'You can check the bound fields by looping through row cells
For Each cell As TableCell In e.Row.Cells
If cell.Text = "Earned" Then
cell.Font.Bold = True
End If
Next
End If
End Sub

Edit template not displaying

I have a gridview that has a item template and an edit template. The problem is, whenever I click the edit button, the edit templates don't show up.
Here is my page code:
<asp:GridView
ID="RoutesGridView"
runat="server"
CssClass="table table-striped table-hover"
GridLines="None"
AutoGenerateColumns="False"
DataKeyNames="RouteId,LocationId,ConcurrencyId"
AllowPaging="true"
EmptyDataText="No Information Retrieved">
<Columns>
<asp:TemplateField Visible="false">
<EditItemTemplate>
<asp:Label id="RouteIdLB" runat="server" Text='<%# Bind("RouteId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="RouteIdLB" runat="server" Text='<%# Bind("RouteId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="NameTB" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="NameLB" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description" >
<EditItemTemplate>
<asp:TextBox ID="DescriptionTB" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="DescriptionLB" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Zone" SortExpression="TimeZoneCode">
<EditItemTemplate>
<asp:DropDownList ID="TimeZoneDDL" runat="server" DataSource="<%# GetTimeZones() %>" AppendDataBoundItems="true" DataTextField="TimeZoneCode" DataValueField="TimeZoneId" SelectedValue='<%# Bind("TimeZoneId") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TimeZoneCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="IsActive" HeaderStyle-CssClass="textCenterAlign" ItemStyle-CssClass="textCenterAlign">
<EditItemTemplate>
<asp:CheckBox ID="IsActiveCB" runat="server" Checked='<%# Bind("IsActive") %>' />
</EditItemTemplate>
<ItemTemplate>
<%# IIf(Eval("IsActive").Equals(True), "<i class='icon-ok'></i>", " ")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Deleted" SortExpression="IsDeleted" HeaderStyle-CssClass="textCenterAlign" ItemStyle-CssClass="textCenterAlign">
<EditItemTemplate>
<asp:CheckBox ID="IsDeletedCB" runat="server" Checked='<%# Bind("IsDeleted") %>' Enabled="False" />
</EditItemTemplate>
<ItemTemplate>
<%# IIf(Eval("IsDeleted").Equals(True), "<i class='icon-ok'></i>", " ")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" ItemStyle-CssClass="actions">
<EditItemTemplate>
<asp:LinkButton ID="SaveButton" runat="server" CssClass="btn btn-mini btn-primary hiddenButton" CommandName="Update" Visible="True" Enabled="True" ToolTip="Save pending changes" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-ok icon-white"></i>Save</asp:LinkButton>
<asp:LinkButton ID="CancelButton" runat="server" CssClass="btn btn-mini btn-danger hiddenButton" CommandName="Cancel" Visible="True" Enabled="True" ToolTip="Cancel pending changes" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-ban-circle icon-white"></i>Cancel</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CssClass="btn btn-mini btn-primary hiddenButton" CommandName="Edit" Visible="True" Enabled="True" ToolTip="Edit this entry" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>'><i class="icon-edit icon-white"></i>Edit</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CssClass="btn btn-mini btn-danger hiddenButton" CommandName="Delete" Visible="True" Enabled="True" ToolTip="Delete this entry" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' OnClientClick='return confirm("Are you certain that you want to delete this entry?");'><i class="icon-trash icon-white"></i>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB code behind:
Protected Sub RoutesGridView_RowEditing(ByVal sender As System.Object, ByVal e As GridViewEditEventArgs) Handles RoutesGridView.RowEditing
Dim gv As GridView = CType(sender, GridView)
Dim row As GridViewRow = gv.Rows(e.NewEditIndex)
Dim RouteId As Integer = CInt(CType(row.FindControl("RouteIdLB"), Label).Text)
_Route = RouteManager.GetItem(RouteId, _dftIdentity)
gv.EditIndex = e.NewEditIndex
RoutesGridView_DataBind()
End Sub
Protected Sub RoutesGridView_RowCancelingEdit(ByVal sender As System.Object, ByVal e As GridViewCancelEditEventArgs) Handles RoutesGridView.RowCancelingEdit
_Route = Nothing
RoutesGridView.EditIndex = -1
RoutesGridView_DataBind()
End Sub
Protected Sub RoutesGridView_RowUpdating(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles RoutesGridView.RowUpdating
Try
Dim gv As GridView = CType(sender, GridView)
Dim row As GridViewRow = gv.Rows(e.RowIndex)
With _Route
.Name = CType(row.FindControl("NameTB"), TextBox).Text
.TimeZoneId = CType(row.FindControl("TimeZoneDDL"), DropDownList).SelectedValue
.IsActive = CType(row.FindControl("IsActiveCB"), CheckBox).Checked
End With
RouteManager.Update(_Route, _dftIdentity)
_Route = Nothing
gv.EditIndex = -1
RoutesGridView_DataBind()
Catch ex As Exception
ProcessException(ex)
End Try
End Sub
You need to change the Grid edit form property from auto
to template

Resources