2

我有一个gridview,它通过其中的文本框接受来自数据库表的值,然后有编辑和保存按钮。保存按钮将更新数据保存到表中。我想添加一个按钮,它将在文本框中添加具有空值的新行。请给我通过通用集合列表将新行添加到网格视图的方法。

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>

    <br />
    <asp:Button ID="Button1" runat="server" Text="Refresh" />
    <br />

</div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
    <Columns>
    <asp:TemplateField HeaderText="ProjectId">
        <ItemTemplate>
          <asp:TextBox ID="TextBox3" runat="server"  readonly="true"  Text='<%#         Eval("Projectid") %>'>' ></asp:TextBox>

         </ItemTemplate>                
    </asp:TemplateField>
 <asp:TemplateField HeaderText="ProjectName">
        <ItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" OnTextChanged="TextBox1_TextChanged"  Text='<%# Eval("ProjectName") %>' 
               ></asp:TextBox>

         </ItemTemplate>                
    </asp:TemplateField>
     <asp:TemplateField HeaderText="ProjectModifiedDate">
        <ItemTemplate>

            <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged" ReadOnly="true" Text='<%# Eval("ProjectmodifiedDate")%>'  ></asp:TextBox>
            <asp:Image ID="Image1" runat="server" Height="20px" Width="20px" ImageUrl="~/Images/calender.jpg"/>
            <asp:CalendarExtender ID="CalendarExtender1" Enabled="false" TargetControlID="TextBox2" PopupButtonID="Image1" runat="server">
            </asp:CalendarExtender>


         </ItemTemplate>                
    </asp:TemplateField>
     <asp:TemplateField HeaderText="RecordUpdatedDate">
        <ItemTemplate>
            <asp:TextBox ID="TextBox4" runat="server"  ReadOnly="true" Text='<%# Eval("recordupdateddate")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>

     <asp:TemplateField HeaderText="ProjectLocation">
        <ItemTemplate>
            <asp:TextBox ID="TextBox5" runat="server" OnTextChanged="TextBox5_TextChanged" ReadOnly="true" Text='<%# Eval("ProjectLocation")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>

     <asp:TemplateField HeaderText="LocationServerName">
        <ItemTemplate>
            <asp:TextBox ID="TextBox6" runat="server" OnTextChanged="TextBox6_TextChanged" ReadOnly="true" Text='<%# Eval("LocationServerName")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>

     <asp:TemplateField HeaderText="ProjectModifiedBy">
        <ItemTemplate>
            <asp:TextBox ID="TextBox7" runat="server" OnTextChanged="TextBox7_TextChanged" ReadOnly="true" Text='<%# Eval("ProjectModifiedBy")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>

     <asp:TemplateField HeaderText="DBServer">
        <ItemTemplate>
            <asp:TextBox ID="TextBox8" runat="server" OnTextChanged="TextBox8_TextChanged" ReadOnly="true" Text='<%# Eval("DBServer")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>

     <asp:TemplateField HeaderText="DBName">
        <ItemTemplate>
            <asp:TextBox ID="TextBox9" runat="server" OnTextChanged="TextBox9_TextChanged" ReadOnly="true" Text='<%# Eval("DBName")%>'  > > </asp:TextBox>
         </ItemTemplate>                
    </asp:TemplateField>
   </Columns>


   </asp:GridView>

   <br />
   <br />
   <asp:Button ID="Button4" runat="server" Text="Add New Row" />
   <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
   <br />

   <br />
     <asp:Button ID="Button3" runat="server" Text="Edit" />

     <asp:Button ID="Button2"  runat="server" Text="Save" Enabled="false"/>
     <asp:Label ID="Label1" runat="server"></asp:Label>
   <br />

    </form>

后面的代码

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Partial Class Projectinfo
Inherits System.Web.UI.Page

Shared projectinfolist As New List(Of projectinfoclass)



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim objtable As New DataTable("projectinfoclass")
    Dim MyDS As DataSet
    Dim Conn As SqlConnection
    Dim Cmd As SqlDataAdapter
    Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
    Cmd = New SqlDataAdapter("select * from ProjectInfoNew", Conn)
    MyDS = New DataSet()


    Cmd.Fill(objtable)

    projectinfolist.Clear()
    For Each dr As DataRow In objtable.Rows
        projectinfolist.Add(New projectinfoclass With {.projectId = dr("Projectid").ToString(), .Projectname = dr("ProjectName").ToString(), .projectmodifiedDate = dr("ProjectmodifiedDate").ToString(), .ProjectLocation = dr("ProjectLocation").ToString(), .LocationServerName = dr("LocationServerName").ToString(), .ProjectModifiedBy = dr("ProjectModifiedBy").ToString(), .DBServer = dr("DBServer").ToString(), .DBName = dr("DBName").ToString()})
    Next


    GridView1.DataSource = objtable
    GridView1.DataBind()

End Sub

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Try

        For Each obj In projectinfolist

            If obj.Dirty = True Then

                Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
                Dim sqlquery As String
                sqlquery = "Sp_UpdateProjectInfo"
                Dim con As New SqlConnection(strConnString)
                Dim cmd As New SqlCommand(sqlquery, con)
                cmd.CommandType = CommandType.StoredProcedure

                Dim projectname As String = obj.Projectname
                Dim projectmodifieddate As String = obj.projectmodifiedDate
                Dim projectid As String = obj.projectId
                Dim Recordupdateddate As String = obj.Recordupdateddate
                Dim ProjectLocation As String = obj.ProjectLocation
                Dim LocationServerName As String = obj.LocationServerName
                Dim ProjectModifiedBy As String = obj.ProjectModifiedBy
                Dim DBServer As String = obj.DBServer
                Dim DBName As String = obj.DBName

                cmd.Parameters.Add(New SqlParameter("@Projectid", SqlDbType.VarChar))
                cmd.Parameters("@Projectid").Value = System.Convert.ToInt32(projectid)

                cmd.Parameters.Add(New SqlParameter("@projectname", SqlDbType.VarChar))
                cmd.Parameters("@projectname").Value = projectname

                cmd.Parameters.Add(New SqlParameter("@projectmodifieddate", SqlDbType.VarChar))
                cmd.Parameters("@projectmodifieddate").Value = projectmodifieddate

                cmd.Parameters.Add(New SqlParameter("@ProjectLocation", SqlDbType.VarChar))
                cmd.Parameters("@ProjectLocation").Value = ProjectLocation

                cmd.Parameters.Add(New SqlParameter("@LocationServerName", SqlDbType.VarChar))
                cmd.Parameters("@LocationServerName").Value = LocationServerName

                cmd.Parameters.Add(New SqlParameter("@ProjectModifiedBy", SqlDbType.VarChar))
                cmd.Parameters("@ProjectModifiedBy").Value = ProjectModifiedBy

                cmd.Parameters.Add(New SqlParameter("@DBServer", SqlDbType.VarChar))
                cmd.Parameters("@DBServer").Value = DBServer

                cmd.Parameters.Add(New SqlParameter("@DBName", SqlDbType.VarChar))
                cmd.Parameters("@DBName").Value = DBName



                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()


                Label1.Text = "Record inserted successfully"

                con.Dispose()

            End If

        Next
    Catch ex As Exception
        Throw ex
    Finally

    End Try


End Sub


Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)


    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex

    projectinfolist.Item(rowindex).Projectname = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True
End Sub

Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).projectmodifiedDate = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub



Protected Sub TextBox5_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).ProjectLocation = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub

Protected Sub TextBox6_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).LocationServerName = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub

Protected Sub TextBox7_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).ProjectModifiedBy = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub

Protected Sub TextBox8_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).DBServer = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub

Protected Sub TextBox9_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim txtbx As TextBox = TryCast(sender, TextBox)
    Dim row As GridViewRow = txtbx.Parent.NamingContainer
    Dim rowindex As Integer = row.RowIndex
    projectinfolist.Item(rowindex).DBName = txtbx.Text
    projectinfolist.Item(rowindex).Dirty = True


End Sub

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
    For Each row As GridViewRow In GridView1.Rows
        Dim tb1 As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
        tb1.ReadOnly = False
        tb1.BorderColor = Drawing.Color.BurlyWood
        Dim tb2 As TextBox = DirectCast(row.FindControl("TextBox2"), TextBox)
        tb2.ReadOnly = False
        tb2.BorderColor = Drawing.Color.BurlyWood

        Dim cl As AjaxControlToolkit.CalendarExtender = DirectCast(row.FindControl("CalendarExtender1"), AjaxControlToolkit.CalendarExtender)
        cl.Enabled = True

        Dim tb5 As TextBox = DirectCast(row.FindControl("TextBox5"), TextBox)
        tb5.ReadOnly = False
        tb5.BorderColor = Drawing.Color.BurlyWood
        Dim tb6 As TextBox = DirectCast(row.FindControl("TextBox6"), TextBox)
        tb6.ReadOnly = False
        tb6.BorderColor = Drawing.Color.BurlyWood
        Dim tb7 As TextBox = DirectCast(row.FindControl("TextBox7"), TextBox)
        tb7.ReadOnly = False
        tb7.BorderColor = Drawing.Color.BurlyWood
        Dim tb8 As TextBox = DirectCast(row.FindControl("TextBox8"), TextBox)
        tb8.ReadOnly = False
        tb8.BorderColor = Drawing.Color.BurlyWood
        Dim tb9 As TextBox = DirectCast(row.FindControl("TextBox9"), TextBox)
        tb9.ReadOnly = False
        tb9.BorderColor = Drawing.Color.BurlyWood
    Next
    Button2.Enabled = True
End Sub

End Class

班级

Imports Microsoft.VisualBasic

Public Class projectinfoclass


Public _Projectname As String

Public Property Projectname() As String
    Get
        Return _Projectname
    End Get
    Set(ByVal value As String)
        _Projectname = value
    End Set
End Property

Public _projectmodifiedDate As String
Public Property projectmodifiedDate() As String
    Get
        Return _projectmodifiedDate
    End Get
    Set(ByVal value As String)
        _projectmodifiedDate = value
    End Set
End Property

Public _projectid As String
Public Property projectId() As String
    Get
        Return _projectid
    End Get
    Set(ByVal value As String)
        _projectid = value
    End Set
End Property

Public _Recordupdateddate As String
Public Property Recordupdateddate() As String
    Get
        Return _Recordupdateddate
    End Get
    Set(ByVal value As String)
        _Recordupdateddate = value
    End Set
End Property

Public _ProjectLocation As String
Public Property ProjectLocation() As String
    Get
        Return _ProjectLocation
    End Get
    Set(ByVal value As String)
        _ProjectLocation = value
    End Set
End Property

Public _LocationServerName As String
Public Property LocationServerName() As String
    Get
        Return _LocationServerName
    End Get
    Set(ByVal value As String)
        _LocationServerName = value
    End Set
End Property

Public _ProjectModifiedBy As String
Public Property ProjectModifiedBy() As String
    Get
        Return _ProjectModifiedBy
    End Get
    Set(ByVal value As String)
        _ProjectModifiedBy = value
    End Set
End Property

Public _DBServer As String
Public Property DBServer() As String
    Get
        Return _DBServer
    End Get
    Set(ByVal value As String)
        _DBServer = value
    End Set
End Property

Public _DBName As String
Public Property DBName() As String
    Get
        Return _DBName
    End Get
    Set(ByVal value As String)
        _DBName = value
    End Set
End Property



Public _Dirty As Boolean
Public Property Dirty() As Boolean
    Get
        Return _Dirty
    End Get
    Set(ByVal value As Boolean)
        _Dirty = value
    End Set
End Property

结束类

4

1 回答 1

0

我有一个类似的问题....我所做的是在表格中创建一个空白行并将其绑定到gridview,并将最后一行置于编辑模式!

于 2014-02-23T01:58:16.557 回答