locked
Asp.Net Listview RRS feed

  • Question

  • User-47589850 posted

    Hi,

    I have a excel for pay list. I want to convert mssql and asp.net listview. I can view listview but not monthly like pivot.

    How can I see the moon month? People will come in months after they will be single line and also update on listview. 

    Thanks

    Excel Like

    Name       1   2  3  4   5   6   7   8  9   1 0   11  12

    example 10  40 10 etc.

    ex2         50 160 12

    sql 

    months

    id month year

    1  1         2018

    2  2       2018

    users

    id user 

    1 example

    2 ex2

    pay

    id   userID monthID money

    1   1           1             10

    2   2           1             50

     <asp:ListView ID="ListView1" runat="server">
                <LayoutTemplate>
                    <table>
                        <tr>
                            <td>
                               Name
                            </td>
                            <td>1</td>
                             <td>2</td>
                             <td>3</td>
                        </tr>
    
                   
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
                         </table>
                </LayoutTemplate>
               
                <ItemTemplate>
                 
                  <tr>
                     <td>
                            <%#Eval("name") %>
                          <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("money") %>'></asp:TextBox>
                      </td>
                      </tr>
                </ItemTemplate>
                
            </asp:ListView>

    Friday, February 2, 2018 11:42 AM

All replies

  • User-47589850 posted

    can you help me?

    Saturday, February 3, 2018 9:35 AM
  • User-47589850 posted

    html static 

    <table> <thead> <tr> <td>Name</td> <td>1</td> <td>2</td> </tr> </thead> <tbody> <tr> <td> example</td> </tr> <tr> <td colspan="1"></td> <td>80</td> <td>70</td> </tr> <tr> <td> ex2</td> </tr> <tr> <td colspan="1"></td> <td>100</td> <td>200</td> </tr> </tbody> </table>

    Saturday, February 3, 2018 7:46 PM
  • User1400794712 posted

    Hi asp.netlearning,

    It seems you want to use month as columns' names and users' names as the first column data. You can combine these value in datatable and then bind datatable data with Listview. I used EF to connect to database and made a demo about how to combine value in datatable, please refer to it:

    CS code:

    private TestContext db = new TestContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable listData = new DataTable();
        listData.Columns.Add(new DataColumn("Name"));
        foreach (var item in db.Months.ToList())
        {
            listData.Columns.Add(new DataColumn(item.id.ToString()));
        }
        foreach (var item in db.users.ToList())
        {
            DataRow dr = listData.NewRow();
            var user = db.pay.Where(p => p.userID == item.id).ToList();
            dr[0] = item.user;
            for (int i = 1; i < listData.Columns.Count; i++)
            {
                var data = user.Where(u => u.monthID.ToString() == listData.Columns[i].ToString()).FirstOrDefault();
                if (data == null)
                {
                    dr[i] = "";
                }
                else
                {
                    dr[i] = data.money;
                }
            }
            listData.Rows.Add(dr);
        }
        ListView1.DataSource = listData;
        ListView1.DataBind();
    }

    Aspx code:

    <asp:ListView ID="ListView1" runat="server">
        <LayoutTemplate>
            <table>
                <tr>
                    <td>Name
                    </td>
                    <td>1</td>
                    <td>2</td>
                </tr>
                <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%#Eval("Name") %>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("1") %>'></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("2") %>'></asp:TextBox>
                </td>
            </tr>
        </ItemTemplate>
    
    </asp:ListView>

    How it works:

    Best Regards,

    Daisy

    Monday, February 5, 2018 7:20 AM