GridView模板

时间:2009-12-12 16:36:44

标签: asp.net

有关如何格式化/构建/模板GridView的任何帮助,以便excel电子表格的单元格中的内容 显示为:

<li>"looking to display data from column1 here between the li tags"</li>
    <p>"and data from column2 here between the p tags"</p>

<!-- and so row two from excel / objDataset1 would do the same -->
  <li>"data from column1 here"</li>
    <p>"data from column2 here"</p>

etc...

以下是我用来将excel文件中的命名区域转换为gridview的代码:

 Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                & "Data Source=" & Server.MapPath("Spreadsheet.xls") _
                & ";" & "Extended Properties=Excel 8.0;"
    '  original code was:  & "Data Source=" & Server.MapPath("../ExcelData.xls") _ ' ???


    Dim objConn As New OleDbConnection(sConnectionString)
    objConn.Open()

    ' Create new OleDbCommand to return the data from the worksheet / RangeName.
    Dim objCmdSelect As New OleDbCommand("SELECT * FROM RangeName", objConn)
    Dim objAdapter1 As New OleDbDataAdapter()
    objAdapter1.SelectCommand = objCmdSelect
    Dim objDataset1 As New DataSet()

    ' Fill the DataSet with the information from the worksheet / RangeName.
    objAdapter1.Fill(objDataset1, "XLData") ' ?!?!?

    ' Build 
    GridView1.DataSource = objDataset1.Tables(0).DefaultView
    GridView1.DataBind()
    objConn.Close()

----------------------------我追求的一点------------- -------------------------

 <!-- It doesn't do much a present but it does display the info -->
<asp:GridView ID="GridView1" runat="server" style="height: 182px" 
    BackColor="Transparent"
    BorderColor="#DEDFDE"
    BorderStyle="None"
    BorderWidth="1px" 
    CellPadding="4"
    ForeColor="Black"
    GridLines="None">


    <RowStyle BackColor="Transparent"

    />

    <FooterStyle BackColor="#CCCC99" />

   <%-- <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />--%>
    <%-- I do not want to show the column header info from the excel file - which is coming through - also --%>


    <AlternatingRowStyle BackColor="Transparent" />

</asp:GridView>

我甚至不知道我是否应该使用gridview或其他东西 - 所以如果可以的话,请保持简单明了...

3 个答案:

答案 0 :(得分:1)

如果您想要在网页上查看电子表格数据,那么gridview是一个很好的选择。这正是它的设计目标。

顺便说一句,将<li&gt; s与<p>混合在一起是不好的形式。 <p></p>完全可以在<li></li>内生效,但<li>元素可以完全包含所有其他元素。换句话说,您不应该将任何其他标记作为<ul>的直接子项。

编辑: 是的,卡彭特,你很亲密。在<li>内进行格式化没问题。只需使用div将整个格式放在<li>中。例如:

<html>
<head>
<style type="text/css">
.Question {
    background-color: orange;
}
.Answer {
    font-weight: bold;
}
</style>
</head>
<body>
<ol>
<li>
  <div>
    <p class="Question">Q: Why is SO so awesome?</p>
    <p class="Answer">A: Because the community writes the questions.</p>
  </div>
</li>
<li>
  <div>
    <p class="Question">Q: Should I join?</p>
    <p class="Answer">A: Yes, if you want to help the community by upvoting good questions and answers.</p>
  </div>
</li>
<li>
  <div>
    <p class="Question">Q: Do you have to join?</p>
    <p class="Answer">A: No, you can ask questions and read answers anonymously.</p>
  </div>
</li>
</ol>
</body>
</html>

并且不要担心使用答案部分...你没有足够的声誉来评论/编辑,但是在我刚刚通过提出你的问题给你的10之后你会更接近。 : - )

答案 1 :(得分:0)

很抱歉首先回答所有Rap的答案部分 - 我没有Openid所以这是我在这里使用的解决方法。谢谢你的帮助...

我正在寻找一个ol,其中每个li都有一个问题和一个答案,而且那个li然后ap标签看起来对我很好看 - 布局明智......应该是li text那么另一个李......那好吗?

答案 2 :(得分:0)

告诉我如何让它最终发挥作用......&amp;有关代码的任何提示吗?

    ' Create connection string variable. Modify the "Data Source" parameter as
    ' appropriate for your environment.
    Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                & "Data Source=" & Server.MapPath("Spreadsheet.xls") _
                & ";" & "Extended Properties=Excel 8.0;"
    '  original:  & "Data Source=" & Server.MapPath("../ExcelData.xls") _


    Dim objConn As New OleDbConnection(sConnectionString)
    objConn.Open()
    ' Create new OleDbCommand to return data from worksheet / RangeName.
    Dim objCmdSelect As New OleDbCommand("SELECT * FROM RangeName", objConn)
    Dim objAdapter1 As New OleDbDataAdapter()
    objAdapter1.SelectCommand = objCmdSelect

    ' Create new DataSet to hold information from the worksheet / RangeName.
    Dim objDataset1 As New DataSet()

    ' Fill the DataSet with the information from the worksheet / RangeName.
    objAdapter1.Fill(objDataset1, "XLData")

    ' Create a new DataTable object 
    Dim MyTable = New DataTable
    MyTable = objDataset1.Tables(0)

    ' Find how many rows are in the Rows collection 
    ' of the new DataTable object.
    Dim numrows = MyTable.Rows.Count

    If numrows = 0 Then
        display_htmlstring.InnerHtml = "<p>No records found.</p>"
    Else

        Dim rowashtml As String
        rowashtml = "<ol>"

        For loop1 = 0 To numrows - 1
' the excel rangename has column headings of Problem & Answers
            rowashtml = rowashtml & "<li>" & Server.HtmlEncode(MyTable.Rows(loop1).Item("Problem")) _
              & "<p>" & Server.HtmlEncode(MyTable.Rows(loop1).Item("Answers")) & "</p></li>"

        Next loop1
        display_htmlstring.InnerHtml = rowashtml & "</ol>"
    End If

------------------------ the girdView is gone and I am now using just ---------------

<div id="display_htmlstring" runat="server">
  </div>

再次感谢Rap的帮助(li“Stuff can here here hopefuly”p“and pli)