none
求一个GridView的ASP.NET(VB)例子 RRS feed

  • 问题

  • 看CSDN一天了,还是没弄明白: 需求如下: 新建一页面:temp.aspx,页面上画一个GRIDVIEW(不绑定数据源),一个BUTTON 页面加载后,GRIDVIEW显示五空行五空列 想实现鼠标单击任意一单元格,都可以对单元格自由编辑,单元格失去焦点后触发LOSTFOCUS事件 二十五个单元格输入完后,由BUTTON按钮保存数据 特来求一个能满足上列需求的例子(VB)
    2009年4月6日 5:49

答案

  • <%@ Page Language="VB" Debug="true" ValidateRequest="false" %>
    
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dt As DataTable = New DataTable
        For i As Integer = 1 To 5
          dt.Columns.Add(New DataColumn("", GetType(System.String)))
        Next
        For i As Integer = 0 To 4
          dt.Rows.Add(dt.NewRow)
          For j As Integer = 0 To 4
            dt.Rows(i)(j) = Request.Form("txt" + i.ToString() + "_" + j.ToString())
          Next
            
        Next
        GridView1.DataSource = dt
        GridView1.DataBind()
        
      End Sub
    
      Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To 4
          For j As Integer = 0 To 4
            Response.Write("<li>第 " + i.ToString() + " 行第 " + j.ToString() + " 列的值是:" + Request.Form("txt" + i.ToString() + "_" + j.ToString()))
          Next
        Next
        ' 以下代码只是为了实现在表格里面保留原来的值,如果不需要则可以删除。
        Dim dt As DataTable = New DataTable
        For i As Integer = 1 To 5
          dt.Columns.Add(New DataColumn("", GetType(System.String)))
        Next
        For i As Integer = 0 To 4
          dt.Rows.Add(dt.NewRow)
          For j As Integer = 0 To 4
            dt.Rows(i)(j) = Request.Form("txt" + i.ToString() + "_" + j.ToString())
          Next
            
        Next
        GridView1.DataSource = dt
        GridView1.DataBind()
      End Sub
    
    
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          Dim dv As DataRowView = CType(e.Row.DataItem, DataRowView)
          For i As Integer = 0 To e.Row.Cells.Count - 1
            e.Row.Cells(i).Attributes.Add("onclick", "showEdit(" + e.Row.RowIndex.ToString() + "," + i.ToString() + ")")
            e.Row.Cells(i).Text = "<input onblur='lostfocus(this)' name='txt" + e.Row.RowIndex.ToString() + "_" + i.ToString() + "' readonly='readonly' class='noborder' value='" + dv(i).ToString() + "'/>"
          Next
        End If
      End Sub
    
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
      End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <style type="text/css">
        .noborder
        {
          border-width: 0px;
        }
        .hasborder
        {
          border-width: '';
        }
      </style>
    
      <script type="text/javascript">
        function showEdit(r, c) {
          document.forms[0].elements["txt" + r + "_" + c].readOnly = false;
          document.forms[0].elements["txt" + r + "_" + c].className = 'hasborder';
          document.forms[0].elements["txt" + r + "_" + c].select();
        }
        function lostfocus(o) {
          o.className = 'noborder'
          o.readOnly = true;
        }
      </script>
    
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" ShowHeader="false" OnRowDataBound="GridView1_RowDataBound">
      </asp:GridView>
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load" />
      <asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
      </form>
    </body>
    </html>
    
    2009年4月6日 7:48
    版主
  •   Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          Dim dv As DataRowView = CType(e.Row.DataItem, DataRowView)
          For i As Integer = 0 To e.Row.Cells.Count - 1
            e.Row.Cells(i).Attributes.Add("onclick", "showEdit(" + e.Row.RowIndex.ToString() + "," + i.ToString() + ")")
            e.Row.Cells(i).Attributes.Add("style", "width:100px") '此处设置单元格宽度
            e.Row.Cells(i).Text = "<input style='width:100%' onblur='lostfocus(this)' name='txt" + e.Row.RowIndex.ToString() + "_" + i.ToString() + "' readonly='readonly' class='noborder' value='" + dv(i).ToString() + "'/>" '此处里面的100%设置文本框占满单元格
          Next
        End If
      End Sub

    孟宪会
    2009年4月6日 10:27
    版主

全部回复

  • <%@ Page Language="VB" Debug="true" ValidateRequest="false" %>
    
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dt As DataTable = New DataTable
        For i As Integer = 1 To 5
          dt.Columns.Add(New DataColumn("", GetType(System.String)))
        Next
        For i As Integer = 0 To 4
          dt.Rows.Add(dt.NewRow)
          For j As Integer = 0 To 4
            dt.Rows(i)(j) = Request.Form("txt" + i.ToString() + "_" + j.ToString())
          Next
            
        Next
        GridView1.DataSource = dt
        GridView1.DataBind()
        
      End Sub
    
      Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To 4
          For j As Integer = 0 To 4
            Response.Write("<li>第 " + i.ToString() + " 行第 " + j.ToString() + " 列的值是:" + Request.Form("txt" + i.ToString() + "_" + j.ToString()))
          Next
        Next
        ' 以下代码只是为了实现在表格里面保留原来的值,如果不需要则可以删除。
        Dim dt As DataTable = New DataTable
        For i As Integer = 1 To 5
          dt.Columns.Add(New DataColumn("", GetType(System.String)))
        Next
        For i As Integer = 0 To 4
          dt.Rows.Add(dt.NewRow)
          For j As Integer = 0 To 4
            dt.Rows(i)(j) = Request.Form("txt" + i.ToString() + "_" + j.ToString())
          Next
            
        Next
        GridView1.DataSource = dt
        GridView1.DataBind()
      End Sub
    
    
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          Dim dv As DataRowView = CType(e.Row.DataItem, DataRowView)
          For i As Integer = 0 To e.Row.Cells.Count - 1
            e.Row.Cells(i).Attributes.Add("onclick", "showEdit(" + e.Row.RowIndex.ToString() + "," + i.ToString() + ")")
            e.Row.Cells(i).Text = "<input onblur='lostfocus(this)' name='txt" + e.Row.RowIndex.ToString() + "_" + i.ToString() + "' readonly='readonly' class='noborder' value='" + dv(i).ToString() + "'/>"
          Next
        End If
      End Sub
    
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
      End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <style type="text/css">
        .noborder
        {
          border-width: 0px;
        }
        .hasborder
        {
          border-width: '';
        }
      </style>
    
      <script type="text/javascript">
        function showEdit(r, c) {
          document.forms[0].elements["txt" + r + "_" + c].readOnly = false;
          document.forms[0].elements["txt" + r + "_" + c].className = 'hasborder';
          document.forms[0].elements["txt" + r + "_" + c].select();
        }
        function lostfocus(o) {
          o.className = 'noborder'
          o.readOnly = true;
        }
      </script>
    
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" ShowHeader="false" OnRowDataBound="GridView1_RowDataBound">
      </asp:GridView>
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load" />
      <asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
      </form>
    </body>
    </html>
    
    2009年4月6日 7:48
    版主
  • 我刚学习VS2008,感觉很牛很强大; 看了孟版回的贴子,感觉你更牛更强大! 向你致敬!
    2009年4月6日 8:23
  • 再请版主帮一下: 单元格激活之后,出来一个文本框,怎么能保持文本框和单元格的宽度一致呢?如果每行有10列的话,宽度会超出屏幕许多,不怎么美观. 我在源码中的style中添加了width属性(相对值和绝对值)均不成功.
    2009年4月6日 9:09
  •   Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          Dim dv As DataRowView = CType(e.Row.DataItem, DataRowView)
          For i As Integer = 0 To e.Row.Cells.Count - 1
            e.Row.Cells(i).Attributes.Add("onclick", "showEdit(" + e.Row.RowIndex.ToString() + "," + i.ToString() + ")")
            e.Row.Cells(i).Attributes.Add("style", "width:100px") '此处设置单元格宽度
            e.Row.Cells(i).Text = "<input style='width:100%' onblur='lostfocus(this)' name='txt" + e.Row.RowIndex.ToString() + "_" + i.ToString() + "' readonly='readonly' class='noborder' value='" + dv(i).ToString() + "'/>" '此处里面的100%设置文本框占满单元格
          Next
        End If
      End Sub

    孟宪会
    2009年4月6日 10:27
    版主
  • 多谢多谢孟版如此耐心回复! 看了你许多贴子,我对你的景仰如长江之水,滔滔不绝
    2009年4月6日 10:48