datagrid

G

Guest

I have a Datagrid on the aspx page. and the first column is a template column
with the aspLink. But I want to activate or show it as a link only if the
value in the second column is "true".
How can I do this. Please suggest.

The sample code is below.

<asp:datagrid id="DGVisits" runat="server" AllowSorting="True" Width="643px">
<ItemStyle Font-Size="X-Small" Font-Names="Verdana"
ForeColor="Black"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Names="Verdana" Font-Bold="True"
ForeColor="Black" BackColor="DarkGray"></HeaderStyle>
<Columns><asp:TemplateColumn HeaderStyle-BackColor="LightGrey">
<ItemTemplate>
<asp:linkbutton CommandName="Select" ID="Linkbutton1"
CausesValidation="False" Runat="server"><%#
DataBinder.Eval(Container.dataitem, "ID")%></asp:linkbutton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

Thanks,

Vic
 
K

Ken Cox [Microsoft MVP]

Hi Vic,

I'd jam in an IIF to test the other data value and substitute an empty
string if the other value is false. Here's the idea, let us know if it
helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DGVisits" runat="server" AllowSorting="True"
Width="643px">
<itemstyle font-size="X-Small" font-names="Verdana"
forecolor="Black"></itemstyle>
<headerstyle font-size="X-Small" font-names="Verdana"
font-bold="True" forecolor="Black" backcolor="DarkGray"></headerstyle>
<columns>
<asp:templatecolumn HeaderStyle-BackColor="LightGrey">
<itemtemplate>
<asp:linkbutton CommandName="Select"
ID="Linkbutton1" CausesValidation="False" Runat="server">
<%# iif(DataBinder.Eval(Container.dataitem,
"Boolean"),DataBinder.Eval(Container.dataitem, "ID"),"")%>
</asp:linkbutton>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DGVisits.DataSource = CreateDataSource()
DGVisits.DataBind()
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("ID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
G

Guest

I don't want to display an empty string. I still want to display the ID but
not as link. Is there a way I can do this using datagrid.

Thanks,

Vic


Ken Cox said:
Hi Vic,

I'd jam in an IIF to test the other data value and substitute an empty
string if the other value is false. Here's the idea, let us know if it
helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DGVisits" runat="server" AllowSorting="True"
Width="643px">
<itemstyle font-size="X-Small" font-names="Verdana"
forecolor="Black"></itemstyle>
<headerstyle font-size="X-Small" font-names="Verdana"
font-bold="True" forecolor="Black" backcolor="DarkGray"></headerstyle>
<columns>
<asp:templatecolumn HeaderStyle-BackColor="LightGrey">
<itemtemplate>
<asp:linkbutton CommandName="Select"
ID="Linkbutton1" CausesValidation="False" Runat="server">
<%# iif(DataBinder.Eval(Container.dataitem,
"Boolean"),DataBinder.Eval(Container.dataitem, "ID"),"")%>
</asp:linkbutton>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DGVisits.DataSource = CreateDataSource()
DGVisits.DataBind()
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("ID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


Vic said:
I have a Datagrid on the aspx page. and the first column is a template
column
with the aspLink. But I want to activate or show it as a link only if the
value in the second column is "true".
How can I do this. Please suggest.
 
E

Elton Wang

Hi Vic,

Suppose you get data to a datatable (tbl), you can do it
in following way:

DataView dv = tbl.DefaultView;
dv.RowFilter = "2ndCol = true";
Datagrid.DataSource = dv;
Datagrid.DataBind();

HTH,

Elton Wang
(e-mail address removed)
-----Original Message-----
I have a Datagrid on the aspx page. and the first column is a template column
with the aspLink. But I want to activate or show it as a link only if the
value in the second column is "true".
How can I do this. Please suggest.

The sample code is below.

<asp:datagrid id="DGVisits" runat="server"
AllowSorting="True" Width="643px">
 
G

Guest

Vic, I always find this easier in the code-behind. A few ways to go, but the
gist is that you do what you need in ItemDataBound by first getting the value
of your boolean in column 2 (e.item.findcontrol("MyBoolean")), and then e.g.

a. Have two controls--a linkbutton and a label--in your column 1, and set
each visible= on/off as appropriate, or

b. Have a single control--a hyperlink--and set navigateurl as appropriate (I
think a hyperlink with no navigateurl shows up as a label)

hth,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top