Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / February 2006

Tip: Looking for answers? Try searching our database.

GridView - Display two columns as one

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Felix_WafyTech - 20 Feb 2006 19:02 GMT
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
Steven Cheng[MSFT] - 21 Feb 2006 03:30 GMT
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

Signature

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

Felix_WafyTech - 21 Feb 2006 12:47 GMT
Thanks Steven, your suggestion worked.

> Hi Felix,
>
[quoted text clipped - 43 lines]
>
> #GridView.RowDataBound Event

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.
> rowdatabound.aspx
>
[quoted text clipped - 8 lines]
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Steven Cheng[MSFT] - 22 Feb 2006 01:23 GMT
You're welcome Felix,

Best Regards,

Steven Cheng
Microsoft Online Support

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.