Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: sizeof(struct) in C#.net Pin
bsaksida3-Jun-09 20:38
bsaksida3-Jun-09 20:38 
GeneralRe: sizeof(struct) in C#.net Pin
Luc Pattyn3-Jun-09 23:02
sitebuilderLuc Pattyn3-Jun-09 23:02 
QuestionDataGridView Pin
Yathish hatter3-Jun-09 18:28
Yathish hatter3-Jun-09 18:28 
AnswerRe: DataGridView Pin
Henry Minute4-Jun-09 3:01
Henry Minute4-Jun-09 3:01 
QuestionGet data from a DataSet and display Pin
CodingLover3-Jun-09 17:42
CodingLover3-Jun-09 17:42 
NewsRe: Get data from a DataSet and display Pin
CodingLover3-Jun-09 18:21
CodingLover3-Jun-09 18:21 
QuestionListview Pin
benjamin yap3-Jun-09 16:40
benjamin yap3-Jun-09 16:40 
AnswerRe: Listview Pin
Niladri_Biswas3-Jun-09 19:16
Niladri_Biswas3-Jun-09 19:16 
It is a bit tricky. However I have done that.

Here I am taking a datatable as my datasource

Objective:

Col1 	Col2 	Col3 

1	2	3

4	5	6

7	8	9

10	11	12


I hope you are asking something similar to this.

Approach:

Step 1:

Create a datatable with 3 columns and 4 rows in .cs file of .aspx page PageLoad event

DataTable dtSource = new DataTable();

        #region Data Table Creation

        dtSource.Columns.Add("Col1");

        dtSource.Columns.Add("Col2");

        dtSource.Columns.Add("Col3");       

        #endregion


        #region Add Rows
        dtSource.Rows.Add("1", "2", "3");
        dtSource.Rows.Add("4", "5", "6");
        dtSource.Rows.Add("7", "8", "9");
        dtSource.Rows.Add("10", "11", "12");
        #endregion



Step 2:

Drag & drop a list view in the .aspx page

<asp:ListView ID="myListView" runat="server">
           <LayoutTemplate>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                     
            </LayoutTemplate>  
            
            <ItemTemplate>
            
            </ItemTemplate>           
            
            
        </asp:ListView>



Step 3:

In the code behind, bind the ListView to the datasource(i.e. datatable)


myListView.DataSource = dtSource;

myListView.DataBind();



Step 4:

Create a user control whose .ascx will be

<table>
    <tr>
        <td>
            <asp:Label ID="lblHeaderCol1" runat="server" Text="Col1"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lblHeaderCol2" runat="server" Text="Col2"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lblHeaderCol3" runat="server" Text="Col3"></asp:Label>
        </td>
    </tr>
    
    <tr>
        <td>
            <asp:Label ID="lblContentColumn1" runat="server"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lblContentColumn2" runat="server"></asp:Label>
        </td>
        <td>
            <asp:Label ID="lblContentColumn3" runat="server"></asp:Label>
        </td>
    </tr>
</table>




Step 5:

In the code behind of the usercontrol(.ascx) expose some properties

#region Properties

    public string Col1 { get; set; }

    public string Col2 { get; set; }

    public string Col3 { get; set; }

    public int Counter { get; set; }

    #endregion


As you can make out that the properties name are the same as the column names



Step 6:

In the Load event of the UserControl, consume those properties by the respective label controls

protected void Page_Load(object sender, EventArgs e)
    {
        lblContentColumn1.Text = Col1;

        lblContentColumn2.Text = Col2;

        lblContentColumn3.Text = Col3;

        Counter = Counter + 1;

        if (Counter > 1)
        {
            lblHeaderCol1.Text = lblHeaderCol2.Text = lblHeaderCol3.Text = String.Empty;
        }        
    }



Step 7:

The last step is to call the user control from the .aspx page

Call it from within the item template

<ItemTemplate>
            
            <uc1:MyUserControl ID="MyUserControl1" runat="server" Col1=<%# DataBinder.Eval(Container.DataItem, "Col1")%>  Col2=<%# DataBinder.Eval(Container.DataItem, "Col2")%> Col3=<%# DataBinder.Eval(Container.DataItem, "Col3")%> Counter = <%# Container.DataItemIndex %> />
            
            </ItemTemplate> 




So the final ListView will look similar to this

<asp:ListView ID="myListView" runat="server">
           <LayoutTemplate>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                     
            </LayoutTemplate>  
            
            <ItemTemplate>
            
            <uc1:MyUserControl ID="MyUserControl1" runat="server" Col1=<%# DataBinder.Eval(Container.DataItem, "Col1")%>  Col2=<%# DataBinder.Eval(Container.DataItem, "Col2")%> Col3=<%# DataBinder.Eval(Container.DataItem, "Col3")%> Counter = <%# Container.DataItemIndex %> />
            
            </ItemTemplate>           
            
            
        </asp:ListView>




What I am doing?

Basically I have exposed the properties from the user control similar to the columns available in the source and have kept a counter.

I am calling the user control from inside the item template ; so each time the user control will be iterated.

But in that case the headers will also be iterated. Inorder to make it unique, the counter is used which will allow the header to be displayed for only the first time.


N.B.~ This is one of the solutions that came in my mind instantly.

If if get a better idea, i will tell you that.

Or else, any kind of improvement from your side, is highly welcome.


Hope this helps.
Smile | :)

Niladri Biswas

GeneralRe: Listview Pin
benjamin yap3-Jun-09 23:31
benjamin yap3-Jun-09 23:31 
GeneralRe: Listview Pin
Niladri_Biswas4-Jun-09 4:41
Niladri_Biswas4-Jun-09 4:41 
Questionappend files Pin
Zopilote323-Jun-09 12:30
Zopilote323-Jun-09 12:30 
AnswerRe: append files Pin
Luc Pattyn3-Jun-09 12:38
sitebuilderLuc Pattyn3-Jun-09 12:38 
AnswerRe: append files Pin
Christian Graus3-Jun-09 13:06
protectorChristian Graus3-Jun-09 13:06 
Question[Message Deleted] Pin
har dh3-Jun-09 10:00
har dh3-Jun-09 10:00 
AnswerRe: Need programming tips for my issue. Pin
Not Active3-Jun-09 10:02
mentorNot Active3-Jun-09 10:02 
AnswerRe: Need programming tips for my issue. Pin
EliottA3-Jun-09 10:10
EliottA3-Jun-09 10:10 
AnswerRe: Need programming tips for my issue. Pin
Christian Graus3-Jun-09 10:29
protectorChristian Graus3-Jun-09 10:29 
AnswerRe: [Message Deleted] Pin
Luc Pattyn3-Jun-09 13:37
sitebuilderLuc Pattyn3-Jun-09 13:37 
GeneralRe: [Message Deleted] Pin
Christian Graus3-Jun-09 13:48
protectorChristian Graus3-Jun-09 13:48 
GeneralRe: [Message Deleted] Pin
Luc Pattyn3-Jun-09 14:13
sitebuilderLuc Pattyn3-Jun-09 14:13 
GeneralRe: [Message Deleted] Pin
Christian Graus3-Jun-09 14:24
protectorChristian Graus3-Jun-09 14:24 
GeneralRe: [Message Deleted] Pin
Chris Maunder3-Jun-09 14:51
cofounderChris Maunder3-Jun-09 14:51 
GeneralRe: [Message Deleted] Pin
Christian Graus3-Jun-09 14:55
protectorChristian Graus3-Jun-09 14:55 
GeneralRe: [Message Deleted] Pin
Chris Maunder3-Jun-09 16:36
cofounderChris Maunder3-Jun-09 16:36 
GeneralRe: [Message Deleted] Pin
Luc Pattyn3-Jun-09 15:00
sitebuilderLuc Pattyn3-Jun-09 15:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.