0

我想向我的网站添加一个简单的表格/类似 Excel 的功能,它将显示一些值。所以,我添加了一个网格视图。我根本不使用数据集或数据库投标。我只有一个带有两个条目的小表格。当用户单击按钮时,我想将一些值作为新行添加到 datagridview 控件。如何在 VB.NET 中向 datagridview 控件添加新记录?

我试过这段代码:

GridView1.Rows.Add(New String() {Value1, Value2})

但我收到一个错误:

“添加”不是“System.Web.UI.WebControls.GridViewRowCollection”的成员

我的 ASPX:

<asp:GridView ID="GridView1" runat="server" Width="731px">
    <Columns>
        <asp:BoundField HeaderText="Name" />
        <asp:BoundField HeaderText="Grade" />
    </Columns>
</asp:GridView>
4

2 回答 2

0

好的。使用类似的数据表

protected void Page_Load(object sender,EventArgs e)
{
     if(!IsPostback)
     {
         DataTable myDataTable = new DataTable();

         DataColumn myDataColumn; 

         myDataColumn = new DataColumn();
         myDataColumn.DataType = Type.GetType("System.String");
         myDataColumn.ColumnName = "Value1";
         myDataTable.Columns.Add(myDataColumn);

         myDataColumn = new DataColumn();
         myDataColumn.DataType = Type.GetType("System.String");
         myDataColumn.ColumnName = "Value2";
         myDataTable.Columns.Add(myDataColumn);
         ViewState["Data"]=myDataTable;
     }
}


protected void btnAdd(object sender,EventArgs e)
{
    Datatable Dt=(Datatable)ViewState["Data"];
    DataRow Dr=Dt.NewRow();
    Dr[0]=TextBox1.Text;
    Dr[1]=TextBox2.Text;
    Dt.Rows.Add(Dr);
    ViewState["Data"]=Dt;
}

希望这会帮助你。

于 2012-06-25T22:51:16.943 回答
0

这很容易。您所要做的就是将您的网格绑定到任何Colleciton,例如List、Array、DataTable。创建你的记录类

Public Class MyRecord
    Private _name As String = ""
    Private _grade As Integer = ""

    Public Property Name() As String 
        Get   
            Return Me._name    
        End Get    
        Set(ByVal value As String)   
            Me._name = Value
        End Set   
    End Property

    Public Property Grade() As Integer
        Get   
            Return Me._grade 
        End Get    
        Set(ByVal value As Integer)   
            Me._grade = Value
        End Set   
    End Property

End Class

在您的 Page_Load 中将您的网格绑定到列表

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPotback Then

        Dim list As New List(Of MyRecord)()
        list.Add(New MyRecord() With { _
                       Key .Name = "dsfsdf", _
                       Key .Grade = 45 _
        })
        list.Add(New MyRecord() With { _
                       Key .Name = "dsfsd234f", _
                       Key .Grade = 50 _
        })

        Session("MyList") = list
        GridView1.DataSource = list
        GridView1.DataBind()
    End If
End Sub

然后添加一些带有文本框和按钮的表单并处理按钮 on_click 事件

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
    Dim list As New List(Of MyRecord)        

    If Session("MyList") Is Not Nothing Then
        list = DirectCast(Session("MyList"), List(Of MyRecord)) 
    End If

    list.Add(New MyRecord() With { _
                       Key .Name = txt1.Text, _
                       Key .Grade = Convert.ToInt32(txt2.Text) _
    })


    GridView1.DataSource = list 
    GridView1.DataBind()

End Sub

抱歉,如果有任何错误,我是 C# 开发人员

于 2012-06-25T23:07:08.697 回答