I have this GridView:
<asp:GridView CssClass="table table-hover mt-5" ID="OrderTable" runat="server" AutoGenerateColumns="False" DataKeyNames="Identificativo" DataSourceID="DBGestioneOrdini" OnPreRender="GridView_PreRender">
<Columns>
<asp:BoundField DataField="Identificativo" HeaderText="Identificativo" InsertVisible="False" ReadOnly="True" SortExpression="Identificativo" />
<asp:TemplateField HeaderText="Genera Fattura" HeaderStyle-CssClass="text-center" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton type="button" ID="generateInvoiace" runat="server" OnClick="generateInvoiace_Click" ForeColor="Black" CommandName="SelectRow" CommandArgument='<%# Eval("Identificativo") %>'>
<i class="fa-solid fa-file-invoice-dollar fa-lg>
</i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It takes the data from an Order Table. The problem here is that I need to disable the clickable icon for every record which have a specific value on a field of the db ... I can't actually realize how can i modify only certain icons since the table is rendered by asp.net and not manually
Ok, so say we have this grid view:
<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server" Checked='<%# Eval("Active") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hotel Information" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="cmdView" runat="server" Text="Info" CssClass="btn"
OnClick="cmdView_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And our code to load the grid view could be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid
End If
End Sub
Sub LoadGrid()
Dim rstData As DataTable
rstData = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
GHotels.DataSource = rstData
GHotels.DataBind()
End Sub
Public Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
rstData.TableName = strSQL
End Using
End Using
Return rstData
End Function
So no loops, just send a data table to the grid, and we now have this:
However, lets say we ONLY want the info button to show for when the hotel is active.
so, for things like totals, time formatting, color formatting, or whatever?
You use the row data bound event.
So, we can add to above this event that will "hide" the info button for any row in which the hotel is not active.
So, this code:
Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chkActive As CheckBox = e.Row.FindControl("chkActive")
Dim cmdView As Button = e.Row.FindControl("cmdView")
If chkActive.Checked = False Then
' hide the info button
cmdView.Style.Add("display", "none")
End If
End If
End Sub
So, as noted, for running totals, highlght a row, color a row - or in this simple case hide/show the info button based on active column?
(and note you are NOT limited to just the controls on that row - you have full use of the whole database row if you wish durng the binding).
And we now see/get this:
And note once again, no looping or for next code.
Related
So, I am trying to user an asp:RadioButtonList in an asp:Table element. I am needing to repeat the radio buttons into four separate cells for selection by the user. I have been playing around with it for a couple days and just can't figure it out. I included the code I currently have below as well as a picture example of what I am trying to achieve.
<asp:Table runat="server" CssClass="table table-sm table-bordered text-center align-items-center">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
<h5>ExampleText</h5>
</asp:TableHeaderCell>
<asp:TableHeaderCell>
<asp:Label Text="ExampleText" runat="server" />
</asp:TableHeaderCell><asp:TableHeaderCell>
<asp:Label Text="ExampleText" runat="server" />
</asp:TableHeaderCell><asp:TableHeaderCell>
<asp:Label Text="ExampleText" runat="server" />
</asp:TableHeaderCell><asp:TableHeaderCell>
<asp:Label Text="ExampleText" runat="server" />
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
ExampleText
</asp:TableCell>
<asp:TableCell ColumnSpan="4">
<asp:radiobuttonlist runat="server" RepeatDirection="Horizontal" >
<asp:listitem />
<asp:listitem />
<asp:listitem />
<asp:listitem />
</asp:radiobuttonlist>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
But this ends up lookging more like this...
Well, if you drop in a Radiobutton list as a "single" control into a column, then you going to get/have/see a radio button list in that ONE column.
However, you can of course use separate radio button(s), and not a list.
And it turns out you can have those "separate" buttons STILL behave as a group. In other words, grouping the radio buttons means all of the "logic" to select one, but de-select the others STILL works.
So, we would need to have say 5 columns in the table (or better yet gridview), and then use the "group" option to glue them back together.
However, with say 5 options, and 5 seperate buttons? Well, then we kind of don't care about a "value" but ONLY if the RB is checked, or not.
So, say we have a hotel rating from 1 to 5.
Normally, we could have a "value" for each button, and thus set 1-5, or get 1-5.
but, now we have 5 buttons, so we have to write extra code to take the value 1 to 5, and translate into our 5 seperate buttons.
Hence, lets call them R1 to R5, and that keeps this simple.
So, lets drag in a grid view, and THEN add 5 RB's, for the seperate columns:
So, we have this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" Width="50%">
<Columns>
<asp:BoundField DataField="HotelName" HeaderText="Hotel Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="5 Star" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><asp:RadioButton ID="R1" runat="server" GroupName="G1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Excellent" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><asp:RadioButton ID="R2" runat="server" GroupName="G1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Good" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><asp:RadioButton ID="R3" runat="server" GroupName="G1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fair" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><asp:RadioButton ID="R4" runat="server" GroupName="G1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Poor" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><asp:RadioButton ID="R5" runat="server" GroupName="G1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And our code behind to load up the grid could be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT * FROM tblHotelsA ORDER BY HotelName"
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
Dim rstData = New DataTable
rstData.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstData
GridView1.DataBind()
End Using
End Using
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gData As DataRowView = e.Row.DataItem
Dim intRating As Integer = gData("Rating")
If intRating > 0 Then
Dim strRadioBut As String = "R" & intRating
Dim RB As RadioButton = e.Row.FindControl(strRadioBut)
RB.Checked = True
End If
End If
End Sub
so, on row databound, we could have bound JUST the value if this was as single radio button list, but we now have to pluck out WHICH RB to check box based on the hotel rating column (with a value from 1 to 5).
The end result is thus this:
Was custom pagination available in .Net Framework 4, If yes how we can implement?
Currently I am implementing this by using GridView.VirtualItemCount however this is not avaible in .NET 4.
Hum, it would seem that VirtualItemCount is not available in .net 4.0.
However, GridViews still supported paging in .net 4.0.
So, if I have a gv, and turn on paging like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover" Width="50%"
DataKeyNames="ID" AllowPaging="True">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Hotel Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="txtHotel" runat="server"
Text='<%# Eval("HotelName") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="cmdDelete" runat="server" CssClass="btn btn-default"
OnClick="cmdDelete_Click"
OnClientClick="return mydelprompt(this)" >
<span aria-hidden="true" class="glyphicon glyphicon-trash"></span>
Delete
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And code to load - (don't load with a data reader - they don't work - you need a "interable" data source.
So, code to load the grid can be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT * from tblHotels WHERE Description is not null
Order by HotelName"
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
Dim rst As New DataTable
rst.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rst
GridView1.DataBind()
End Using
End Using
End Sub
And it outputs the grid - with a working pager:
So, no virtualItemCount seems to be available, but you can still turn on data paging with the gv in .net 4.0.
I've been trying to refresh a GridView when two textbox set me the range then click Search, and returns me the columns in that range.
But the thing is, in first load the GridView fills up with all the columns, that works fine but when I click search button doesn't refresh at all, it just stays the same.
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView CssClass="table" AllowSorting="true" HeaderStyle-HorizontalAlign="Center" EmptyDataText="No hay pendientes." CellPadding="10" HeaderStyle-CssClass="bg-primary p-2 text-dark bg-opacity-25" runat="server" ID="gvBitacora" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="cvebitacora" HeaderText="No" ItemStyle-Font-Bold="true" ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Formulario No." ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# String.Format("{0}-{1}", Eval("cveano"), Eval("cvenumero"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cveusuario" HeaderText="Usuario" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="cveestado" HeaderText="Estado" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="observacion" HeaderText="Observación" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="fechaaccion" HeaderText="Fecha" DataFormatString="{0:dd/ MM/ yyyy}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Revisión" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="btnVer" CssClass="btn btn-primary" runat="server" CommandName="btnVer" CommandArgument="<%# Container.DataItemIndex %>">Ver <i class='far fa-eye'></i></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And here the Button Click in VB
Private Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click
Dim user As User = CType(Session.Item("user"), User)
Dim oBLBitacora As New BLBitacora
Dim lBitacora, lBitacora2 As New List(Of Bitacora)
If user.cverol = 2 Then
If panelsStayOpen_headingOne.Checked = True Then
lBitacora = oBLBitacora.BitacorasGetDate(dateFechaInicio.Text, dateFechaFinal.Text)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
ElseIf user.cverol = 3 Then
lBitacora = oBLBitacora.BitacorasGet(2)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
End Sub
GridView on first load
PAGE LOAD
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim user As User = CType(Session.Item("user"), User)
Dim oBLBitacora As New BLBitacora
Dim lBitacora, lBitacora2 As New List(Of Bitacora)
If Not IsPostBack Then
If user.cverol = 2 Then
lBitacora = oBLBitacora.BitacorasGet(1)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
ElseIf user.cverol = 3 Then
lBitacora = oBLBitacora.BitacorasGet(2)
gvBitacora.DataSource = lBitacora
gvBitacora.DataBind()
End If
Else
If panelsStayOpen_headingOne.Checked = True Then
panelsStayOpen_headingOne.Attributes("class") = "form-check-input"
panelsStayOpen_headingOne.Attributes("aria-expanded") = "true"
panelsStayOpen_collapseOne.Attributes("class") = "accordion-collapse collapse show"
Else
panelsStayOpen_headingOne.Attributes("class") = "collapsed form-check-input"
panelsStayOpen_headingOne.Attributes("aria-expanded") = "false"
panelsStayOpen_collapseOne.Attributes("class") = "accordion-collapse collapse"
End If
End If
where is your page load event showing the initial bind? You'll also want to be sure you call Updatepanel.Update() after the search click.
I've managed to fix it ,the Panel Update needed to be in the initial Form, not only the GridView.
I have one particular column value in the database, which has different values, As I want to change the color of grid in asp.net, based on each value. I don't want to hard code the column value
Thanks in Advance
Ok, assume a table like this:
Ok, so that is a table. I suppose the column could be in the same as our table that drives the grid view, but the process works the same.
Ok, so now our markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" CssClass="table">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
And our code to load the grid:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Dim strSQL As String =
"SELECT tblHotels.ID, FirstName, LastName, HotelName, tblHotels.City, Province, Description, Color from tblHotels
LEFT JOIN tblCityColor on tblCityColor.City = tblHotels.City
ORDER BY HotelName"
Using cmdSQL As SqlCommand = New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST3))
cmdSQL.Connection.Open()
Dim rstTable As New DataTable
rstTable.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstTable
GridView1.DataBind()
End Using
End Sub
The above loads our grid. But for formatting colors? We joined in that above "color" table into the query.
So, now in the row data bound event, we can do this:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gData As DataRowView = e.Row.DataItem
If IsDBNull(gData.Item("Color")) = False Then
e.Row.Cells(3).BackColor = FromName(gData.Item("Color"))
e.Row.Cells(5).BackColor = FromName(gData.Item("Color"))
End If
End If
End Sub
Not that for defaulted gv columns, or databound, the columns appear in the cells array as per above.
Output:
Now it is possible you have some (or all) templated fields in the gv.
You then don't use cells collection, but have to use find control.
So say our grid has this markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" CssClass="table">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescript" runat="server"
Text = '<%# Eval("Description") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So for above, to change the description (which is a plane jane text box). then the code to format background color would be:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gData As DataRowView = e.Row.DataItem
If IsDBNull(gData.Item("Color")) = False Then
e.Row.Cells(3).BackColor = FromName(gData.Item("Color"))
Dim txtDesc As TextBox = e.Row.FindControl("txtDescript")
txtDesc.BackColor = FromName(gData.Item("Color"))
End If
End If
I have a GridView I populate, and want to show/hide the edit link based on whether the person logged in is either an Admin or User. I am not receiving any errors but cannot figure out why its not working.
aspx
<asp:GridView ID="RepView" runat="server" HeaderStyle-BackColor="#bfbfbf" HeaderStyle-ForeColor="White" HeaderStyle-Font-Underline="true" CellPadding="2" GridLines="None" AutoGenerateColumns="false" Width="990px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" />
<asp:BoundField DataField="Shift" HeaderText="Shift" />
<asp:BoundField DataField="Supervisor" HeaderText="Supervisor" />
<asp:BoundField DataField="Center" HeaderText="Center" />
<asp:BoundField DataField="DateSubmitted" HeaderText="Date Entered" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Details">
<ItemTemplate>
Edit
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
Private Sub BindGrid()
Dim DefaultConnection As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Using con As New SqlConnection(DefaultConnection)
'This is not working...
If My.User.IsInRole("User") Then
RepView.Columns(9).Visible = False
ElseIf My.User.IsInRole("Admin") Then
RepView.Columns(9).Visible = True
End If
' End of questionable part....
Using cmd As New SqlCommand()
cmd.CommandText = ("SELECT * from Reps")
cmd.Connection = con
con.Open()
RepView.DataSource = cmd.ExecuteReader()
RepView.DataBind()
con.Close()
End Using
End Using
End Sub
Try moving your code behind "Questionable Part" from the Private Sub BindGrid() to The gridview_Load event. My guess is that what's happening is that the gridview is loading, then it gets to your code Private sub BindGrid() to show/hide columns but the columns are already loaded, therefor it can not hide them. It will continue to show the columns even after running through this code unless you run the check and show/hide the columns in the load event.
One other possible solution is that it looks like in the Private Sub BindGrid() you are trying to hide the columns before the query which means the columns will not have been created yet so you have to create the columns first before you can hide and of them. Move the questionable part down below your sql command.
Try something like this...
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim x As Integer = 0
If x = 0 Then
GridView1.Columns(0).Visible = False
Else
GridView1.Columns(0).Visible = True
End If
End Sub
I have tested this and using x = 0 will hide the first column using x = 1 will show the first column.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Sprayer_Parts_CatalogConnectionString %>"
SelectCommand="SELECT [ID], [Order#] AS column1, [Gallon], [Price] FROM [Tanks]">
</asp:SqlDataSource>