【问题标题】:Best way to create dynamic table with left column header使用左列标题创建动态表的最佳方法
【发布时间】:2011-06-22 09:42:28
【问题描述】:

我正在创建具有 20 多行和列数的动态表,可以根据用户输入进行更改。第一列是标题,其他列需要使用从 Web 服务返回的数据进行绑定。并且有几行可以编辑。当用户单击提交按钮时,需要验证更改的单元格并处理数据。 我已经创建了 ASP.net 表并一一添加了行和单元格。但这不是可重用的方法,有没有其他方法可以创建以左列作为标题的可编辑动态表?

【问题讨论】:

    标签: c# asp.net user-interface dynamic


    【解决方案1】:

    如果您尝试为客户端创建动态可编辑表格,您可能需要使用一些 javascript 框架。那里有很多很棒的。我最近测试了 jQuery 的 DataTablesjQueryjqGrid

    【讨论】:

    • 我正在寻找服务器端替代方案。无论如何感谢您的信息。
    【解决方案2】:

    GridView 支持自定义 HeaderColumn,它是 RowHeaderColumn Property

    查看此演示页面,了解如何提供仅允许对某些行进行编辑:

    aspx:

     <asp:GridView ID="GridView1" runat="server" ShowHeader="true"  
                RowHeaderColumn="Month" AutoGenerateEditButton="True"></asp:GridView>
    

    代码隐藏(对于 VB.Net 感到抱歉,here's 如有必要,请使用转换器)

    Public Class GridViewDemo
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
               bindSampleData()
            End If
        End Sub
    
        Private Sub bindSampleData()
            Dim rnd As New Random(Date.Now.Millisecond)
            Dim data As New DataTable("SampleData")
            data.Columns.Add("Month", GetType(String))
            data.Columns.Add("Sold", GetType(Double))
            data.Columns.Add("Units", GetType(Int32))
            For m As Int32 = 1 To 12
                Dim row As DataRow = data.NewRow
                row("Month") = Globalization.CultureInfo.CurrentCulture.DateTimeFormat().GetMonthName(m)
                row("Sold") = 1000 * rnd.Next(1, 10) + rnd.Next(0, 999)
                row("Units") = 10 * rnd.Next(1, 10) + rnd.Next(0, 99)
                data.Rows.Add(row)
            Next
    
            Me.GridView1.DataSource = data
            Me.GridView1.DataBind()
        End Sub
    
        Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim month As String = DirectCast(DirectCast(e.Row.DataItem, DataRowView)("Month"), String)
                ' don't allow to edit current month's values to demonstrate how to edit certain rows '
                If month.Equals(Globalization.CultureInfo.CurrentCulture.DateTimeFormat().GetMonthName(Date.Now.Month)) Then
                    e.Row.Cells(0).Enabled = False
                Else
                    e.Row.Cells(0).Enabled = True
                End If
            End If
        End Sub
    
        Private Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            GridView1.EditIndex = e.NewEditIndex
            bindSampleData()
        End Sub
    
        Private Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
            GridView1.EditIndex = -1
            bindSampleData()
        End Sub
    
        Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
            Dim records(e.NewValues.Count - 1) As DictionaryEntry
            e.NewValues.CopyTo(records, 0)
            ' Iterate through the array and HTML encode all user-provided values 
            ' before updating the data source.
            Dim entry As DictionaryEntry
            For Each entry In records
               e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())
            Next
            ' process the changes, f.e. write it to the database, senseless with my random sample-data '
    
            GridView1.EditIndex = -1
            bindSampleData()
       End Sub
    End Class
    

    【讨论】:

    • 嘿蒂姆,很好的例子,我仍然找不到将数据绑定为我的问题的更新图像的方法。 header 是表的第一列,数据流逐列,不像普通的行绑定。
    猜你喜欢
    • 2021-01-24
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多