• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

assigning the data source to a data grid

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
ube100
Asked:
ube100
  • 3
  • 3
  • 3
1 Solution
 
BuggyCoderCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
do u want to bind the list to a specific column in your grid?
0
 
ube100Author Commented:
My List got about 10 columns but I just want to display only 6 columns in my grid.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Meir RivkinFull stack Software EngineerCommented:
if the 6 columns are predefined then u can remove them from the list before bind to the grid.
0
 
BuggyCoderCommented:
did you follow the link i mentioned!!!
Also I hope you know how to use <Columns> and <ItemTemplate> tags in gridview.
0
 
ube100Author Commented:
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
 
BuggyCoderCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
 
ube100Author Commented:
Thanks!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now