如何在Visual Basic中向datagridview控件添加新记录?

时间:2012-06-25 22:12:58

标签: asp.net .net vb.net datagridview

我想在我的网站上添加一个简单的表/ excel-like feauture,它将显示一些值。所以,我添加了一个gridview。我根本不使用数据集或数据库。我只有一个带有两个条目的小表格。当用户单击按钮时,我想将一些值作为新行添加到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>

2 个答案:

答案 0 :(得分: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;
}

希望这会对你有所帮助。

答案 1 :(得分: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#开发人员