Solved

assigning the data source to a data grid

Posted on 2012-03-20
9
262 Views
Last Modified: 2012-06-22
Hi,

I'm getting a web service response as List<T> and at the moment I'm assigning it to a datagrid data source: i.e.

Exams.ExamBookingService.ExamBookingServiceSoapClient target = new Exams.ExamBookingService.ExamBookingServiceSoapClient();
                GetExamsServiceResponse esr;

                esr = target.GetActiveExams("O6UJ9A001SUJ", "241222576");
                GridView1.DataSource = esr.ExamsDisplay;
                GridView1.DataBind();

The problem is I don't want to show all the column in the list but rather want only the few columns in my grid. How do I manipulate the columns in the grid at run time and where do I assign the datasource to the grid?

Please someone help me?

Thanks in advance!
0
Comment
Question by:ube100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37741756
you have to use template field:-
 <asp:TemplateField HeaderText="FirstName">
                     <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# ((Customer)Container.DataItem).CustPerson.FirstName %>'></asp:Label>
                    </ItemTemplate>
 </asp:TemplateField>

Open in new window

Refer to the following url for more detail:-
http://weblogs.asp.net/gurusarkar/archive/2010/04/28/binding-list-of-custom-class-to-gridview-or-listview-control.aspx
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37741778
do u want to bind the list to a specific column in your grid?
0
 

Author Comment

by:ube100
ID: 37741801
My List got about 10 columns but I just want to display only 6 columns in my grid.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 42

Expert Comment

by:sedgwick
ID: 37741812
if the 6 columns are predefined then u can remove them from the list before bind to the grid.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37741869
did you follow the link i mentioned!!!
Also I hope you know how to use <Columns> and <ItemTemplate> tags in gridview.
0
 

Author Comment

by:ube100
ID: 37742010
Yes I followed the link and this is what I done:

 <Columns>
    <asp:TemplateField HeaderText="ExamCode" runat="server">
                     <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# (Container.DataItem) .ExamCode%>'></asp:Label>
                    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

But I'm getting following error message:
Object does not contain a definition for 'ExamCode' and no extension method 'ExamCode' accepting a first argument of type 'Object' could be found.
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 250 total points
ID: 37742022
Assuming your class name is Exam and there is a property to get exam code namely ExamCode, here is how binding should look like:-

<asp:Label ID="Label2" runat="server" Text='<%#((Exam)Container.DataItem) .ExamCode%>'></asp:Label>

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37742027
Container.DataItem is the the type T in your list so for example if the object is Person and u want to bind the ID then u need to do:
<asp:Label ID="Label2" runat="server" Text='<%# ((Person)(Container.DataItem)).ID%>'></asp:Label>
0
 

Author Closing Comment

by:ube100
ID: 37742067
Thanks!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question