GridView - Display two columns as one

F

Felix_WafyTech

Hi,

I would like to display two columns as one. I have two hyperlink columns
that display an image and perform two different actions based on the Id
field in the row. I would like to display both these columns as a single
column. Once that's accomplished I would like to hide or show either one of
the columns or both the columns based on another integer column.

If the integers column value is

0 then I would like to display both the hyperlink columns.
1 then I would like to display the first hyperlink column.
2 then I would like to display the second hyperlink column.
3 then I do not want to display anything.

Is this possible? Any help is greatly appreciated.

Thanks,
Felix.J
 
S

Steven Cheng[MSFT]

Hi Felix,

Welcome to the MSDN newsgroup.

As for the put two columns as one and set the control's visibility
according to another database column's value, I think you can consider
using the following approach:

1. Define a template column(field) in the GridView control and add the two
Hyperlink controls in that template field.

2. Register the GridView's RowDataBound event, retrieve the two hyperlink
controls' reference and set their properties according to the current
DataItem's certain value.

For example, suppose we define the following templateFields in Gridview:

<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:HyperLink id="hlOne" runat="server">Link
One</asp:HyperLink>
<asp:HyperLink id="hlTwo" runat="server">Link
Two</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
........................


In RowDataBound Event, we can use the following code to get inner control
reference:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
HyperLink link1 = e.Row.FindControl("hlOne") as HyperLink;
HyperLink link2 = e.Row.FindControl("hlTwo") as HyperLink;

//e.Row.DataItem
}

Also, e.Row.DataItem is the current DataItem , we can query certain
column's value from it. Use DataBinder.Eval or explicitly cast it to a
concrete data class object(like DataRowView)...

#GridView.RowDataBound Event
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.
rowdatabound.aspx

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top